Hello,
I am quite new to arduino and am having problems writing a sketch for a countdown that is started when a switch is flicked, and is restarted when the switch is flicked to off and then back on. I am not sure whether my description is of any help so here is the code:
int delayPeriodOff = 1000;
const int delayPeriodOn = 50;
const int ledPin = 12;
const int switchPin = 7;
int val= 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop() {
val = digitalRead(switchPin);
if (val == HIGH) {
countDown();
}
if (val == LOW) {
blinkLed();
}
}
void blinkLed () {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
void countDown () {
digitalWrite(ledPin, HIGH);
delay(delayPeriodOn);
digitalWrite(ledPin, LOW);
delay(delayPeriodOff);
delayPeriodOff = delayPeriodOff-20;
if (delayPeriodOff <= 0) {
ledOn ();
}
}
void ledOn () {
while(val == HIGH, delayPeriodOff <= 0);
digitalWrite (ledPin, HIGH);
delay (INFINITY);
}
As stated, I am new to arduino and programming in general and might have missed any number of obvious errors. I would appreciate any and all help.
thanks
Apower101