Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
: [Select]
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(3); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
int n1 = servo1.toInt();
int n2 = servo2.toInt();
5 Servo
include <Servo.h>
Servo myservo;
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int target=0;
int sekarang=0;
void setup() {
myservo.attach(9);
myservo.write(90);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
buttonState4 = digitalRead(button4);
buttonState5 = digitalRead(button5);
if(buttonState1 == HIGH) target=0;
if(buttonState2 == HIGH) target=45;
if(buttonState3 == HIGH) target=90;
if(buttonState4 == HIGH) target=135;
if(buttonState5 == HIGH) target=180;
if(target>sekarang){
for(int i=sekarang;i<=target;i++){
myservo.write(i);
delay(5);}
sekarang=target;}
if(sekarang>target){
for(int i=sekarang;i>=target;i--){
myservo.write(i);
delay(5);}
sekarang=target;}
}