Advertisement
RealHero

Arduino 1.10

Jun 5th, 2021
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #define RED_PIN 11
  2. #define BLUE_PIN 10
  3. #define GREEN_PIN 9
  4. #define RED_BUT_PIN 4
  5. #define BLUE_BUT_PIN 3
  6. #define GREEN_BUT_PIN 2
  7.  
  8. void setup(){
  9. pinMode(RED_PIN, OUTPUT);
  10. pinMode(BLUE_PIN, OUTPUT);
  11. pinMode(GREEN_PIN, OUTPUT);
  12.  
  13. pinMode(RED_BUT_PIN, INPUT_PULLUP);
  14. pinMode(BLUE_BUT_PIN, INPUT_PULLUP);
  15. pinMode(GREEN_BUT_PIN, INPUT_PULLUP);
  16. }
  17.  
  18. int red = 0;
  19. int blue = 0;
  20. int green = 0;
  21.  
  22. boolean redWasUp = true;
  23. boolean blueWasUp = true;
  24. boolean greenWasUp = true;
  25.  
  26. void loop(){
  27. Control(red, redWasUp, RED_PIN, RED_BUT_PIN);
  28. Control(blue, blueWasUp, BLUE_PIN, BLUE_BUT_PIN);
  29. Control(green, greenWasUp, GREEN_PIN, GREEN_BUT_PIN);
  30. }
  31.  
  32. void Control(int &color, boolean &wasUp, int colorPin, int butPin){
  33. if(handleClick(butPin, wasUp)){
  34. if(color == 0)
  35. color = 255;
  36. else
  37. color = 0;
  38. analogWrite(colorPin, color);
  39. }
  40. }
  41.  
  42. boolean handleClick(int buttonPin, boolean wasUp){
  43. boolean isUp = digitalRead(buttonPin);
  44. if (wasUp && !isUp){
  45. delay(10);
  46. isUp = digitalRead(buttonPin);
  47. }
  48. if(isUp && !isUp){
  49. delay(10);
  50. isUp = digitalRead(buttonPin);
  51. return !isUp;
  52. }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement