This is my codes:
const unsigned long sampleTime = 5000;
void setup()
{
pinMode(13, OUTPUT);
pinMode(3, INPUT);
Serial.begin(9600);
}
void loop() {
if ( digitalRead(3) == HIGH and millis() <= 5000 )
{
digitalWrite(13, HIGH);
}
if ( digitalRead(3) == HIGH and millis() > 5000)
{
digitalWrite(13, LOW);
}
if ( digitalRead(3) == LOW and millis() > 5000 )
{
digitalWrite(13, LOW);
}
if ( digitalRead(3) == HIGH and millis() > 5000);
{
millis() - sampleTime;
}
}
The problem here is, after the first if statement, the output stays low. Is there a way that I can reset the system after the second if statement? I tried to use the third and fourth if statement to restart everything but it seems its not working. Here is my original code:
void setup()
{
pinMode(13, OUTPUT);
pinMode(3, INPUT);
Serial.begin(9600);
}
void loop() {
if ( digitalRead(3) == HIGH and millis() <= 5000)
{
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
}
}
Okay, basics here. Do you realize that millis() will always be greater than 5000, following 5 seconds after boot time?
Just realized that. I am sorry I am a newbie here. So can I do the millis()-millis() in the fourth if statement?
Please follow the advice on posting a programming question given in Read this before posting a programming question
In particular note the advice to Auto format code in the IDE and to use code tags when posting code here
karlcorporal7:
Just realized that. I am sorry I am a newbie here. So can I do the millis()-millis() in the fourth if statement?
I don't see that anywhere. But this statement does a calculation and then throws the result in the garbage can:
millis() - sampleTime;
You seem to be completely lost and just "winging it". You should read the documentation and tutorials on millis(). I get that you're a newbie, but the processor doesn't care. 
if ( digitalRead(3) == HIGH and millis() > 5000);
{
millis() - sampleTime;
}
This is a common beginner mistake. You put a ';' at the end of the 'if' line so that is the only statement inside the 'if'. The 'if' can contain only one "statement". The ';' is a statement. The "block statement", surrounded by { and }, then isn't a part of the 'if'.
karlcorporal7:
Just realized that. I am sorry I am a newbie here. So can I do the millis()-millis() in the fourth if statement?
A bit more like this:
const unsigned long sampleTimeMs = 5000;
unsigned long mostRecentSampleMs;
void setup()
{
pinMode(13, OUTPUT);
pinMode(3, INPUT);
Serial.begin(9600);
// force an immediate first sample
mostRecentSampleMs = millis() - sampleTimeMs;
}
void loop() {
if ( digitalRead(3) == HIGH and millis()-mostRecentSampleMs <= sampleTimeMs )
{
digitalWrite(13, HIGH);
}
if ( digitalRead(3) == HIGH and millis()-mostRecentSampleMs > sampleTimeMs)
{
digitalWrite(13, LOW);
mostRecentSampleMs = millis();
}