Jay_

Arduino Chapter 7 EX1

Mar 8th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. /**
  2.  Arduno: Chapter 7 Exercise 1
  3. **/
  4.  
  5. int ledPin = 9; // choose the pin for the LED
  6. int inputPin1 = 3; // button 1
  7. int inputPin2 = 2; // button 2
  8.  
  9. void setup() {
  10.   pinMode(ledPin, OUTPUT); // declare LED as output
  11.   pinMode(inputPin1, INPUT); // make button 1 an input
  12.   pinMode(inputPin2, INPUT); // make button 2 an input
  13. }
  14.  
  15. int value = 0;
  16.  
  17. // Exercise 1: Use the buttons to control the LEDs on/off state by fading it
  18. void loop()
  19. {
  20.   if (digitalRead(inputPin1) == LOW)
  21.   {
  22.     value--;
  23.   }
  24.   else if (digitalRead(inputPin2) == LOW)
  25.   {
  26.     value++;
  27.   }
  28.   value = constrain(value, 0, 255);
  29.   analogWrite(ledPin, value);
  30.   delay(10);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment