Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Arduino Switch Case...
 
Share:
Notifications
Clear all

[Solved] Arduino Switch Case no break

1 Posts
1 Users
0 Reactions
677 Views
(@ledmeup)
Active Member
Joined: 3 years ago
Posts: 11
Topic starter  

Hi everybody!

I dearly need some help with this sketch!

I have a toggle switch on a MEGA 2560.

Each time the switch is pushed, a counter goes up by 1. Reachind "8", it is reset to "0".

This part just works fine.

BUT: When I add the Switch Case, it gets stuck in "case 1". The serial printer gives out "mainSwitchcounter1?" and "1". On and on.

And the LED does "on/off" forever. So the "break" does not work.

What am I missing?

Your help would be highly appreciated!!!

// for MEGA 2560
// Each time the mainSwitch is activated, the counter goes up by 1 and calls up a corresponding Switch Case section which lights up an LED
//Sketch gets stuck in case 1


int mainSwitch = 9; // Toggle Switch

int buttonState = 0;
int lastButtonState = 0;

const int ledPin1 =  2;      // the number of the LED pin which indicates the Switch Case section
const int ledPin2 =  3;
const int ledPin3 =  4;
const int ledPin4 =  5;
const int ledPin5 =  6;
const int ledPin6 =  7;
const int ledPin7 =  8;



void setup() {
 
  // initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
  pinMode(mainSwitch, INPUT);
  Serial.begin(9600);
}

void loop() {
  
  buttonState = digitalRead(mainSwitch);
  
// checking the mainSwitch
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      mainSwitchcounter++;
      Serial.println("mainSwitchcounter");
      Serial.println(mainSwitchcounter);
    }
  }
  delay(1000);

//   Switch Case with 7 sections. Each indicated by LED if triggered
switch(mainSwitchcounter) {
      case 1:                                // "break" doesn`t work. LED is switched on/off indefinitely. Serialprint says mainSwitchcounter1? and "1"
        digitalWrite(ledPin1, HIGH);
        Serial.println("mainSwitchcounter1?");
    Serial.println(mainSwitchcounter);
    delay(2000);
        digitalWrite(ledPin1,LOW);
        break;
      case 2:
        digitalWrite(ledPin2, HIGH);
        Serial.println("mainSwitchcounter2?");
    Serial.println(mainSwitchcounter);
        delay(2000);
        digitalWrite(ledPin2,LOW);
        break;
      case 3:
        digitalWrite(ledPin3, HIGH);
        Serial.println("mainSwitchcounter3?");
    Serial.println(mainSwitchcounter);
        delay(2000);
        digitalWrite(ledPin3,LOW);
        break;
      case 4:
        digitalWrite(ledPin4, HIGH);
        Serial.println("mainSwitchcounter4?");
    Serial.println(mainSwitchcounter);
        delay(2000);
        digitalWrite(ledPin4,LOW);
        break;
      case 5:
        digitalWrite(ledPin5, HIGH);
        Serial.println("mainSwitchcounter5?");
    Serial.println(mainSwitchcounter);
        delay(2000);
        digitalWrite(ledPin5,LOW);
        break;
        case 6:
        digitalWrite(ledPin6, HIGH);
        Serial.println("mainSwitchcounter6?");
    Serial.println(mainSwitchcounter);
        delay(2000);
        digitalWrite(ledPin6,LOW);
        break;
      case 7:
        digitalWrite(ledPin7, HIGH);
        Serial.println("mainSwitchcounter7?");
    Serial.println(mainSwitchcounter);
        delay(2000);
        digitalWrite(ledPin7,LOW);
        break;
}
// Resets the mainSwitchcounter to 0
if (mainSwitchcounter >= 8) {
  mainSwitchcounter = 0;
}
}

Thomas


   
ReplyQuote
Share: