I have written a program similar to blink without delay tutorial but I am having a hard time getting it to do what I want it to do. I would like it to simply receive an input from the serial monitor and if it is "1" turn on the pin 13 LED for 1 minute. After the 1 minute cycle I would like it to turn the LED off and wait to receive another command. I must have missed something because the LED just won't shut off.
What am I missing, why won't it shut off when the "diff" is greater than 1 minute?
Thank you for your help
Jake
PS all the serial print stuff what an attempt of me figuring out what is going wrong. It didn't work.
//Created by Jacob
int Led = 13;
unsigned long previousMillis = 0;
void setup() {
pinMode(Led, OUTPUT);
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0); //wait for an inputed value.
int val = Serial.read()-'0';
Serial.print(val); //just to make sure send it back to me in the serial monitor
Serial.println(" inputed value");
unsigned long currentMillis = millis(); //my running timer I think
Serial.print(currentMillis); //tell me what it's current value is
Serial.println(" currentMillis");
Serial.print(previousMillis); //tell me what previousMillis currently is
Serial.println(" previousMillis");
delay(1000);
if (val == 1){ //Okay if the value inputed happens to be one lets do something
digitalWrite(Led, HIGH); //
unsigned long milsval=val*60.*1000.; //convert value to minutes
Serial.print(milsval);
Serial.println(" inputed value in milsec");
unsigned long diff = currentMillis - previousMillis; //the prupose of these serial.prints is for me to keep track of when it should enter the next if statement
Serial.print(diff);
Serial.println(" difference");
delay(1000); //so as to not recieve too much data
if (currentMillis - previousMillis > milsval) {
digitalWrite(Led, LOW); //once the time interval has passed shut off the LED
previousMillis = currentMillis; //set them equal to the whole time difference aplicable
val = 0; //set the value to 0 to get me out of this whole if statement started on line 25
}
}
}