Problems controlling continuous rotation servo with IR sensor

Hi All,

My goal is to have the continuous rotation servo rotate until the IR sensor reads a certain value (790), and have it stop for about 2 seconds. Essentially, rotate, stop for 2 sec, rotate, stop for 2 sec, etc. I am powering both the servo and IR sensor externally.

I want to avoid using the delay function since this will be part of a bigger project. Currently the continuous servo rotates until it reads the desired IR value then stops forever. Also the serial monitor stops printing the values thereafter. I believe it has to do with my timing using millis().

Here’s my code

#include <Servo.h>

Servo continuousservo;


int linesensor_pin = 0; //connected to analog 0
int linesensor_value;


void setup() {
 Serial.begin(9600);
continuousservo.attach(8, 1000, 2000);

}

void loop() {

  IR_Sensor();
  wheelservo();

 Serial.print("\tIR Sensor\t"); Serial.print(linesensor_value);

}
void IR_Sensor()
{

int linesensor_value = analogRead(linesensor_pin);
Serial.println(linesensor_value);
}
void wheelservo()
{
  unsigned long periodcontservo = 2000;
  static unsigned long lasttime = 0;
  int line = analogRead(linesensor_value);

  
 if (line > 790){
    unsigned long currentMillis = millis(); // begin to get current time
    while (currentMillis - lasttime < periodcontservo){
    
    continuousservo.writeMicroseconds(1480);
    }
      }
    
    else{
    continuousservo.writeMicroseconds(1430);
    }
}

 /*
    if (line > 780){
    continuousservo.writeMicroseconds(1480);
  }
 if (line < 780){
    continuousservo.writeMicroseconds(1430);
 }
}
*/

Any help would be appreciated!
Thanks!

Thanks for using code tags!

It’s generally better to put the complete program in a single code block rather than three separate ones since this would allow us to more easily copy and paste it into the IDE if we want to test your code.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.
CCing @232 on this:

232:

tplin:

    unsigned long currentMillis = millis(); // begin to get current time

while (currentMillis - lasttime < periodcontservo){
      continuousservo.writeMicroseconds(1480);
    }

You never update currentMillis in the while loop, and that’s why it gets stuck there forever.

Thanks for the heads up!

I updated currentMillis

 void wheelservo()
{
  unsigned long periodcontservo = 2000;
  static unsigned long lasttime = 0;
  int line = analogRead(linesensor_value);


  if (line > 790) {
    unsigned long currentMillis = millis(); // begin to get current time
    while (currentMillis - lasttime < periodcontservo) {
      continuousservo.writeMicroseconds(1480);
      lasttime = currentMillis;
    }
  }

  else {
    continuousservo.writeMicroseconds(1430);
  }
}

Now the continuous servo spins until desired value then spins slowly in the same direction without ever stopping.

I updated currentMillis

No. You "updated" lasttime. You need to call millis() again in the body of the while statement.

But, before you do that, all that the while loop is doing is waiting for some amount of time to pass, just like delay() does. You might as well use delay(), since you clearly don't understand that the point is to NOT just wait until some amount of time has passed.

If I use delay, then my IR sensor stops reading values which is what I want to avoid. I want to servo to spin until the sensor reads a value and then stop for 5 seconds then spin again.

Can you please explain where exactly I call millis() again?

Thanks!