Delay not working?

Hello everybody,

I am trying to make a simple sketch and use it on an arduino nano ATmega328P.

It is very simple: I have a servo and I need it to move twice. The first movement happens when you give power to the arduino, while the second one is after about 4 minutes and 33.5 seconds and it's triggered via a relè.

The problem is: while the first movement is always exactly right, the second one (the delayed one)fails 70% of the times: some times it's correct, some times it happens before the established time, some times it doesn't happen at all.

This is the code I'm using:

#include <Servo.h>
Servo myservo;

void setup() {
myservo.attach(6);
myservo.write(0);
pinMode(7,INPUT_PULLUP);
}

void loop() {
if (digitalRead(7)==LOW) {delay(200000);delay(73500)
;myservo.write(90);}

Can anybody help me? What am I doing wrong? I would prefer using delay and not millis

Thank You!

You need to effect the delay when the switch goes from HIGH to LOW, not when the switch is LOW.

Look at the change in state example in the IDE examples.

BTW, forget about using delay(), look at the BWD example (blink without delay).

would prefer using delay and not millis

I think you got that the wrong way round.

Please remember to use code tags when posting code

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components.
Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

The default integer type is 16bit signed, maximum value is 32767. Your delays exceed this value and you must explicitly tell the compiler what you want if the numbers are not to be truncated / wrapped / overflowed.

//No good:
delay(200000);delay(73500);

//Good, note the "unsigned long" suffix:
delay(273500UL);

EDIT: Prefix became suffix..

A small piece of advice. It would be easier to provide help if the code were Auto Formatted in the IDE and posted here using code tags like this

#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(6);
  myservo.write(0);
  pinMode(7, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(7) == LOW)
  {
    delay(200000);
    delay(73500);
    myservo.write(90);
  }

It would also help if the code did not have a missing } at the end of the loop() function

Danois90:
The default integer type is 16bit signed, maximum value is 32767. Your delays exceed this value and you must explicitly tell the compiler what you want if the numbers are not to be truncated / wrapped / overflowed.

//No good:

delay(200000);delay(73500);

//Good, note the "unsigned long" prefix:
delay(273500UL);

?

TheMemberFormerlyKnownAsAWOL:
?

Huh? Should delay have been decay?

Danois90:
Huh? Should delay have been decay?

No, all that stuff about suffixes.

Edit: I hadn't even noticed that @Danois90 described them as "prefixes", I was referring to the fact that they're simply not necessary - the compiler is perfectly capable of representing long numeric literals.

First of all, thank you to everyone for the support.

larryd:
You need to effect the delay when the switch goes from HIGH to LOW, not when the switch is LOW.

How can I apply this to my code? I am sorry but I’m kind of new to programming

There is both the ‘state change example’ and the BWD example that comes with the IDE.

Go through these two examples, then try to incorporate them in your sketch.

Show us your attempt then we can help out.

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components. Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

I am sorry but I am not very good at drawing schematics. I am trying to prepare something for you, I will show you as soon as possible.

I have a servo on pin 6 and a relay on pin 7.

I tried with millis, here is my trial (but unfortunately it does not work, the servo is now opening at the right time, but regardless of the relay). Am I on the right path?

#include <Servo.h>
Servo myservo;
unsigned long currentMillis = 0;
unsigned long buttonPressedMillis = 0;
long interval;

void setup() {
  myservo.attach(6);
  pinMode(7, INPUT_PULLUP);

}


void loop() {
  currentMillis = millis();
  if (digitalRead(7) == LOW) {
    buttonPressedMillis = currentMillis;
    interval=5000;
  }
  if ((currentMillis - buttonPressedMillis) > interval) {
    myservo.write(90);
  } else {
    myservo.write(0);
  }
}

Try this:

//Version 1.0

#include <Servo.h>
Servo myservo;

const byte mySwitch                     = 7;  //my switch, pushed = closed/LOW
const byte heartBeatLED                 = 13; //a LED that toggles every X seconds
const byte diagnosticLED                = 12; //a LED used to trace happenings  ;-)

byte lastMySwitchState;

boolean timingFlag                      = false;

unsigned long currentMillis;
unsigned long buttonPressedMillis;
unsigned long heartBeatMillis;
unsigned long switchMillis;

const unsigned long switchInterval      = 50;          //50 milliseconds 
const unsigned long heartBeatInterval   = 1 * 500ul;   //1/2 second
const unsigned long interval            = 5 * 1000ul;  //5 seconds

//unsigned long interval          = 4 * 60 * 1000 + 33500ul;  //4 minutes and 33.5 seconds

//************************************************************************************
void setup()
{
  myservo.attach(6);

  pinMode(heartBeatLED, OUTPUT);
  pinMode(diagnosticLED, OUTPUT);

  pinMode(mySwitch, INPUT_PULLUP);
  lastMySwitchState = digitalRead(mySwitch);  //LOW = pushed/closed

} //END of setup()


//************************************************************************************
void loop()
{
  currentMillis = millis();

  //****************************
  //to check for 'blocking code', toggle the heartBeat LED
  if (currentMillis - heartBeatMillis >= heartBeatInterval)
  {
    //restart the timer
    heartBeatMillis = currentMillis;

    //toggle LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));

  }

  //****************************
  //is timing enabled and has the timer timed out?
  if (timingFlag == true && currentMillis - buttonPressedMillis >= interval)
  {
    //disable timing
    timingFlag = false;

    myservo.write(90);

    //turn OFF the LED
    digitalWrite(diagnosticLED, LOW);
  }

  //****************************
  //time to check the switches?
  if (currentMillis - switchMillis >= switchInterval)
  {
    //restart the timer
    switchMillis = currentMillis;
    
    checkSwitches();
  }
  
  //****************************

} //END of loop()


//************************************************************************************
void checkSwitches()
{
  byte currentState;

  //get the current state of the switch
  currentState = digitalRead(mySwitch);

  //*****************************
  //was there a change in state on this switch?
  if (lastMySwitchState != currentState)
  {
    //update to the new state
    lastMySwitchState = currentState;

    //has the switch gone LOW?
    if (timingFlag == false && currentState == LOW)
    {
      //start the timer
      buttonPressedMillis = millis();

      //enable timing
      timingFlag = true;

      //turn the diagnostic LED ON
      digitalWrite(diagnosticLED, HIGH);

    }

    //the switch has gone HIGH
    else
    {
      //do nothing
    }

    //*****************************

  } //END of   if (timingFlag == false && currentState == LOW)

  //other switches if any

} //END of checkSwitches


//************************************************************************************
// END of sketch

but I am not very good at drawing schematics.

How did you wire up your components if you don’t understand electronics or schematics?
You’ll need to master one of those first.