#include <Servo.h>
enum { // automatic numbering (enumeration)
S_IDLE, // 0
S_MAXA, // 1
S_MID, // 2
S_MAXB, // 3
S_WHAT, // 4 <== added, but not used in sequence right now
S_WAITING,// 5
S_FINISH // 6
};
Servo myservo;
int PIR_SensorPin = 8; //used a button in Wokwi for testing, easiest if low active, so added defn of ACTIVE next
int ACTIVE = LOW; //choose one, leave the other commented out
//int ACTIVE = HIGH; //your sensor is HIGH Active, so choose this one
unsigned long duration; //=0 by default
unsigned long lastMillis; //=0 by default
//int calibrationTime = 3; //irrelevant code at this time
int state = S_IDLE;
void setup()
{
Serial.begin(9600);
Serial.println("Hello");
pinMode (PIR_SensorPin, INPUT_PULLUP); // Set pinMode
// digitalWrite(PIR_SensorPin, LOW); //serves no purpose
// because of the way Servo.h works, you need to set the position before you enable the pin, but no one tells anyone this
myservo.writeMicroseconds(1500); // set servo to mid-point
myservo.attach(9, 900, 2100); // servo.attach(pin, min, max)
//the following is meaningless, code at this time
// Serial.print("calibrating sensor ");
// for (int i = 0; i < calibrationTime; i++) {
// Serial.print(".");
// delay(1000);
// }
Serial.println("S_IDLE"); //establish the starting state
}
void loop() {
if (millis() - lastMillis > duration) { // Step in sequence finished - time to take action!
lastMillis = millis();
switch (state) {
case S_MAXA:
Serial.println("S_MAXA");
myservo.writeMicroseconds(2100); // turn servo to new position
duration = 2000; // Duration of this position
state = S_MID; // Next step of the sequence - runs when duration has passed.
break;
case S_MID:
Serial.println("S_MID");
myservo.writeMicroseconds(2000); // turn servo to new position
duration = 2000; // Duration of this position
state = S_MAXB; // Move to the next state
break;
case S_MAXB:
Serial.println("S_MAXB");
myservo.writeMicroseconds(2100); // turn servo to new position
duration = 2000; // Duration of this position;
state = S_WAITING; // Move to the next state
break;
case S_WAITING:
Serial.println("S_WAITING");
myservo.writeMicroseconds(1500); // return servo to netural position
duration = 2000; // Wait for moving on.
state = S_FINISH; // Move to the next state
break;
case S_FINISH:
Serial.println("S_FINISH");
state = S_IDLE; // Done - time to look at the PIR sensor again.
duration = 0; // Wait for moving on.
break;
case S_IDLE:
if (digitalRead(PIR_SensorPin) == ACTIVE) { //start sequence if active
state = S_MAXA;
Serial.println(); //just ends line of ...
duration = 0; //move immediately to first state, no delay
lastMillis = millis();
}
else { //just cycling, so pump out another .
Serial.print(".");
delay(50);
}
break;
//the following is just a hangover from problem solving in thread
// case S_WHAT:
// Serial.println("S_WHAT");
// myservo.writeMicroseconds(1000); // WHAT position
// duration = 1000; // Duration of this position
// state = S_MAXB; // Move to the next state <== decide which is next state
// break;
}
}
}