Arduino switch case / if ...else

hey everybody,

I'd like to write an difficult sketch and need help.

I've got 4 LED's in different colours.
in a few weeks I want to integrate 2 sensors via analog pin in my program.

but I don't know how to use "if, else if, else" or "case1", "case2","case3" , "case4".

Now I've tried "if, else if, else"
but it doesn't work.

below u see the code with my "default program"
I'd like to use the 2 new "IR" sensors for stopping
my default programm and to show that a container is empty.
(one IR sensor per container)

so if container1 is empty , program stops and LED red is HiGH
so if container2 is empty , program stops and LED orange is HiGH
until i filled the containers.

#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo

const int ledPin = 7;
const int tasterPin = 2;
int potPin = A0;    // select the input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(tasterPin, INPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(8, OUTPUT);
  servoLeft.attach(10); // Set left servo to digital pin 10
  servoRight.attach(9); // Set right servo to digital pin 9
}

void loop() {
  if (digitalRead(tasterPin) == LOW) {
    digitalWrite(ledPin, HIGH);
    servoLeft.write(90);
  servoRight.write(90);
  } else{
    digitalWrite(ledPin, LOW);
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(250);              // wait for a second
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(12, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(250);              // wait for a second
    digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(8, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(250);              // wait for a second
    digitalWrite(8, LOW);    // turn the LED off by making the voltage LOW
    delay(250);              // wait for a second
    forward(); // Example: move forward
    delay(500); // Wait 2000 milliseconds (2 seconds)
    reverse();
    delay(500);
    turnRight();
    delay(500);
    turnLeft();
    delay(500);
    stopRobot();
    delay(500);
  }
}

// Motion routines for forward, reverse, turns, and stop
void forward() {
  servoLeft.write(0);
  servoRight.write(180);
}
void reverse() {
  servoLeft.write(180);
  servoRight.write(0);
}
void turnRight() {
  servoLeft.write(0);
  servoRight.write(180);
}
void turnLeft() {
  servoLeft.write(0);
  servoRight.write(180);
}
void stopRobot() {
  servoLeft.write(90);
  servoRight.write(90);
}

kindly regards

Don Dennis

You have only one if-else. What is wrong with it? What does it do that you think it should not do, or not do that you think it should do? :slight_smile:

yes it's right I've taken the second else out, because it didn't worked.

I just want to have 4 programs (4 case).

so the first (if) is my Standby program

the second ( else) is my main program

but now I need 2 more
for showing that one of the containers is empty nd stopping the program

kindly regards

Don Dennis

Dtensta:
yes it's right I've taken the second else out, because it didn't worked.

I just want to have 4 programs (4 case).

so the first (if) is my Standby program

the second ( else) is my main program

but now I need 2 more
for showing that one of the containers is empty nd stopping the program

kindly regards

Don Dennis

You still didn't answer the question. What didn't work? In other words, what happened?

We're not that interested in a sketch that works, simply because you don't need helpt with that :wink: Show us the sketch that did not work so we can advise on what is wrong and help you to solve the issue.