Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <Servo.h>
  2. Servo mouth;
  3. Servo neck;
  4.  
  5. const int buttonPin = 7;
  6. int buttonState;
  7. int mouthPos = -1;
  8. int lastButtonState = LOW;
  9.  
  10. long lastDebounceTime= 0;
  11. long debounceDelay = 50;
  12.  
  13. void setup() {
  14.   mouth.attach(3);
  15.   neck.attach(2);
  16.  pinMode(buttonPin, INPUT);
  17.  mouth.write(95);
  18.  neck.write(75);
  19. }
  20.  
  21. void loop() {
  22.  
  23.   int reading = digitalRead(buttonPin);
  24.   if(reading != lastButtonState) {
  25.     lastDebounceTime = millis();
  26.   }
  27.   if((millis() - lastDebounceTime) > debounceDelay) {
  28.     if(reading != buttonState) {
  29.       buttonState = reading;
  30.       if(buttonState == HIGH) {
  31.          toggleMouth();
  32.       }
  33.     }
  34.   }
  35.   lastButtonState = reading;
  36.   int neckPot = analogRead(2);
  37.   int neckPos = map(neckPot,0,1024,15,75);
  38.   neck.write(neckPos);
  39. }
  40.  
  41. void toggleMouth(){
  42.    if(mouthPos == -1) {
  43.      mouthPos = 1;
  44.      mouth.write(75);
  45.    } else {
  46.      mouthPos = -1;
  47.      mouth.write(95);
  48.    }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement