i'm not sure what you're expecting to see -- the code prints msec and msecLst immediately after msecLst is set to msec.
when moving east, you can simply start the motor and stop it after a delay. a flag is needed to only do this once east nightfall
but by capturing msec in msecLst, all times after moving east can be measured relative to msecLst if moving east only occur once
it's difficult to say more without seeing more of the code
please examine the following output and code. do i understand what you are trying to do correctly?
msec 0, msecLst 0, rest 100, ouest 100, night 0, moveEast 0
msec 1000, msecLst 0, rest 100, ouest 100, night 0, moveEast 0
msec 2001, msecLst 0, rest 100, ouest 100, night 0, moveEast 0
msec 3002, msecLst 0, rest 100, ouest 100, night 0, moveEast 0
msec 4003, msecLst 0, rest 100, ouest 100, night 0, moveEast 0
msec 5004, msecLst 0, rest 100, ouest 100, night 0, moveEast 0
msec 6005, msecLst 0, rest 100, ouest 0, night 0, moveEast 0
msec 7006, msecLst 0, rest 100, ouest 0, night 0, moveEast 0
night:
moveEast:
msec 8007, msecLst 8007, rest 0, ouest 0, night 1, moveEast 1
moveEast:
msec 9027, msecLst 8007, rest 0, ouest 0, night 1, moveEast 1
day:
moveEast:
msec 10038, msecLst 8007, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 11061, msecLst 8007, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 12076, msecLst 8007, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 13091, msecLst 8007, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 14107, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 15123, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 16126, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 17129, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 18132, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 19136, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 20139, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 21142, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 22146, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 23148, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 24152, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 25155, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
moveWest: 18151
msec 26158, msecLst 8007, rest 0, ouest 100, night 0, moveEast 0
msec 27177, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 28181, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
moveWest: 21177
msec 29184, msecLst 8007, rest 0, ouest 100, night 0, moveEast 0
msec 30203, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 31207, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
moveWest: 24203
msec 32210, msecLst 8007, rest 0, ouest 100, night 0, moveEast 0
msec 33230, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 34233, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
moveWest: 27229
msec 35236, msecLst 8007, rest 0, ouest 100, night 0, moveEast 0
msec 36256, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 37259, msecLst 8007, rest 100, ouest 100, night 0, moveEast 0
msec 38262, msecLst 8007, rest 100, ouest 0, night 0, moveEast 0
night:
moveEast:
msec 39264, msecLst 39264, rest 0, ouest 0, night 1, moveEast 1
day:
moveEast:
msec 40286, msecLst 39264, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 41310, msecLst 39264, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 42327, msecLst 39264, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 43342, msecLst 39264, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 44359, msecLst 39264, rest 100, ouest 100, night 0, moveEast 1
moveEast:
msec 45376, msecLst 39264, rest 100, ouest 100, night 0, moveEast 0
msec 46393, msecLst 39264, rest 100, ouest 100, night 0, moveEast 0
msec 47397, msecLst 39264, rest 100, ouest 100, night 0, moveEast 0
msec 48401, msecLst 39264, rest 100, ouest 100, night 0, moveEast 0
msec 49405, msecLst 39264, rest 100, ouest 100, night 0, moveEast 0
unsigned long msec;
unsigned long msecLst;
unsigned long interval = 6000;
bool moveEast;
bool night;
int capteurest;
int capteurouest;
int capteurnord;
const byte PinFakeInpE = A1;
const byte PinFakeInpW = A2;
const byte dirPin2 = 12;
const byte freinPin2 = 11;
const byte vitessePin2 = 10;
// -----------------------------------------------------------------------------
void
loop (void)
{
msec = millis ();
capteurest = digitalRead (PinFakeInpE) ? 100 : 0;
capteurouest = capteurnord = digitalRead (PinFakeInpW) ? 100 : 0;
// -------------------------------------
if (! night && capteurest<10 && capteurouest<10 && capteurnord<10)
{
Serial.println (" night:");
msecLst = msec;
night = moveEast = true;
}
else if (night && capteurest > 20) {
Serial.println (" day:");
night = false;
}
// -------------------------------------
if (moveEast) {
Serial.println (" moveEast:");
if(((msec - msecLst) >= interval))
{
digitalWrite (dirPin2, LOW);
digitalWrite (freinPin2, LOW);
analogWrite (vitessePin2, 5); //Speed of the motor
moveEast = false;
}
}
else if (capteurest < capteurouest) {
Serial.print (" moveWest: ");
Serial.print (msec - msecLst);
Serial.println ();
digitalWrite (dirPin2, HIGH);
digitalWrite (freinPin2, HIGH);
analogWrite (vitessePin2, 200);
}
else {
analogWrite (vitessePin2, 0);
}
// -------------------------------------
Serial.print ("msec ");
Serial.print (msec);
Serial.print (", msecLst ");
Serial.print (msecLst);
Serial.print (", rest ");
Serial.print (capteurest);
Serial.print (", ouest ");
Serial.print (capteurouest);
Serial.print (", night ");
Serial.print (night);
Serial.print (", moveEast ");
Serial.print (moveEast);
Serial.println ();
delay (1000);
}
void setup (void)
{
Serial.begin (9600);
pinMode (PinFakeInpE, INPUT_PULLUP);
pinMode (PinFakeInpW, INPUT_PULLUP);
}