Hi I have written a function that uses the if statement:
void walkmode(void) {
if (analogRead(1)>300 || analogRead(3)>300){
digitalWrite(6, HIGH); //blink LED when writing to EEPROM
delay(100);
digitalWrite(6,LOW);
readSensors();
parse();
writetoEEPROM();
delay(200);
}
if (index == 507){
digitalWrite(6, HIGH); //if the EEPROM is full then keep LED1 on.
}
}
The LED blinks a bit and then turns off. I want the LED to blink constantly while the soft sensor is being touched and do all of the other things listed at the same time.
When I used this code the check the sensor circuit- it works properly- it blinks while the sensor is touched with the conductive pom-pom and stops when the pom-pom is taken away.
here is that code:
[code]int led =6;
int sensor = 1;
void setup()
{ //start the serial communication
Serial.begin(9600);
pinMode(6, OUTPUT);
}
void loop()
{
Serial.println(analogRead(1)); //send the value of the analog input 2
delay(10); //delay to give the analog to digital converter time
if (analogRead(1)>350)
(digitalWrite(6, HIGH));
delay(100);
(digitalWrite(6,LOW));
}
[/code][/code]
Thanks I'm sure there is a simple solution- but I could use the help
The problem isn't with any if statement. It is with this statement:
delay(100);
This statement says do nothing but watch the clock until the specified time has elapsed.
If clock watching is important, by all means use the statement.
If you want to do other things, and only periodically check the clock, look at the blink without delay example to see how it can be done.
Basically, you set some flags, saying what needs to be done, and some times when that something was last done. On each pass through loop, you check to see if it's time to change anything.
When you turn the LED on, that is a discrete event. Note the time. On each pass through loop, see if the LED is on and it's time to turn the LED off. If so, that's a discrete event. Record the time.
If not, see if the LED is off and it's time to turn the LED on.
Unless walkmode is itself called on every pass through loop, all it should be doing is setting flags (theLEDNeedsToBlink = true;). The actual checking of millis(), etc. should be done in loop.