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);
}
}