0

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;
}
0

I think you want something like this:

int motorRunSecs = 2
bool chairLastState = false;
bool chairCurrState = false;
int motorDirection = FORWARD;

void loop() {
    chairCurrState = onChair();

    // if the chair occupied state has changed, then do something
    if (chairCurrState != chairOccupied) {

        // set the motor Direction based on the state of the chair
        motorDirection = ((chairCurrState) ? FORWARDS : BACKWARDS);

        // run motor for X secs
        motor.run(motorDirection);
        delay(motorRunSecs * 1000);
        motor.run(RELEASE);

        // now update the last chair state
        chairLastState = chairCurrState;
    }
}
0

You really want to look at the finite state machine to model your problem. With this, you are looking at states and the transition between states.

In your problem, for example, you want to turn on a motor for a certain length of time after the chair state changes from empty to occupied. However, in your code you are not retaining the previous state (empty or occupied) so you cannot act when there is a state change fom empty to occupied.

Here is some rough C++ code to show you how this could work in paractice.

// states:
// ChairOccupancyState: occupied; empty
// MotorState: on; off

// pseudo code

loop() {
   if ( ChairOccupancyState == empty && onChair == true ) {
      // we have a transition from empty to occupied
      // switch motor on direction 1
      motorState = on ;
      // record time
      motor_on_at_ms  = millis() ;
      ChairOccupancyState = occupied ;
   }

   if ( ChairOccupancyState == occupiedy && onChair == false ) {
      // we have a transition from occupied to empty
      // switch motor on direction 2
      motorState = on ;
      // record time
      motor_on_at_ms  = millis()
      ChairOccupancyState = empty ;
   }

   If (motorState == on && millis() - motor_on_at_ms > 2000 ) {
      motorState == off ;
      // switch motor off
   }

}
0

Fairly easy.

You have to determine if the pressure has changed from its previous reading.

If it has, you can start to turn the motor for two seconds.

Once the time is up, turn the motor off.

Once you put your code in that framework, it would be much easier to code top down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.