multiple conditions for the same process

Hi Guys,

I am using a Mega 2560 for my project.

I want to use a ON/OFF button to control LEDs running in sequence (i.e. flashing in order)

A 4-pin rotary switch is used for dimming the brightness of LEDs.

Here is my code:

byte button = 7;
byte led[4] = 
{
  2,3,4,5}; // 4 leds.
byte rSwitch[4] = 
{
  8,9,10,11}; // 4-position rotary switch
unsigned long int brightness;

void setup()
{
  pinMode(button, INPUT);
  for(int i = 0; i<4; i++) 
  {
    pinMode(led[i], OUTPUT); 
  } 

  for(int a = 0; a<4; a++) 
  {
    pinMode(rSwitch[a], INPUT);
  }
}



void loop()
{
  if(digitalRead(button) == HIGH) // if button is pressed
  {
    if(digitalRead(rSwitch[1]) == HIGH)  //select 1st brightness
    {
      brightness = 10; // set brightness
      blink_led_task(); // blink LED function
    }

    else if(digitalRead(rSwitch[2]) == HIGH)  //select 2nd brightness
    {  
      brightness = 50; // set brightness
      blink_led_task(); // blink button
    }

    else if(digitalRead(rSwitch[3]) == HIGH)  //select 3rd brightness
    {  
      brightness = 100; // set brightness
      blink_led_task(); // blink LED function
    }

    else if (digitalRead(rSwitch[4]) == HIGH) //select 2nd brightness
    {
      brightness = 200; // set brightness
      blink_led_task(); // blink LED function
    }
  }
  else
  {
    for(int i = 0; i<4; i++)
      digitalWrite(led[i], LOW); // turn off all led.
  }
}

unsigned long blink_led_time;

void blink_led_task()
{
  if(millis() - blink_led_time > 500) // 500 ms.
    blink_led_time = millis();
  else
    return;
  static byte number = 0;
  for(int i = 0; i<4; i++)
    digitalWrite(led[i], LOW); // turn off all leds first.
  analogWrite(led[number], brightness);
  number++;
  if(number == 4)
    number = 0; // reset back to zero.
  // digitalWrite(led, digitalRead(led) ^ 1); // toggle led.
}

It compiles but does not work as I expected, nothing happens if I turn the rotary switch or press the button.

Actually it was developed from the old version:

byte button = 8;
byte led[4] = {
  2,3,4,5}; // 4 leds.
void setup()
{
  pinMode(button, INPUT);
  for(int i = 0; i<4; i++) 
  {
    pinMode(led[i], OUTPUT); 
    digitalWrite(led[i], LOW);
  }
}
void loop()
{
  if(digitalRead(button) == HIGH) // if button is pressed
  {
    blink_led_task(); // blink button
  }
  else
  {
    for(int i = 0; i<4; i++)
      digitalWrite(led[i], LOW); // turn off all led.
  }
}
unsigned long blink_led_time;
void blink_led_task()
{
  if(millis() - blink_led_time > 500) // 500 ms.
    blink_led_time = millis();
  else
    return;
  static byte number = 0;
  for(int i = 0; i<4; i++)
    digitalWrite(led[i], LOW); // turn off all leds first.
  digitalWrite(led[number], HIGH);
  number++;
  if(number == 4)
    number = 0; // reset back to zero.
  // digitalWrite(led, digitalRead(led) ^ 1); // toggle led.
}

The original one works perfectly.

I couldn't see why it doesn't work after adding the rotary switch.

Could anyone give me advice and hints ? Thank you very much

Haven't you got a thread open about this in the Leds & Multiplexing section?

dannable:
Haven't you got a thread open about this in the Leds & Multiplexing section?

I thought it should be quite basic problem for Arduino programming, may be it is better to place it under general Project Guidance.

But I went to the Microcontrollers one wrongly.

Sorry for duplicating.

  else if (digitalRead(rSwitch[4]) == HIGH)

Your array only has 4 elements. You start by reading element "1" and finish by reading element "4", which doesn't exist. I think you meant to use 0, 1, 2, and 3.

byte led[4] =  {2,3,4,5}; // 4 leds.

//...
analogWrite(led[number], brightness);

Pins 2 and 4 aren't PWM pins and do not support calls to analogWrite(). Well, it will still set them to HIGH or LOW, based on the value you pass.

unsigned long int brightness;

brightness can't be more than 255. Why make it a long? It just needs to be an int, or even a byte.

Since you have not used the internal pull-up resistors on your INPUTs, have wired external pull-downs? (I assume so, since you are referencing the buttons as active HIGH).

Also, nothing will happen unless the button is pressed and held. Is that what you intended?