What i am trying to achive is:
press button
led on
press button again
led blinking
press button again
led blinking fast
press again
led off
i have been using the case statements.
What happens is that i press the button led goes on, then nothing changes until the led turns off in case 4.
Any help would be highly appreciated.
#include <avr/sleep.h>
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 1;
// the number of the LED pin
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
const long interval1 = 500;
// variables will change:
// variable for reading the pushbutton status
bool oldState = HIGH;
int showType = 0;
int Power __attribute__ ((section (".noinit")));
void PowerDown () {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
ADCSRA &= ~(1<<ADEN); // Turn off ADC to save power
sleep_enable();
sleep_cpu();
}
void setup() {
Power = !Power;
if (!Power) PowerDown();
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
bool newState = digitalRead(buttonPin);
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(buttonPin);
if (newState == LOW) {
showType++;
if (showType > 4)
showType=0;
startShow(showType);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
}
}
// Set the last button state to the old state.
oldState = newState;
}
void startShow(int i) {
switch(i){
case 0: digitalWrite (ledPin, HIGH);
break;
case 1: {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
;
break;
case 2:
{
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval1) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
}
;
break;
case 3: {
static float in = 4.712;
float out;
// do input, etc. here - as long as you don't pause, the LED will keep pulsing
in = in + 0.001;
if (in > 10.995)
in = 4.712;
out = sin(in) * 127.5 + 127.5;
analogWrite(ledPin,out);
}
;
break;
case 4: digitalWrite (ledPin, LOW);
break;
}
}