Hi,
i have a poti which gives me values. If the value is over 700 a led should switch on. But after a specific time for example two secconds it should switch off. When the poti values go under 700 again and then above 700 again it should start over again (switch LED on and after 2 sec off again).
i have written a code but something is not working out how it should.
int poti = A0;
int potiwert = 0;
int LED = 3;
unsigned long startTime;
int timing = false;
unsigned long timeOut = 2000;
void setup()
{
pinMode(poti, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop()
{
potiwert = analogRead(poti);
//Serial.println(potiwert);
if(potiwert > 700)
{
digitalWrite(LED, HIGH);
Serial.println("EIN");
startTime = millis();
timing = true;
}
else
{
digitalWrite(LED, LOW);
Serial.println("AUS1");
}
if (timing == true)
{
if(millis() - startTime > timeOut)
{
digitalWrite(LED, LOW);
Serial.println("AUS2");
timing = false;
}
}
}
What something is that? How is it working out? How should it?
(I can tell from your code that you forgot the "When the poti values go under 700 again and then above 700 again" part. That is called "state change detection". See: File -> Examples -> 02.Digital -> StateChangeDetection)
first of all thanks for answering. So if the value goes above 700, the LED switches on and it stays on for longer than 2 sec. Basically forever. As soon as i turn the poti back under 700 it switches off, which is wanted. So the problem is that the LED stays on when above 700.
Have you looked at the StateChange example sketch?
I am not sure how to further explain what I said. I thought it was crystal clear. You do not need to track the timing condition with a variable. It is inherent in the time stamp value vs. the current time.