Advertisement
CaptainSpaceCat

Arduino - RGButtons

Mar 28th, 2017
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define RED 7
  2. #define BLUE 6
  3. #define GREEN 5
  4. #define R_BUTTON 2
  5. #define G_BUTTON 1
  6. #define B_BUTTON 0
  7.  
  8. int rFlag = 0;
  9. int gFlag = 0;
  10. int bFlag = 0;
  11.  
  12. bool rState = true;
  13. bool gState = true;
  14. bool bState = true;
  15.  
  16. void setup() {
  17.    pinMode(RED, OUTPUT);
  18.    pinMode(GREEN, OUTPUT);
  19.    pinMode(BLUE, OUTPUT);
  20.    pinMode(R_BUTTON, INPUT_PULLUP);
  21.    pinMode(G_BUTTON, INPUT_PULLUP);
  22.    pinMode(B_BUTTON, INPUT_PULLUP);
  23. }
  24.  
  25. void loop() {
  26.   digitalWrite(RED, rFlag);
  27.   digitalWrite(GREEN, gFlag);
  28.   digitalWrite(BLUE, bFlag);
  29.  
  30.   if ((digitalRead(R_BUTTON) == LOW) && rState) {
  31.     rFlag = (rFlag + 1) % 2;
  32.     rState = false;
  33.   } else if (digitalRead(R_BUTTON) == HIGH) {
  34.     rState = true;
  35.   }
  36.  
  37.   if ((digitalRead(G_BUTTON) == LOW) && gState) {
  38.     gFlag = (gFlag + 1) % 2;
  39.     gState = false;
  40.   } else if (digitalRead(G_BUTTON) == HIGH) {
  41.     gState = true;
  42.   }
  43.  
  44.   if ((digitalRead(B_BUTTON) == LOW) && bState) {
  45.     bFlag = (bFlag + 1) % 2;
  46.     bState = false;
  47.   } else if (digitalRead(B_BUTTON) == HIGH) {
  48.     bState = true;
  49.   }
  50. }
Advertisement
RAW Paste Data Copied
Advertisement