Servo issues

Hi everyone,

I am quite new into the world of C++ and there is something I can't get right.

I am building a 'machine' where a LEGO brick falls on a disc, there is a photoresistor reading the light value, and as soon as the brick falls down the photoresistor can read the change in light. In order to work like it should, the servo should now turn 90° to the left, and return. However in my code it doesn't. It returns straight away, I guess that is because the photoresistor gets a higher value again. What I'd like to see happening is that the servo turns the full 90° before returning. Does that make sense to any of you? :slight_smile:
Down here is the code that matters:

if(sensorVal > 7){ // Values of the sensor are mapped between 0 and 10
      servo.write(servoValEmpty);  //Turn 90° left
      delay(100);                  //Tme to actually turn
      servo.write(servoValFull);   //Return to first position
      servo.write(servoValFull);  //Always be in firstpostition (startposition)

servoValEmpty equals 157°
servoValFull equals 66°

All help gets very appreciated! Also a term so I maybe can google it? Because I do not really know how to describe the situation.

Thank you very much,

You need to program a state machine - the change in light level triggers a state change,
the servo sweep completing will trigger another change of state.

You'll need to sweep the servo as in the example, in order to know when it completes.

Similar to what @MarkT has said ...

Use the sensor to set the state of a variable - e.g. servoMove = true;

Use the value of that variable to decide whether to move the servo.

Don't use the sensor to control the servo directly.

I will leave you to figure out when you need servoMove = false;


Okay, thanks! Now I might even get my machine running!