Bike light - switch case help!

Hi all,

I’m having trouble making this work. I want a button to be pressed which in turn cycles through different cases / different light sequences. I have each case all set up, however, I’m wondering how I would make for example - case 3 - loop within the case until the button is pressed again. Currently when it reaches case 3, it would only flash on and off then stop and not loop - how would I make this loop inside the switch case?

Please, someone, tell me where I’m going wrong?!

Regards,

Sam.

// variables will change:
int LDRValue = 0;
int BrightnessValue = 0;

int Count = 0;

// constants won't change
const int ledPin = 6;
const int confidenceLED = 3;
const int buttonPin = 4;


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  pinMode(confidenceLED, OUTPUT);

  Serial.begin(9600);


  //Confidence LED FLASH X 3  *CHECKED & WORKING*

  for (int x = 0; x < 3; x++) {
    digitalWrite (confidenceLED, HIGH);
    delay (500);
    digitalWrite(confidenceLED, LOW);
    delay (500);


  }
}

void loop() {


  switch (Count) // Define the Switch Cases

  { case 1: // High power LED constant *CHECKED & WORKING*
      Serial.println("\t case 1 matched");
      digitalWrite (ledPin, HIGH);
      break;

    case 2: // LED Brightness controlled by ambient light level *CHECKED & WORKING*
      Serial.println("\t case 2 matched");
      LDRValue = analogRead(A2);

      Serial.begin(9600);
      Serial.println(BrightnessValue);

      BrightnessValue = map(LDRValue, 22, 600, 0, 255);
      analogWrite(ledPin, BrightnessValue);
      delay(20);
      break;

    case 3: // LED on full but flashing at a visible rate
      Serial.println("\t case 3 matched");

      digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);              // wait for a second
      digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);              // wait for a second

      break;

    case 4: // CUSTOM FLASHING MODE
      Serial.println("\t case 4 matched");

      digitalWrite(ledPin, HIGH);
      delay(300);
      digitalWrite(ledPin, LOW);
      delay(300);
      break;
  }

  while (digitalRead(buttonPin) == HIGH);
  while (digitalRead(buttonPin) == LOW);

  {

    Count = Count + 1;
    if (Count > 4) Count = 0;
    delay(200);


  }
}

See the state change detection example in the IDE (File, Examples, Digital). It shows how to increment the button push count for each transition. Hope this will help if I understood the question.

Please, someone, tell me where I'm going wrong?!

 while (digitalRead(buttonPin) == HIGH);

With INPUT_PULLUP on the buttonPin, the code is waiting at this statement for another press, and you are not cycling through loop() to re-enter the switch cases .

As groundFungus suggest, look at the ide state change example for how to read a button press.

Hi,

Thanks for your replies, I have the button cycling through each switch case which works all fine. However, I want the cases to loop inside each case function while it waits for the button press.

[/case 3: // LED on full but flashing at a visible rate Serial.println("\t case 3 matched");

digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second

break; ]

e.g when this case is matched it simply turns on and then off when I want it to continuously turn on and off.....and not just once....

Cheers.

cattledog: while (digitalRead(buttonPin) == HIGH);

With INPUT_PULLUP on the buttonPin, the code is waiting at this statement for another press, and you are not cycling through loop() to re-enter the switch cases .

As groundFungus suggest, look at the ide state change example for how to read a button press.

However, I want the cases to loop inside each case function while it waits for the button press.

You can not use the while() statement for reading the buttons. It will prevent the code from cycling through the loop and repeatng the selected case. https://www.arduino.cc/en/Reference/While

If the button is not pressed again, how do you think the code can ever leave this bloc?

while (digitalRead(buttonPin) == HIGH);

Where did you see that this was a correct way to read a button?

while (digitalRead(buttonPin) == HIGH);
  while (digitalRead(buttonPin) == LOW);

In the state change example did you see that the buttonPushCounter variable only changes on a button press? If you use that variable for the switch statement, it will keep the state (count) til you push the button again. Get rid of the count variable and use buttonPushCounter. Reaad the switch every time through loop() and execute the case based on buttonPushCount. If the buttonPushCount does not change it will execute the same case over and over.