I'm having a little trouble with making a code for Arduino. I hope you can help me.
I'm trying to set a count down timer when LDR1 gets light. When LDR2 gets light when the timer is still counting down, a LED must go burning.
The problem is that I can't find a code for a timer witch you can set and reset.
I hope you guys can help me.
The millis() function can tell you when LDR1 triggered. It can tell you when LDR2 triggered. If the difference in times is less than some value, do something (like light the LED).
Creating a countdown timer has meaning only if you want to display the time. That's not listed as one of your requirements.
Loop is called in an infinite loop. You can test the current time, as reported by millis() against the time that LDR1 triggered, and do something if more than some amount of time has passed.
I don't know very much about arduino, but I can still search for timers. And writing one is a litlle to high for me as a beginner :P.
And of course I mean of I get it working :D.
I tried the code, but I can't get it working. Maybe something is wrong with my code.
unsigned long time;
int LDR1 = 2;
int LDR2 = 3;
int LED1 = 13;
void setup()
{
Serial.begin(9600);
pinMode (LED1, OUTPUT);
}
void loop()
{
Serial.print("Time: ");
time = millis(); //prints time since program started
Serial.println(time); //wait a second so as not to send massive amounts of data
delay(1000);
if (time <= 8333) //if time is less then 8333ms
{
digitalWrite(LED1, HIGH); //light LED1
delay(2000); //wait 2 seconds
digitalWrite(LED1,LOW); //turn off LED1
}
else
{
digitalWrite(LED1,LOW); // else turn off LED1, just do nothing
}
}
This will be true only in the first eight seconds or so after downloading your code.
You need to make your timing relative to the last time you read the timer.
And writing one is a litlle to high for me as a beginner
That's the great thing about the Arduino - someone else has already done it for you.
you need to have the time you are going to end
endtime = millis() + 8333;
then you need to see if you have reached that time
if(millis() > endTime)
The millis counter keeps on going so what you have will only work one time after that time will always be greater than 8333 no matter what.
unsigned long time;
int analogPin1 = 2; // LDR1 connected to analogpin 2
int analogPin2 = 3; // LDR2 connected to analogpin 3
int LED1 = 13; // LED connected to digitalpin 13
int val = 0; // variable to store the value read
int endTime;
void setup()
{
Serial.begin(9600);
pinMode (LED1, OUTPUT);
}
void loop()
{
Serial.print("Time: ");
time = millis(); // prints time since program started
Serial.println(time); // wait a second so as not to send massive amounts of data
delay(1000);
endTime = millis() + 8333;
analogRead(analogPin1);
analogRead(analogPin2);
if (millis() > endTime) // if time is less then 8333ms
{
digitalWrite(LED1, HIGH); // light LED1
delay(2000); // wait 2 seconds
digitalWrite(LED1,LOW); // turn off LED1
delay(5); // wait 0.005 seconds
}
else
{
digitalWrite(LED1,LOW); // else turn off LED1, just do nothing
}
}
every time through the loop so it will never time out.
If you want it to be a one time only then put this in the setup.
However all that will happen is the light will flash after 8.3 seconds and then nothing will happen ever again.
I think you need to define what you actually want to do.
It isn't easy resetting the timer's count to zero.
What is easy is setting your own "zero" by reading "millis" at the start of "loop", and basing your timings relative to this.