skizziks_53

Reddit arduino RGB flasher 1.0

Oct 15th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.73 KB | None | 0 0
  1. /*
  2.    15 October 2019
  3.    Reddit RGB on-off - v 1.0
  4.  
  5.    Sketch to cycle through RGB output, with start/stop buttons.
  6.  
  7.    I tested it on a Nano by only connecting the buttons and watching the serial monitor output.
  8.  
  9.    Note: this uses a lot of dynamic memory, but that is because of all the serial.print() statements in it.
  10. */
  11.  
  12. const int button_to_turn_on_leds__pin = 2; // Renamed variable to make it more understandable.
  13. const int button_to_turn_off_leds__pin = 3; // Renamed variable to make it more understandable.
  14. // Also, are you using pulldown resistors or not? It is good to note that at the pin declarations.
  15.  
  16. // !!!!!!!!!! I changed both the buttons to use INPUT_PULLUP. !!!!!!!!!!!!
  17.  
  18. bool leds_on_button_pin__value = false;
  19. bool leds_off_button_pin__value = false;
  20. // You should always name your variables so that you know just from the variable name what they are being used for.
  21. // If it is for a pin number, then make the variable name end with "pin".
  22. // If it is for some other value, then make the variable end in "value".
  23.  
  24. const int redPin = 9;
  25. const int greenPin = 10;
  26. const int bluePin = 11;
  27. int counter = 0; // <---------------------- I don't know what this is supposed to be used for?
  28.  
  29.  
  30. // added variables:
  31. // The three variables below are used for checking the buttons only once every 250 milliseconds.
  32. // This also means that you don't need to de-bounce the buttons, since the time between checks is far longer than they would bounce anyway.
  33. int button_check_interval_milliseconds = 250;
  34. unsigned long button_check_begin_time = 0;
  35. unsigned long button_check_current_time = 0;
  36.  
  37.  
  38.  
  39. bool cycle_led_colors = false; // This controls if the LEDs change colors or not.
  40. int led_color_phase = 1; // This is to control which LED will be turned on. 1 = red, 2 = green and 3 = blue.
  41. // Using two separate variables to control the on/off and color means that it will resume on the color it left off on.
  42.  
  43. int led_cycle_time_milliseconds = 1000; // This is how fast you want the LEDs to change colors.
  44. unsigned long led_cycle_begin_time = 0;
  45. unsigned long led_cycle_current_time = 0;
  46.  
  47.  
  48. void setup() {
  49.   Serial.begin(9600);
  50.  
  51.   pinMode(button_to_turn_on_leds__pin, INPUT_PULLUP);
  52.   pinMode(button_to_turn_off_leds__pin, INPUT_PULLUP);
  53.   // You did not say how your buttons are connected. Declaring them as INPUT implies that they have pull-down resistors...???
  54.   // Using INPUT_PULLUP is easier since that way you don't need a pull-down resistor for each button.
  55.   pinMode(redPin, OUTPUT);
  56.   pinMode(greenPin, OUTPUT);
  57.   pinMode(bluePin, OUTPUT);
  58.  
  59.   pinMode(13, OUTPUT); // You might not need these couple lines....
  60.   digitalWrite(13, LOW); // Some of the China clone boards will turn pin #13 on unless you tell them to turn it off.
  61.  
  62.   // perform_turn_on_command(); // <----------------- if you want the LEDs to start out running, then you can uncomment this line.
  63.  
  64.   Serial.println("Exiting setup()");
  65. }
  66.  
  67.  
  68.  
  69. void loop() {
  70.  
  71.   check_if_it_is_time_to_check_the_buttons(); // This function checks if it is time to check the buttons or not.
  72.  
  73.   check_to_cycle_leds();
  74.  
  75.   /*
  76.     int buttonState1;
  77.     buttonState1 = digitalRead(buttonPin1);
  78.  
  79.  
  80.     int buttonState2;
  81.     buttonState2 = digitalRead(buttonPin2);
  82.  
  83.     if (buttonState1, buttonState2 == LOW) { // <----------------------------------- this statement form is incorrect for the Arduino IDE (maybe for C/C++ also?)
  84.     // --------------------------------------------- Also, your button pins are set to {INPUT} but you are checking for a LOW state here? Did you mean to use INPUT_PULLUP?
  85.     counter++;
  86.     delay(150);
  87.     }
  88.     else if (counter == 0) {
  89.     digitalWrite(redPin, LOW);
  90.     digitalWrite(greenPin, LOW);
  91.     digitalWrite(bluePin, LOW);
  92.     }
  93.     else if (counter == 1) {
  94.     digitalWrite(redPin, HIGH);
  95.     delay(1000);
  96.     digitalWrite(redPin, LOW);
  97.     delay(1000);
  98.     digitalWrite(greenPin, HIGH);
  99.     delay(1000);
  100.     digitalWrite(greenPin, LOW);
  101.     delay(1000);
  102.     digitalWrite(bluePin, HIGH);
  103.     delay(1000);
  104.     digitalWrite(bluePin, LOW);
  105.     delay(1000);
  106.     }
  107.     else {
  108.     counter = 0;
  109.     }
  110.   */
  111. } // -------------- end of main loop()
  112.  
  113.  
  114.  
  115.  
  116. void check_if_it_is_time_to_check_the_buttons() {
  117.   // This function checks if it is time to check the buttons or not.
  118.   button_check_current_time = millis();
  119.   if (button_check_current_time >= button_check_begin_time) {
  120.     if (button_check_current_time >= (button_check_begin_time + button_check_interval_milliseconds)) {
  121.       check_turn_on_button();
  122.       check_turn_off_button();
  123.       button_check_begin_time = millis();
  124.     }
  125.   }
  126.   else {
  127.     button_check_begin_time = millis();
  128.   }
  129. }
  130.  
  131. void check_turn_on_button() {
  132.   if (cycle_led_colors == false) { // The 'on' button only works if the LEDs are turned off. This also prevents double-cycling of the button.
  133.     leds_on_button_pin__value = digitalRead(button_to_turn_on_leds__pin);
  134.     if (leds_on_button_pin__value == LOW) {
  135.       Serial.println("check_turn_on_button(): leds_on button pressed");
  136.       perform_turn_on_command();
  137.     }
  138.   }
  139.  
  140. }
  141.  
  142. void perform_turn_on_command() {
  143.   Serial.println("perform_turn_on_command(): turning leds on");
  144.   start_up_led_cycle();
  145. }
  146.  
  147. void check_turn_off_button() {
  148.   if (cycle_led_colors == true) { // The 'off' button only works if the LEDs are turned on. This also prevents double-cycling of the button.
  149.     leds_off_button_pin__value = digitalRead(button_to_turn_off_leds__pin);
  150.     if (leds_off_button_pin__value == LOW) {
  151.       Serial.println("check_turn_off_button(): leds_off button pressed");
  152.       perform_turn_off_command();
  153.     }
  154.   }
  155.  
  156. }
  157.  
  158. void perform_turn_off_command() {
  159.   Serial.println("perform_turn_off_command(): turning leds off");
  160.   cycle_led_colors = false;
  161.   turn_all_leds_off();
  162. }
  163.  
  164. void check_to_cycle_leds() {
  165.   // If the leds are
  166.   if (cycle_led_colors == true) {
  167.     check_timer_to_cycle_leds();
  168.   }
  169. }
  170.  
  171. void check_timer_to_cycle_leds() {
  172.   // This function checks if it is time to cycle the LED color or not.
  173.   led_cycle_current_time = millis();
  174.   if (led_cycle_current_time >= led_cycle_begin_time) {
  175.     if (led_cycle_current_time >= (led_cycle_begin_time + led_cycle_time_milliseconds)) {
  176.       rotate_led_color_cycle();
  177.       display_led_color_cycle();
  178.       led_cycle_begin_time = millis();
  179.     }
  180.   }
  181.   else {
  182.     led_cycle_begin_time = millis();
  183.   }
  184. }
  185.  
  186. void start_up_led_cycle() {
  187.   // Anything needed to start up the LED light cycle goes in here.
  188.   Serial.println("start_up_led_cycle(): starting leds");
  189.   cycle_led_colors = true;
  190.   led_cycle_begin_time = millis(); // ---- resetting this time makes sure that the current color led will stay on for the full time.
  191.   display_led_color_cycle();
  192. }
  193.  
  194. void rotate_led_color_cycle() {
  195.   Serial.println("rotate_led_color_cycle(): changing LED color");
  196.   led_color_phase++;
  197.   if (led_color_phase == 4) {
  198.     led_color_phase = 1;
  199.   }
  200. }
  201.  
  202. void display_led_color_cycle() {
  203.   switch (led_color_phase) {
  204.     case 1:
  205.       Serial.println("display_led_color_cycle(): red led on");
  206.       digitalWrite(redPin, HIGH);
  207.       digitalWrite(greenPin, LOW);
  208.       digitalWrite(bluePin, LOW);
  209.       break;
  210.     case 2:
  211.       Serial.println("display_led_color_cycle(): green led on");
  212.       digitalWrite(redPin, LOW);
  213.       digitalWrite(greenPin, HIGH);
  214.       digitalWrite(bluePin, LOW);
  215.       break;
  216.     case 3:
  217.       Serial.println("display_led_color_cycle(): blue led on");
  218.       digitalWrite(redPin, LOW);
  219.       digitalWrite(greenPin, LOW);
  220.       digitalWrite(bluePin, HIGH);
  221.       break;
  222.   }
  223. }
  224.  
  225. void turn_all_leds_off() {
  226.   Serial.println("turn_all_leds_off(): turning all leds off");
  227.   digitalWrite(redPin, LOW);
  228.   digitalWrite(greenPin, LOW);
  229.   digitalWrite(bluePin, LOW);
  230. }
  231.  
  232.  
  233.  
  234.  
  235.  
  236. // ~~~~~~~~~ end ~~~~~~~~~~
Add Comment
Please, Sign In to add comment