I'm a noob when it comes to time-based coding.
I have a pressure sensor which shows me readings for when someone is sitting on a chair.
I want to turn a motor for 2 seconds when the pressure sensor says occupied I want to turn the motor the other way for 2 seconds when the pressure sensor says empty.
Also after the motor has turned for 2 seconds, I don't want it to turn the motor at all until the reading of the pressure has changed to the other reading.
Here is my code.
#include <AFMotor.h>
AF_DCMotor motor(1,MOTOR12_64KHZ);
AF_DCMotor motor2(2,MOTOR12_64KHZ);
int fsrPin = 0; //FSR and 10K resistor are connected to pin 0
int fsrReading; //the analog reading from the resistor divider
bool WheelLock = true; //controls whether the wheelchair is locked or not
long startTime = millis();
long stopTime = startTime;
void setup(void){
Serial.begin(9600);
motor.setSpeed(255);
startTime = 0;
stopTime = 5000;
}
void loop(){
while(startTime < stopTime){
if(onChair() == false){
motor.run(BACKWARD);
startTime = millis();
}
else{
motor.run(FORWARD);
startTime = millis();
}
}
motor.run(RELEASE);
Serial.println("RELEASED");
startTime = 0;
Serial.println(startTime);
}
bool onChair(){
fsrReading = analogRead(fsrPin);
if(fsrReading > 800){
Serial.println("Wheelchair is occupied");
WheelLock = false;
}
else{
Serial.println("Wheelchair is empty");
WheelLock = true;
}
delay(200);
return WheelLock;
}