if (motor == HIGH) { } :o

Hello all,

I'm trying to register the duration of an event.
This event will trigger more than 1 time and i need to sum up the duration .
I tried with the code bellow but for some reason my program does not go to the first and second IF statements.
Could someone help me out ?
Thank you in advance !

const int motor = 13; //pin control venti0lator
char  c,x;
int  cv=0,cv0 ,cv1  , cp  , cs , cc ;
void setup() { 
  pinMode(motor, OUTPUT);


void loop() {
  // put your main code here, to run repeatedly:
Serial.println  ( "sunt aici   ") ;
  digitalWrite(motor, HIGH);
      delay (4000);
    digitalWrite(motor, LOW );
  Serial.println  ( "acum aici ... am oprit otorul    ") ;
 if (motor == HIGH) { 
  cv0=millis() ;
   Serial.println ( "am pornit motorul ");
      while (motor  == LOW) { 
        cv=cv + (cv1-cv0);
        Serial.println ( "STOP STOP STOP  ") ;
        Serial.println  ( "a pornit la  ") ; Serial.print( cv0 ) ;
        Serial.println  ( "sm oprit la   ") ; Serial.print( cv1 ) ;
        Serial.println  ( "diferenta este  ") ; Serial.print( cv) ;

if (motor == HIGH) { 
  cv0=millis() ;

doesn't get run until after the motor has stopped!
You need to put the timing insie the control block, and analyse it when the motor has stopped.

Once you get the hang of it - try removing the delay() calls - then your whole project will become more responsive as your expand it.

p.s. you also have to READ the motor state - not the pin number

i'll try that
thank you!

It's nearly always impossible to mix timing using millis() and delay(). In your program if you are trying to time how long motor is on you already know the answer because you have the delay(4000) between on and off so it's 4 seconds per loop. If you want to know the cumulative time it's on for just count the number of times round the loop.

Either that or change ALL the timing over to using millis() which would be the normal advice but isn't really needed for such a simple program.

BTW you can't use while() like that either. There's nothing in the loop that can change the thing you're checking so if it ever gets in there it will never get out again.


motor is set as a constant 13. Therefore it will never be 'HIGH', only 13.

You'd be better to set motor_pin as 13 and then digital.read a variable like motor_value to see if pin 13 is high or low.

This splits the values of motor_pin == 13 and motor_value (which could be high or low).