1

I want to loop one specific case inside of my switch, until the button gets pressed a second time. How can i make this work?

Code:

const int ledrood = 9;
const int ledgroen = 10;
const int ledblauw = 11;
const int buttonPreset = 2;
const int buttonMix = 4;
float sensorWaarde = 0;
int switchtel = 1;
int roodval;
int groenval;
int blauwval;
int buttonState = LOW;
void setup() {
Serial.begin(9600);
pinMode(ledrood, OUTPUT);
pinMode(ledgroen, OUTPUT);
pinMode(ledrood, OUTPUT);
pinMode(sensorWaarde, INPUT);
pinMode(buttonPreset, INPUT);
pinMode(buttonMix, INPUT);
}
void loop() {
int sensorWaarde = analogRead(A0); 
buttonState = digitalRead(buttonMix);
if (buttonState == HIGH) {
delay(200);
switch (switchtel) {
 case 1:
 roodval = 255;
 roodval = sensorWaarde / 4;
 groenval = 0;
 blauwval = 0;
 analogWrite( ledgroen, groenval );
 analogWrite( ledrood, roodval );
 analogWrite( ledblauw, blauwval);
 Serial.println(roodval);
 switchtel = switchtel + 1;
 break;
 case 2:
 roodval = 0; // cyaan
 groenval = 255;
 blauwval = 255;
 analogWrite( ledgroen, groenval );
 analogWrite( ledrood, roodval );
 analogWrite( ledblauw, blauwval);
 Serial.println("2");
 switchtel = switchtel + 1;
 break;
 case 3:
 roodval = 255; // magenta
 groenval = 0;
 blauwval = 255;
 analogWrite( ledgroen, groenval );
 analogWrite( ledrood, roodval );
 analogWrite( ledblauw, blauwval);
 Serial.println("3");
 switchtel = switchtel + 1;
 break;
 switch (switchtel) {
 }
 }
 }
}
asked Dec 1, 2017 at 13:56
1

2 Answers 2

1

You need to split your cases into multiple parts.

For example, you have a case which sets up a mode, and a case that runs a mode, and if needed a case that finishes a mode.

For example:

switch (mode) {
 case 0: // Idle
 break;
 case 1: // Start - do whatever to set up the mode here
 redValue = 100;
 greenvalue = 200;
 mode = 2; // Switch to the next case
 break;
 case 2: // Run - this is the bit that repeats
 if (digitalRead(3) == HIGH) {
 mode = 3; // Finish it
 }
 // Do whatever for each loop here.
 break;
 case 3: // It is finished.
 redValue = 0;
 greenValue = 0; 
 // etc.
 mode = 0; // Or whatever you want to do next
 break;
}

The key thing here is that case 1 is equivalent to a mini setup() and case 2 is equivalent to a mini loop(). You switch to case 1, which does the setup, and that case then switches to case 2, which it stays at until you tell it otherwise. That case will keep getting executed until you tell it to move to another case.

answered Dec 1, 2017 at 14:05
0

I want to loop one specific case inside of my switch, until the button gets pressed a second time. How can i make this work?

not sure why it needs to be anything more complicated than this:

 if (isPressed2x(btn)) do_something(); //if button is pressed twich, execute do_someting()

there are lots of ways to implement isPressed2x(). the simplest would be to use a static variable to keep track of it:

char isPressed2x(uint8_t btn) {
 static char cnt=0; //button press counter
 if (isPressed(btn)) { //button is pressed
 cnt+=1; //increment counter
 if ((cnt&0x01) == 0) return 1; //button has been pressed twich
 }
 return 0; //button not pressed, or not pressed twice
}

similarly, isPressed(btn) can be implemented, with or without debouncing.

the key is to break down any task into small, simple and well defined blocks so when re-assembled together, those small, simple and well defined blocks can perform tasks as complicated as you wish them to be.

the alternative is to jumbo all of them into one routine, making it hard to understand, hard to debug and hard to port.

answered Dec 1, 2017 at 16:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.