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.
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);
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);
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.
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?
//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