Help with delays

I have a project which is attempting to run a couple of things in a sequence.

I will post the code and then explain:

#include <Servo.h> 

//Create objects for Servos
Servo myservo;
Servo myservo2;

// Set PIN names
int elPin = 13;
int buttonLedPin = 12;
int ledPin = 11;
int sirenPin = 8;
int buttonPin = 2; 

// Create other variables
long previousMillis = 0;
int buttonState = 0;

void setup(){
  // Setup PINs
  pinMode(elPin, OUTPUT);
  pinMode(buttonLedPin, OUTPUT);  
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);  
  // Setup servos
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object 
  myservo2.attach(4);  // attaches the servo on pin 9 to the servo object
  //Start serial
  Serial.begin(9600);           // set up Serial library at 9600 bps
  // Set servos to 0 degrees
  // Set Button LED on
  digitalWrite(buttonLedPin, HIGH); 
                                      Serial.println("Button LED On");

void loop(){
  digitalWrite(ledPin, HIGH);
  Serial.println("Waiting for button Press");
  buttonState = digitalRead(buttonPin);
  unsigned long currentMillis = millis();
  if (buttonState == HIGH) {
                                      Serial.println("Button Pressed");
    digitalWrite(buttonLedPin, LOW);
                                      Serial.println("Button LED Off");
                                      Serial.println("Running first delay");
                                      Serial.println("Running Fire LEDs");
    digitalWrite(ledPin, LOW);
                                      Serial.println("Running second delay");
                                      Serial.println("EL Wire On");
    digitalWrite(elPin, HIGH);   // set the EL Wire on
                                      Serial.println("Servos at 90");
                                      Serial.println("Initiating siren");
    for (int i=0; i <= 65; i++){
                                      Serial.println("Running Siren");
      tone(sirenPin, 3000, 120);
    digitalWrite(elPin, LOW);   // set the EL Wire off
    digitalWrite(buttonLedPin, HIGH);
                                      Serial.println("Finished resetting");

There is a button with an LED that lights up intitially.
After this button is pressed, the button LED turns off and starts the following sequence.
1500ms delay
ledPin is set to low (it runs some flickering LEDs through a transistor and for some reason setting it to low makes it work - so yes, set it to low.)
1500ms delay
elPin is set to high (it uses a 5v relay to run some el wire)
myservo and myservo2 are both written to 90
then for approx 15000ms the a tone is intermittantly generated on Siren Pin - on for 120ms, off for 230ms.

Once all of this is done, everything resets to how it was at the beginning.

My problem with this code is, the delays dont work - everything just comes on at the same time!

Please help me!


First before I try and follow your code, do you have a external pull-down resistor wired to the button switch? It's required.


I can see nothing really wrong with the code, so it's probably the resistor retrolefty mentioned.

You are saving the millis() value for no reason, I assume this is a start at a version that doesn't use delay().

Also you aren't debouncing the button, but in this program that won't matter.

RE the LED, if you control through a transistor in the normal way that will invert the signal, that is how things work. But for a single LED you don't need a transistor, just a current-limiting resistor.


Thanks for the replies.

I do indeed have a pull down resistor on the button - could the resistance of it be affecting the delays?

The whole code does work, just the delays are the issue - its as if they are just skipped in places.

Also for the LEDs - I am using 2 LEDs on a separate microcontroller and using a transistor to turn the microcontroller on and off...


could the resistance of it be affecting the delays?


I am using 2 LEDs on a separate microcontroller and using a transistor to turn the microcontroller on and off...

Whoa, that's a bit out of left field. Does this have anything to do with the above sketch? Is that sketch running on the processor that's controlled by the transistor?


It doesn't seem likely but what happens when you comment-out or shorten the debug prints?

I know its a little crazy but it was a quick and easy way to get it done... no the sketch running on the 2nd microcontroller is just a simple 2 LED flicker

I am currently testing if the debug messages have anything to do with the code.


EDIT: removing debug messages does not help.

So I have found that the issue was that I was sending the LED pin low in order to make it run.

When tested with just a single LED, and having switched around the lows and highs, the program runs properly.

Now, how can I invert the signal going to the transistor - ie. I can send a 'high' signal and the installed transistor will allow current to pass through it?

I suppose this is a component question now.

Thanks for the help


I was sending the LED pin low in order to make it run.

If you are controlling a LED with a transistor in the normal fashion (ie a low-side switch with the transistor on the LED cathode) then a HIGH will turn the LED on, your LOW will turn it off so this should have manifested itself as an inversion, ie the LED was on when you thought it should be off and vv.

Sorry but I’ve totally lost track of what’s running what.

I still can’t figure out why two Arduinos are in play and what their relationship is. Schematics were designed because it’s too confusing to describe a circuit with words, How about a schematic.