0

I'm trying to make a lamp which quality to change color by pressing a button, pressing once turns on red, pressing again turns on blue and so on, but I also want it to have a light effect in which it smoothly switches between different colors, my code does, however when pressing the button and getting to the last function and keep pressing the button no longer does the light change, it just stays in that state, I have been trying to solve it and investigating for hours but I do not get the expected result, I hope you can help me, thanks.

I enclose the wiring diagram and the code used. wiring diagram

#define varfija 0

int ledR = 11;
int ledB = 10;
int ledG = 9;
int pinButton = 7;
int buttonValue;
int count = 0;
int brillo;
int button_old = 0;

void setup() {
    pinMode(ledR, OUTPUT);
    pinMode(ledG, OUTPUT);
    pinMode(ledB, OUTPUT);
    pinMode(pinButton, INPUT);
}

void loop() {
    buttonValue = digitalRead(pinButton);
    if (button_old == 0 && buttonValue == 1) {
        count++;
        button_old = 1;
    }
    if (button_old == 1 && buttonValue == 0)
        button_old = 0;
    if (count > 4)
        count = 0;

    if (count == 0) {
        analogWrite(ledR, 0);
        analogWrite(ledG, 0);
        analogWrite(ledB, 0);
    }
    else if (count == 1) {
        analogWrite(ledR, 255);
        analogWrite(ledG, 0);
        analogWrite(ledB, 0);
    }
    else if (count == 2) {
        analogWrite(ledR, 0);
        analogWrite(ledG, 255);
        analogWrite(ledB, 0);
    }
    else if (count == 3) {
        analogWrite(ledR, 0);
        analogWrite(ledG, 0);
        analogWrite(ledB, 0);
    }
    else if (count == 4) {
        for (int i = 1; i < 255; i++) {
            analogWrite(ledR, i);
            analogWrite(ledB, varfija);
            analogWrite(ledG, varfija);
            delay(5);
        }
        for (int i = 255; i > 0; i--) {
            analogWrite(ledR, varfija);
            analogWrite(ledB, i);
            analogWrite(ledG, varfija);
            delay(5);
        }
        for (int i = 0; i < 255; i++) {
            analogWrite(ledR, varfija);
            analogWrite(ledB, varfija);
            analogWrite(ledG, i);
            delay(5);
        }
    }
}

sorry for my english, I don't master it

4
  • 1
    Do a Web search for “Arduino debounce button”. Jun 13 '20 at 12:54
  • 2
    It takes a 3*255*5 milliseconds between button checks in state 4 (every 3.825s) so you have to hold it for some time and then it might get noticed...
    – KIIV
    Jun 13 '20 at 14:22
  • 1
    You want to check the button, but at the same time run the animation that takes around 4 seconds. To combine these you'd have to change the animation code, so it no longer uses any delays. So it's no longer blocking the button code. But for example uses the time (millis) to determine the values or the red green and blue led. A simpler version of this problem is Blink without delay.
    – Gerben
    Jun 14 '20 at 13:44
  • I posted an answer to a very similar question recently: arduino.stackexchange.com/questions/76084/… Note how that code uses millis to debounce the button presses. You'll have to do something similar to get the LED color to change over time. I suggest a simple state machine where you have states for the different colors plus states for transitions between colors.
    – Duncan C
    Jun 15 '20 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.