Stap 2: Voorbeeldcode
#include Servo myservo;int LED = 3; //the number of the LED pin int val = 0; //variable to store the sensor valueint pos = 0;int light =0;void setup(){// initialize the digital pin as an output. pinMode(LED,OUTPUT); // initialize serial communication at 9600 bits per second: Serial.begin(9600); // attaches the servo on pin 9 to the servo object myservo.attach(9); // tell servo to go to position in 0 degree myservo.write(0); }void loop(){ val = analogRead(0); // read the input on analog pin 0 Serial.println(val); // print out the value you read //Once smaller than the set value,increasing angles if(val<40){ pos = pos +2; if(pos >= 90){ //After moverd to 90 degree, keep in this degree pos = 90; } // tell servo to go to position in variable 'pos' myservo.write(pos); delay(100); // As the angle increases, increased brightness LED light = map(pos,0,90,0,255); analogWrite(LED,light); //set the brightness }else{ pos = pos -2; //minus 2 degree if(pos <= 0){ pos = 0; //reduced up to 0 degrees } myservo.write(pos); delay(100); light = map(pos,0,90,0,255); analogWrite(LED,light); }}