IF statmenet for a certain time

That would be a little bit funny, because pin 0 is also used for serial communications. I strongly suggest consulting the documentation of pin functions before doing any wiring. It's what I always do.

1 Like

I changed the analog input to pin 2 and still same before , no change at injecting current more set value or less set value
Any other ideas please

Pin 2 has no analog capability. Perhaps the library is mapping analog pins to digital pins. Try using an analog pin, like A1.

emon1.current(A1, 48);  

It shouldn't make a difference but it's worth a try.

I checked the library and the pin numbers are passed to analogRead() so 0 is the same as A0 and 1 is the same as A1...

1 Like

Sorry for the distraction. The "output device" is apparently the on board LED. Was it asked or mentioned how it is behaving?

Yes that's right
Any idea to find why there is no response from the digital output

I found your mistake:

warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
   if (currentMillis - LastTimeAboveLowerLimit >= 2000);
                                                       ^

See the ';' you added? The one the warning message is pointing to? That is why your LED is always off.

1 Like

Thank you @johnwasser for your help
I will check and back to you with the results

I have a querry when you use currentmillis=millis;

Then when current millis value remains the value of millis.....when the program returns to this line.....that is current millis will update when ...the programme comes back to this line.....

I usually put "unsigned long currentMillis = millis();" near the top of loop() and then use 'currentMillis' instead of 'millis()' in the rest of loop(). The value returned by 'millis()' can be different each time it is called. Using 'currentMillis' saves time if a value for 'millis()' is needed more than once. It will also avoid losing time. In this example, the second call to millis() might produce a different answer.

   if (millis() - previousMillis >= 2000 )
   {
    previousMillis = millis();
   }

Correct, a good thing to keep in mind. Also some people like to do this to avoid cumulative errors:

if (millis() - previousMillis >= 2000 )
   {
    previousMillis += 2000;
   }

Actually, when combined with the advice above, it's:

tNow = millis();
if (tNow - previousMillis >= 2000 )
   {
    previousMillis += 2000;
   }
... // other uses of tNow...

thankyou very much....i have one more querry....if you can suggest......it is the correct way to write code ....i am going to use steeper motor to open sliding door which ha 4limit switch to indicate position..first and last begin full open and full close.......

