4 Cases with switch

Hy everybody,

I like to use the case instead a Loop (for/while etc.)

Case 1 should be started with an Switch (Main program)

Case 2 should be started with a photocell (error 1) and Interrupt case1 & default

Case 3 should be started with the second photocell (error 2) and Interrupt case 1 & default

Case 4 / default should be the Standby program (green LED on High until an other Case is running)

So I just need help to do that without an PC so that I could use a button/Switch.

I can’t find any code/Sketch which could do this, so I would be happy If anyone could help me.

Kindly regards
Dennis

You have already made a start by defining the cases, which is good.

Some pseudo code to give you some ideas

case = 4
start of loop()
  switch (case)
    case 1
      run main program

    case 2
      run code for case 2
  
    case 3
      run case for case 3
  
    case 4
      turn on green LED
  end of switch

  if switch is turned on
    case = 1
  end if

  if photocell 1 turned on
    case = 2
  end if
  
  if photocell 2 turned on
    case = 3
  end if
  
end of loop()

Yes, it's exactly what I need.

THX for your time.

But now I don't know how I could integrate my switch into the case code. Yesterday I've tried the 4 case code with some LED's to test it, but i must engerfir case 1 a "1" into the Arduino program, i don't know how i could use a switch instead my PC running the Arduino Software.

Sry for my Bad english.

Kindly regards Dennis

When you say switch do you mean a physical switch or a software switch/case ?

If the former do you know how to read the state of a switch connected to a pin ?

Hello,

yes i know

I could usw them

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()

But how could i integrat a physical switch into the case code /sketch?

Look at my pseudo code in post #1. The suggestion is to read the state of the physical switch [u]after[/u] the switch/case so that it is read on every pass through loop()

thx, but I don’t get it :frowning:

i’ve tried it, but I Don’t know where to write the my contions in the cases,

so I need a compare between 2 photoresistor analog signal and between a digital button

both photoresistors Don’t get light, button ON> Program should start
one photoresistors Don’t get light, button ON> Program shouldn’t start
both photoresistors get light, button ON> Program shouldn’t start

i like to write this in a if/else or in a 4 case switch statement,

int sensorPin = A0; //photoresistor1
int sensorPin1 = A1; //photoresistor2
int sensorValue1 = 0;
int sensorValue2 = 1;
int ledPin = 13;//green
int ledPin1 = 4;//blue
int ledPin2 = 2;//red
int tasterPin = 5; //physical button (START,Normaly Open)
 
void setup()
{ 
 pinMode(ledPin, OUTPUT);
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 
 Serial.begin(9600);
}
 
void loop()
{ 
 sensorValue2 = analogRead(sensorPin1)/4;//0-1023 zu 0-255
 Serial.println(sensorValue2);
 sensorValue1 = analogRead(sensorPin)/4;
 
 if (sensorValue1 < 100) {
 digitalWrite(ledPin, HIGH);
 delay(100);
 }

 
 else if (sensorValue1 > 100){
  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin1, HIGH);
   delay(100);
 }
  if (sensorValue2 > 100){
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
     delay(100);
 }
}

button code

int ledPin = 13;
int tasterPin = 5;
 
void setup(){
  pinMode(ledPin,OUTPUT);
  pinMode(tasterPin,INPUT);
}
 
void loop(){
  if (digitalRead(tasterPin)==HIGH){
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

photoresistior code

int sensorPin = A0;
int sensorValue1 = 0;
int ledPin = 13;
 
void setup()
{ 
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
}
 
void loop()
{ 
 sensorValue1 = analogRead(sensorPin)/4;//0-1023 zu 0-255
 Serial.println(sensorValue1);
 
 if (sensorValue1 < 100) {
 digitalWrite(ledPin, HIGH);
 }
 else 
 { 
 digitalWrite(ledPin, LOW);
 }

 delay(100);
}

many thanks,

now I have a good base for case statements, until now i just used the if/else

// 2xPhotocell Interrupted
// + Pressing Button Starts the program

// Program does not start
// Nearby a light barrier ( empty container)
// Or no pressing of the switch ( contact)




int sensorPin = A0;     //Photocell 1
int sensorPin2 = A1;    //Photocell 2
int sensorValue2 = 1;   //Photocell 1 Value
int sensorValue1 = 0;   //Photocell 2 Value
int ledPin = 13;    //Green LED
int ledPin2 = 2;    //Red   LED
int ledPin3 = 3;    //Yellow LED
int ledPin4 = 4;    //Blue LED
int tasterPin = 5;  //Button (Normaly Open)
 //colorful LED's just for demonstration
void setup()
{ 
 pinMode(ledPin, OUTPUT);
 pinMode(tasterPin,INPUT);
 Serial.begin(9600);
}

void loop()
{ 
 sensorValue1 = analogRead(sensorPin)/256;//0-1023 zu 0-255
  sensorValue2 = analogRead(sensorPin2)/256;
  Serial.println(sensorValue2);

 if (sensorValue1 >2&&sensorValue2 >2&&digitalRead(tasterPin)==HIGH ) {
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin2, HIGH);
delay(250);
digitalWrite(ledPin3, HIGH);
delay(250);
digitalWrite(ledPin4, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin2, LOW);
delay(250);
digitalWrite(ledPin3, LOW);
delay(250);
digitalWrite(ledPin4, LOW);
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin2, HIGH);
delay(250);
digitalWrite(ledPin3, HIGH);
delay(250);
digitalWrite(ledPin4, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin2, LOW);
delay(250);
digitalWrite(ledPin3, LOW);
delay(250);
digitalWrite(ledPin4, LOW);
 }
 else 
 { 
 digitalWrite(ledPin2, HIGH);//Standby/Error
 }}

but now I use 2 more functions, because i like to have -standby program -Main Program -Error1 Program -Error2 Program

so that I could see on LED'S which of this program runs