Created
April 24, 2012 12:14
-
-
Save silviopaganini/2479187 to your computer and use it in GitHub Desktop.
Button controlling Servo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo servo; | |
int btnPin = 2; | |
int lightPins[] = {3, 5, 6}; | |
int serPin = 11; | |
int pos = 0; | |
void setup() | |
{ | |
servo.attach(serPin); | |
pinMode(btnPin, INPUT); | |
for(int a = 0; a<3; a++) { | |
pinMode(lightPins[a], OUTPUT); | |
} | |
} | |
void loop() | |
{ | |
int a = digitalRead(btnPin); | |
if(a == 1) { | |
green(); | |
pos++; | |
pos = min(180, pos); | |
} | |
else { | |
red(); | |
pos--; | |
pos = max(0, pos); | |
} | |
servo.write(pos); | |
delay(15); | |
} | |
void red() | |
{ | |
analogWrite(lightPins[0], 255); | |
analogWrite(lightPins[1], 0); | |
analogWrite(lightPins[2], 0); | |
} | |
void green() | |
{ | |
analogWrite(lightPins[0], 0); | |
analogWrite(lightPins[1], 255); | |
analogWrite(lightPins[2], 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment