#include <Servo.h>
Servo myservo; // create servo object to control a servo
int led = 11;
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
String str = "";
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(led, OUTPUT);
}
void loop() {
ledWrite(str);
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
Serial.println((float)val*5/1024);
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(1000); // waits for the servo to get there
}
void ledWrite(String val)
{
while(Serial.available()>0){
val += char(Serial.read());
delay(20);
}
if(val.length()>0){
Serial.println("str:"+val);
if(val.compareTo("open")>0){
digitalWrite(led,HIGH);
Serial.println("LED已经点亮!");
}
else if(val.compareTo("close")>0){
digitalWrite(led,LOW);
Serial.println("LED已经熄灭!");
}
}
val = "";
}