void loop() {

     delay(10);
             
 buttonState1 = digitalRead(Limithighhigh);
           
   buttonState2 = digitalRead(Limithigh);
           
   buttonState3 = digitalRead(LimitLow);
            
  buttonState4 = digitalRead(LimitLowLow);

unsigned long currentMillis = millis();
if (APDS.gestureAvailable()) {
previousMillis==currentMillis;

              // a gesture was detected, read and print to serial monitor
                 int gesture = APDS.readGesture();

             
                 if( gesture==GESTURE_UP || gesture==GESTURE_DOWN || gesture==GESTURE_LEFT ||gesture==GESTURE_RIGHT){                                            

if ((currentMillis-previousMillis<=interval)){
if(buttonState1==LOW && buttonState2==LOW && buttonState3==LOW && buttonState4==LOW ){

                                                      digitalWrite(dirPin, HIGH);
                         
                         for(int i=0;i<10000;i++){

unsigned long currentMillis = millis();

buttonState1 = digitalRead(Limithighhigh);
                                               
   buttonState2 = digitalRead(Limithigh);
                                                 
 buttonState3 = digitalRead(LimitLow);
                                                  
buttonState4 = digitalRead(LimitLowLow);
                                                               if(buttonState1==LOW && buttonState2==LOW && buttonState3==LOW && buttonState4==LOW){
                                                               digitalWrite(stepPin, HIGH);
                                                               delayMicroseconds(1000);
                                                                digitalWrite(stepPin, LOW);
                                                              delayMicroseconds(1000);
                                                                   }
 
                                                     
       else if (buttonState1==HIGH && buttonState2==LOW && buttonState3==LOW && buttonState4==LOW){
                                                               digitalWrite(stepPin, HIGH);
                                                              delayMicroseconds(1000);
                                                              digitalWrite(stepPin, LOW);
                                                              delayMicroseconds(1000);
                                                                    }
                                                     
   else if (buttonState1==HIGH && buttonState2==HIGH && buttonState3==LOW && buttonState4==LOW){
               
                                                               digitalWrite(stepPin, HIGH);
                                                               delayMicroseconds(1000);
                                                               digitalWrite(stepPin, LOW);
                                                               delayMicroseconds(1000);
                                                                       }
                                                       
    else if (buttonState1==HIGH && buttonState2==HIGH && buttonState3==HIGH && buttonState4==LOW){
               
                                                              digitalWrite(stepPin, HIGH);
                                                              delayMicroseconds(1000);
                                                              digitalWrite(stepPin, LOW);
                                                              delayMicroseconds(1000);
                                                                       }

else if (buttonState1==HIGH && buttonState2==HIGH && buttonState3==HIGH && buttonState4==HIGH){

break;
                                                                       }
                                                             }                           
                                                  }
                                                 
      // Here i am starting the for loop when door is open ....but after entering for loop slowly all elseif will be used as when the stepper will move limit switches condition will keep on changing...........when all are open.....so...stop the stepper.....after this same type of loop i am starting with all button in high condition(when the door is full open)...to close the   door....at that time i want to check if the time after gesture....was detected...is more than few seconds....than do not start any loop..... so in this case i think it is better to use millis instead for current millis....

I don't think this is going to do what you want. It does nothing unless there is a gesture available. It looks like the door will only go down and only if it was fully up and only if it can do it in 1000 steps.

What is it you want the gestures to do?

What is the timer for?

Hi thanks..for...sharing your valuable knowledge.....

Planning to instal a gesture sensor so that the door starts opening/closing when the gesture..sensor detects a gesture..........
.....using 1000 just an arbitrary number.....not intend to stop the stepper when it reaches 1000 ...intent is to break out of for loop ...when all the limit switches detect....HIGH or LOW.. ..the code attached is just a part of code ...as the code will have similar steps for different position of doors.....say in this code i am starting to move ...only when all four limit switch are open....so i am starting the for loop only when all four limit switches is open.... .but practically as the door moves 1 will close and 3will open...then stepper will operate with diff.....speed .....then finally all four switches will close that is door is full open..then i have given command....to break out of for loop to stop the stepper immediately as it is very imp. to prevent mech. damage.......
Now ....not mentioned in the code ....i have also added a condition to close the door when all switches are close.....as also required to close the door when the gesture is detected where door is full open and finally it closes and i break out of loop in a similar way.....but problem is that ..initially a gesture is detected then this for loop starts when conditions satisfied to close the door ..and as per it...it closes the door....after that the code is for from full close to full open....but ....since the gesture was detected at the start.....and after opening it starts executing closing operation.......so i have put a timer that if the time has elapsed since gesture is detected.. then no closing or no opening of door....that means for me that the door function was performed.......so preventing reverse operation.....immediately......for this i need to have system time .....as if i declare currentmillis =millis at the top and.....for loop may take say..2minutes.....than the value of current millis will remain same for 2minutes....so will not help me...with this thing.....also i am not sure.....that particular block will work or not.....
From initial if condition of limit switches to finally breaking out of loop......

It sounds like what you want is:

If the door is fully down and you see an 'UP' gesture:
  Start lifting the door at high speed.
  When the door is almost fully up, switch to a slower speed.
  When the door is fully up, stop.

If the door is fully up and you see a 'DOWN' gesture:
  Start dropping the door at high speed.
  When the door is almost fully down, switch to a slower speed.
  When the door is fully down, stop.

Is that what you want to do?

Yes thats correct.....exactly what i want.

So it's just a matter of translating that into code.

boolean DoorIsUp()
{
  return digitalRead(Limithighhigh) == HIGH);
}

boolean DoorIsNearTop()
{
  return digitalRead(Limithigh) == HIGH);
}

boolean DoorIsNearBottom()
{
  return digitalRead(Limitlow) == HIGH);
}

boolean DoorIsDown()
{
  return digitalRead(Limitlowlow) == HIGH);
}

void loop()
{
  if (APDS.gestureAvailable())
  {
    // a gesture was detected, read
    int gesture = APDS.readGesture();

    // If the door is fully down and you see an 'UP' gesture:
    if (gesture == GESTURE_UP && DoorIsDown())
    {
      // Start lifting the door at high speed.
      digitalWrite(dirPin, HIGH);
      unsigned stepDelay = 5;

      // When the door is fully up, stop.
      while (!DoorIsUp())
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(stepDelay);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(stepDelay);

        // When the door is almost fully up, switch to a slower speed.
        if (DoorIsNearTop())
          stepDelay = 100;
      }
    }


    // If the door is fully up and you see a 'DOWN' gesture:
    if (gesture == GESTURE_DOWN && DoorIsUp())
    {
      // Start dropping the door at high speed.
      digitalWrite(dirPin, LOW);
      unsigned stepDelay = 5;

      // When the door is fully down, stop.
      while (!DoorIsDown())
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(stepDelay);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(stepDelay);

        // When the door is almost fully down, switch to a slower speed.
        if (DoorIsNearBottom())
          stepDelay = 100;
      }
    }
  }
}

Thank you very much.....will use as base for the code......Thx..

I am an begineer in programing.....so one query in mind.....all the return functions declared out of loop.......and now we use...
while (!DoorIsDown())
Will the condition check if the door is down will be performed....at each step of while loop.....also....

boolean DoorIsUp()
{
return digitalRead(Limithighhigh) == HIGH);
}
When we use this.....can we use
return digitalRead(Limithighhigh) == HIGH)&&digitalRead(Limithigh==HIGH)
// Just a query to ensure failure of one limit switch might not cause....false movement.....//required for mechanical safety//

Any way :blush: thanks..

Yes. The function will be called every time and will check the state of the input pin every time.

You can do that but it will cause a big problem! If the Limithigh pin fails to go HIGH you not only won't slow down when going up but also won't stop when you reach the top! The '&&' (AND) requires both to be true.