0

I'm currently learning Arduino. So right now I'm trying to create an Arduino app that will use buttons, LEDs and RGB.

So I have 2 buttons, 8 LEDs and an RGB. The first button is the function switcher: when you press it the LED will light according to their function. The second button is the function activator so right now here's I want to do:

  • Function 1: Turn on/off RED light
  • Function 2: Increase RED light brightness by 5; if already at 255, reset at 0
  • Function 3: Turn on/off GREEN light
  • Function 4: Increase GREEN light brightness by 5; if already at 255, reset at 0
  • Function 5: Turn on/off BLUE light
  • Function 6: Increase BLUE light brightness by 5; if already at 255, reset at 0
  • Function 7: Turn on ALL lights, all brightness to 255
  • Function 8: Turn off ALL lights, all brightness to 0

I already started coding and I'm stuck at turning on and off the red light. Here's my code:

const int red = 5;

int b1, b2; //Buttons
int dir = 0;
int light = 11;
int func = 0;
boolean buttonValue = true;
boolean buttonState = true;

void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(red, OUTPUT);
  for (int i = 11; i <= 18; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  b1 = digitalRead(2);
  if (b1 == HIGH) {
    dir = 2;
    func++;
    if (func >= 8)
      func = 0;
    } else {
      dir = 0;
    }
    digitalWrite(light, LOW);
    if (dir == 2 && light < 18) {
      light++;
    }
    digitalWrite(light, HIGH);
    delay(200);
  }

void function(int pin) {
  b2 = digitalRead(3);
  switch (func) {
    case 1:
      if (b2 = true) {
        digitalWrite(5, HIGH);
      } else {
        digitalWrite(5, LOW);
      }
  }
}
1
  • ArduinoMenu library can do that, using a code driven menu (driven by the 2 buttons) and an headless menu (no output devices). Menu event handlers can then control the led, I've done this for an RGB led with only 2 buttons. github.com/neu-rah/ArduinoMenu
    – neu-rah
    Commented Oct 28, 2017 at 9:32

2 Answers 2

1

I have to be honest after reading you description and looking at you code I have no idea how you arrived at the code you have arrived at.

For now lets try a much simpler bit of code, once that is working you can build on it.

#define PIN_BUTTON_1    2
#define PIN_LED_RED     5

void setup() {
    pinMode(PIN_BUTTON_1, INPUT);
    pinMode(PIN_LED_RED, OUTPUT);
}

void loop() {
    const int buttonState1 = digitalRead(PIN_BUTTON_1);
    digitalWrite(PIN_LED_RED, buttonState1);
    delay(200);
}

If that code doesn't work then there is something wrong electrically.

I think is the next step you were trying for:

#define PIN_BUTTON_1    2
#define PIN_LED_RED     5

int lastButtonState1 = LOW;
int functionNumber = 0;
int currentLedValue = 0;

void setup() {
    pinMode(PIN_BUTTON_1, INPUT);
    pinMode(PIN_LED_RED, OUTPUT);

}

void loop() {
    const int currentButtonState1 = digitalRead(PIN_BUTTON_1);
    if (lastButtonState1 != currentButtonState1)
    {   // If the button's state has changed
        if (currentButtonState1 == HIGH)
        {
            ++functionNumber;
        }
        lastButtonState1 = currentButtonState1;
    }
    DoFunction(functionNumber);
    delay(200);
}

void DoFunction(const int& function)
{
    switch (function)
    {
    case 1:
        if (currentLedValue > 0)
        {
            currentLedValue = LOW;
        }
        else
        {
            currentLedValue = HIGH;
        }
        digitalWrite(PIN_LED_RED, currentLedValue);
        break;
    case 2:
        currentLedValue = 128;
        analogWrite(PIN_LED_RED, currentLedValue)
        break;
    default:
    }
}
0

If you would like to call the function you defined at the end of your code you need to give it a name function name()....

In your code you used if (b2 = true). That will not work as expected, since it sets the b2 variable to true. Note the if expressions use == to compare values, see the Arduino Reference. Further you should use the HIGH and LOW Pin level constants for comparing pin states as described here.

So your code will look like if (b2 == HIGH) { digitalWrite(5, HIGH); } else { digitalWrite(5, LOW); }

or shorter just: digitalWrite(5, b2); without the if/else expressions.

1
  • Hi! I already figured it out but I was wondering why the RGB doesn't turn on.
    – Da Phuc
    Commented Jan 30, 2017 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.