I'm using a ATMega328P-PU on a breadboard as an arduino. How do you start a code after say 20 seconds have elapsed since startup? Example code:-
if(condition)
{
//Start sending morse code!
}
My current code uses millis()
and fails miserably. The speaker does not get activated. I'm using the standard circuit mentioned at the Arduino website
My current code:-
time = millis();
if(time>=20000)
txEnable=true;
val = analogRead(analogPin);
if(val!=0)
{
standby=false;
}
else
standby=true;
if(txEnable==true && standby==true)
{
if(!callsignSender->continueSending())
{
callsignSender->startSending();
}
}
1 Answer 1
The following bare-minimum code will switch pin 13 to high after 20 seconds:
void setup() {
// assuming pin 13 has a LED connected (as on most Arduino boards)
pinMode(13, OUTPUT);
digitalWrite(13, LOW); // would also work without this line
}
void loop() {
unsigned long time = millis();
if (time >= 20000) {
digitalWrite(13, HIGH); // set the LED on
}
}
Try if this works for you, i.e. if a LED connected to that pin will light up 20 seconds after reboot.
If not you will need to find the problem: Is your ATmega running at the correct speed? Is your code executed at all? Are the peripherals working?
-
millis()
returns time in unsigned long format. Above code needs to be adjusted. (Otherwise its bound to create trouble) I'll try your code though. Pls try searching for alternatives!Parth Sane– Parth Sane03/21/2015 18:29:59Commented Mar 21, 2015 at 18:29 -
Well, it would work for what it's written. But I've changed it to
ulong
.fuenfundachtzig– fuenfundachtzig03/21/2015 18:49:21Commented Mar 21, 2015 at 18:49 -
Note that I don't suggest that you actually use code written like that for any other purpose than testing...fuenfundachtzig– fuenfundachtzig03/21/2015 18:53:43Commented Mar 21, 2015 at 18:53
digitalRead()
? If you really need an analog value, instead of comparing with zero you should compare to some low threshold.