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:-

     //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();
      val = analogRead(analogPin);

  if(txEnable==true && standby==true)


  • 1
    You are comparing an analog reading with zero. Are you sure you don't mean digitalRead()? If you really need an analog value, instead of comparing with zero you should compare to some low threshold. Mar 21, 2015 at 18:43
  • I'm basically writing this code for a ham wireless repeater. Its in Pune.(VU2PHA). The analog read value is for detecting any incoming signal for repeating. Its a small scale model for the same big repeater. The repeater custodian is my friend and I'm making it for him.
    – Parth Sane
    Mar 21, 2015 at 18:48
  • The define a "noise threshold" and treat any value below the threshold as equivalent to zero. Mar 21, 2015 at 18:57
  • @Edgar Bonet I'm going to fine tune that once I get in touch with VU2ASH (one of the custodian). I'm making a protosketch.
    – Parth Sane
    Mar 21, 2015 at 18:59
  • I have to use the following protocol. docs.google.com/document/d/…
    – Parth Sane
    Mar 21, 2015 at 19:02

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
    Mar 21, 2015 at 18:29
  • Well, it would work for what it's written. But I've changed it to ulong. 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... Mar 21, 2015 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.