SHARE
TWEET

Using Digital Read with Count

jmyean Mar 26th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Jung Min Yean
  3.  * 03/26/19
  4.  * Program about turning on and off one LED with a button
  5.  *
  6.   */
  7.  
  8.  
  9.  // Declare Constants and variables
  10. const byte Button=9;
  11. const byte LedPin=17;
  12. byte ReadVal; // variable to read button
  13. const byte LedPin2 = 19;
  14. int count=0;
  15.  
  16. void setup() {
  17.   pinMode(LedPin, OUTPUT); //pinMode assign mode of the pin (input or output)
  18.   pinMode(LedPin2, OUTPUT);
  19.   pinMode(Button, INPUT);
  20.   Serial.begin(9600); //activate the monitor
  21. }
  22. void loop() {
  23.   ReadVal = digitalRead(Button); //when on the ReadVal should be 1 and off should be 0, otherwise it is not being read
  24.   delay(500);
  25.   //Serial.println(ReadVal);
  26.   if(ReadVal ==1) // == is asking if the variable has a value of 1
  27.   {
  28.     count ++;
  29.   }
  30.   if (count == 1)  
  31.   {
  32.     digitalWrite(LedPin, HIGH);
  33.     digitalWrite(LedPin2, LOW);
  34.   }
  35.   else if(count == 2)
  36.   {
  37.     digitalWrite(LedPin2, HIGH);
  38.     digitalWrite(LedPin, LOW);
  39.   }
  40.   else if(count == 3)
  41.   {
  42.     digitalWrite(LedPin, HIGH);
  43.     digitalWrite(LedPin2, HIGH);
  44.   }
  45.   else
  46.   {
  47.     digitalWrite(LedPin, LOW);
  48.     digitalWrite(LedPin2, LOW);
  49.     count = 0;
  50.   }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top