Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 15 October 2019
- Reddit RGB on-off - v 1.0
- Sketch to cycle through RGB output, with start/stop buttons.
- I tested it on a Nano by only connecting the buttons and watching the serial monitor output.
- Note: this uses a lot of dynamic memory, but that is because of all the serial.print() statements in it.
- */
- const int button_to_turn_on_leds__pin = 2; // Renamed variable to make it more understandable.
- const int button_to_turn_off_leds__pin = 3; // Renamed variable to make it more understandable.
- // Also, are you using pulldown resistors or not? It is good to note that at the pin declarations.
- // !!!!!!!!!! I changed both the buttons to use INPUT_PULLUP. !!!!!!!!!!!!
- bool leds_on_button_pin__value = false;
- bool leds_off_button_pin__value = false;
- // You should always name your variables so that you know just from the variable name what they are being used for.
- // If it is for a pin number, then make the variable name end with "pin".
- // If it is for some other value, then make the variable end in "value".
- const int redPin = 9;
- const int greenPin = 10;
- const int bluePin = 11;
- int counter = 0; // <---------------------- I don't know what this is supposed to be used for?
- // added variables:
- // The three variables below are used for checking the buttons only once every 250 milliseconds.
- // 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.
- int button_check_interval_milliseconds = 250;
- unsigned long button_check_begin_time = 0;
- unsigned long button_check_current_time = 0;
- bool cycle_led_colors = false; // This controls if the LEDs change colors or not.
- int led_color_phase = 1; // This is to control which LED will be turned on. 1 = red, 2 = green and 3 = blue.
- // Using two separate variables to control the on/off and color means that it will resume on the color it left off on.
- int led_cycle_time_milliseconds = 1000; // This is how fast you want the LEDs to change colors.
- unsigned long led_cycle_begin_time = 0;
- unsigned long led_cycle_current_time = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(button_to_turn_on_leds__pin, INPUT_PULLUP);
- pinMode(button_to_turn_off_leds__pin, INPUT_PULLUP);
- // You did not say how your buttons are connected. Declaring them as INPUT implies that they have pull-down resistors...???
- // Using INPUT_PULLUP is easier since that way you don't need a pull-down resistor for each button.
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- pinMode(13, OUTPUT); // You might not need these couple lines....
- digitalWrite(13, LOW); // Some of the China clone boards will turn pin #13 on unless you tell them to turn it off.
- // perform_turn_on_command(); // <----------------- if you want the LEDs to start out running, then you can uncomment this line.
- Serial.println("Exiting setup()");
- }
- void loop() {
- check_if_it_is_time_to_check_the_buttons(); // This function checks if it is time to check the buttons or not.
- check_to_cycle_leds();
- /*
- int buttonState1;
- buttonState1 = digitalRead(buttonPin1);
- int buttonState2;
- buttonState2 = digitalRead(buttonPin2);
- if (buttonState1, buttonState2 == LOW) { // <----------------------------------- this statement form is incorrect for the Arduino IDE (maybe for C/C++ also?)
- // --------------------------------------------- Also, your button pins are set to {INPUT} but you are checking for a LOW state here? Did you mean to use INPUT_PULLUP?
- counter++;
- delay(150);
- }
- else if (counter == 0) {
- digitalWrite(redPin, LOW);
- digitalWrite(greenPin, LOW);
- digitalWrite(bluePin, LOW);
- }
- else if (counter == 1) {
- digitalWrite(redPin, HIGH);
- delay(1000);
- digitalWrite(redPin, LOW);
- delay(1000);
- digitalWrite(greenPin, HIGH);
- delay(1000);
- digitalWrite(greenPin, LOW);
- delay(1000);
- digitalWrite(bluePin, HIGH);
- delay(1000);
- digitalWrite(bluePin, LOW);
- delay(1000);
- }
- else {
- counter = 0;
- }
- */
- } // -------------- end of main loop()
- void check_if_it_is_time_to_check_the_buttons() {
- // This function checks if it is time to check the buttons or not.
- button_check_current_time = millis();
- if (button_check_current_time >= button_check_begin_time) {
- if (button_check_current_time >= (button_check_begin_time + button_check_interval_milliseconds)) {
- check_turn_on_button();
- check_turn_off_button();
- button_check_begin_time = millis();
- }
- }
- else {
- button_check_begin_time = millis();
- }
- }
- void check_turn_on_button() {
- if (cycle_led_colors == false) { // The 'on' button only works if the LEDs are turned off. This also prevents double-cycling of the button.
- leds_on_button_pin__value = digitalRead(button_to_turn_on_leds__pin);
- if (leds_on_button_pin__value == LOW) {
- Serial.println("check_turn_on_button(): leds_on button pressed");
- perform_turn_on_command();
- }
- }
- }
- void perform_turn_on_command() {
- Serial.println("perform_turn_on_command(): turning leds on");
- start_up_led_cycle();
- }
- void check_turn_off_button() {
- if (cycle_led_colors == true) { // The 'off' button only works if the LEDs are turned on. This also prevents double-cycling of the button.
- leds_off_button_pin__value = digitalRead(button_to_turn_off_leds__pin);
- if (leds_off_button_pin__value == LOW) {
- Serial.println("check_turn_off_button(): leds_off button pressed");
- perform_turn_off_command();
- }
- }
- }
- void perform_turn_off_command() {
- Serial.println("perform_turn_off_command(): turning leds off");
- cycle_led_colors = false;
- turn_all_leds_off();
- }
- void check_to_cycle_leds() {
- // If the leds are
- if (cycle_led_colors == true) {
- check_timer_to_cycle_leds();
- }
- }
- void check_timer_to_cycle_leds() {
- // This function checks if it is time to cycle the LED color or not.
- led_cycle_current_time = millis();
- if (led_cycle_current_time >= led_cycle_begin_time) {
- if (led_cycle_current_time >= (led_cycle_begin_time + led_cycle_time_milliseconds)) {
- rotate_led_color_cycle();
- display_led_color_cycle();
- led_cycle_begin_time = millis();
- }
- }
- else {
- led_cycle_begin_time = millis();
- }
- }
- void start_up_led_cycle() {
- // Anything needed to start up the LED light cycle goes in here.
- Serial.println("start_up_led_cycle(): starting leds");
- cycle_led_colors = true;
- led_cycle_begin_time = millis(); // ---- resetting this time makes sure that the current color led will stay on for the full time.
- display_led_color_cycle();
- }
- void rotate_led_color_cycle() {
- Serial.println("rotate_led_color_cycle(): changing LED color");
- led_color_phase++;
- if (led_color_phase == 4) {
- led_color_phase = 1;
- }
- }
- void display_led_color_cycle() {
- switch (led_color_phase) {
- case 1:
- Serial.println("display_led_color_cycle(): red led on");
- digitalWrite(redPin, HIGH);
- digitalWrite(greenPin, LOW);
- digitalWrite(bluePin, LOW);
- break;
- case 2:
- Serial.println("display_led_color_cycle(): green led on");
- digitalWrite(redPin, LOW);
- digitalWrite(greenPin, HIGH);
- digitalWrite(bluePin, LOW);
- break;
- case 3:
- Serial.println("display_led_color_cycle(): blue led on");
- digitalWrite(redPin, LOW);
- digitalWrite(greenPin, LOW);
- digitalWrite(bluePin, HIGH);
- break;
- }
- }
- void turn_all_leds_off() {
- Serial.println("turn_all_leds_off(): turning all leds off");
- digitalWrite(redPin, LOW);
- digitalWrite(greenPin, LOW);
- digitalWrite(bluePin, LOW);
- }
- // ~~~~~~~~~ end ~~~~~~~~~~
Add Comment
Please, Sign In to add comment