programming for switching between three functions of LEDs by a press switch

Hi guys,

I am Joe, a freshman in Arduino world. I am doing this my project: I want to switch three loop functions (2GreenLed-full brightness → this 2GreenLed-half brightness → 1RedLed-full brightness → back) by every time pressing my press-switch.

I have done:
1, Let Arduino automatically switch three functions base on the following code:

int ledPin = 11;
int ledPin2 = 12;
int ledPin3 = 13;

void setup() {               
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, LOW);
  delay(1000);
 
  analogWrite(ledPin, 50);
  analogWrite(ledPin2, 50);
  digitalWrite(ledPin3, LOW);
  delay(1000);
 
  digitalWrite(ledPin, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, HIGH);
  delay(1000);
}

2, Based on the below code, I can switch between 2GreenLed-full brightness and 1RedLed-full brightness by every time pressing my press-switch:

int ledPin = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int buttonPin = 2;

int val = 0;
int old_val = 0;   
int state = 0;     

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin, INPUT); // and BUTTON is an input
}

void loop(){
  val = digitalRead(buttonPin);
  if ((val == HIGH) && (old_val == LOW)){
    state = 1 - state;
    delay(10);
  }

  old_val = val;

  if (state == 1) {     
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    // turn LED ON
  } else {
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2,LOW);
    digitalWrite(ledPin3,HIGH);
  }
}

But, how can I reach my goal? I should use "switch...case..."? Could any one teach me please? Many thanks!

I am waiting on line. Thanks for any kind people!

zhouding464:
I am waiting on line.

Members will help as and when they can, but never forget it’s a Forum not a Help Desk 8)

JimboZA:
Members will help as and when they can, but never forget it's a Forum not a Help Desk 8)

Yes, u r right!
I just begun my arduino yesterday. I've spent a whole day to try my best to do experiments. And I except to communicate with you guys and gain suggestion for my project.

You have 3 states. You already have a state variable. So increment the state variable every time the button is pushed - 1,2,3. When it reaches 3, reset it to 1. Use the state variable to select an action the same way you already do (it may not be the best way, but it will probably work for now). Nothing about this is a giant leap from where you are now.

aarg:
You have 3 states. You already have a state variable. So increment the state variable every time the button is pushed - 1,2,3. When it reaches 3, reset it to 1. Use the state variable to select an action the same way you already do (it may not be the best way, but it will probably work for now). Nothing about this is a giant leap from where you are now.

Hi there! Thanks for your response.

I am not sure that you mentioned "increment the state variable every time the button is pushed - 1,2,3. When it reaches 3, reset it to 1"... Could you please remind me the reference I should use?
Many thanks!

state = state + 1;
if (state > 3) state = 1;

Hi there,

Thanks for your response! As a supper new learner, I don't fully understand your reference.

I changed my code based on your reminding as below, but it doesn't work... :cry:

int ledPin = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int buttonPin = 2;

int val = 0; 
int old_val = 0;   
int state = 0;     

void setup() { 
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin, INPUT); // and BUTTON is an input 
} 

void loop(){ 
  val = digitalRead(buttonPin); 
  
  if ((val == HIGH) && (old_val == LOW)){ 
    state = 1 - state;
    delay(10);
  } 

  old_val = val; // val is now old, let's store it 

  if (state == 1) {      
    analogWrite(ledPin, 50); 
    analogWrite(ledPin2, 50);
    digitalWrite(ledPin3, LOW);
    // turn LED ON 
  } else { 
    digitalWrite(ledPin, HIGH); 
    digitalWrite(ledPin2,HIGH);
    digitalWrite(ledPin3,LOW);
  } 

  if ((val == HIGH) && (old_val == LOW)){ 
    state = state + 1;
    delay(10);
  } 

  old_val = val; // val is now old, let's store it 

  if (state > 3) {      
    digitalWrite(ledPin, HIGH); 
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    // turn LED ON 
  } else { 
    digitalWrite(ledPin, LOW); 
    digitalWrite(ledPin2,LOW);
    digitalWrite(ledPin3,HIGH);
  } 
}

Could you please teach me? Many thanks!

aarg:

state = state + 1;

if (state > 3) state = 1;

aarg:

state = state + 1;

if (state > 3) state = 1;

Hi man,

I responded you on this page. thank you so much!

It looks like you tried to cut and paste my code without understanding what I told you. In your example code at the top, you successfully handle 2 states. You switch between them using the variable “state”. Now you want to expand the number of states from 2 to 3. What do you need to do with “state”? Where in your code do you modify “state”? That is a part that you need to change. Since an if-else will only handle 2 alternative actions, you need to change your LED tests to handle 3 (hint - by testing “state”).

I think you are under the impression that you can code your way to a solution. But actually, you need to understand the problem and define a logical plan of attack before you can code it.

Please don’t beg for responses. It’s a form of “bumping” and the moderators take dim view of it.