Hi, If someone could help me with my code I would be grateful.
I’m running two 6 volt dc motors (just for ease of use - I will be using 12v self locking motors in the future) an L298N dual motor driver atmega arduino uno - Microcontrollers - Arduino Forum - powered by 7.2v battery, Arduino Uno and an Interlink Electronics FSR402 0.5" Diameter Force Sensing Resistor https://www.rapidonline.com/pdf/78-4000.pdf (I will probably use the bigger pad in the future)
What I would like to achieve, is when first sitting on a chair (medium or big press), the motors run for three seconds and then stop completely while still sitting and pressure still applied.
When getting off the chair, I need a delay of 3 seconds (approximately at the moment - I can adjust the timing later), then the motor is to run in the opposite direction for 3 seconds and completely stop again until sat back down and the process starts all over again.
The code I have written at the moment makes the motors move as follows;
// soft long press makes the motor run in reverse continuously.
// soft short press and release, the motor runs for 3 seconds then stops.
// medium long press makes the motor run continuously in forward.
// medium short press makes the motor run forward, then as soon as fsr is released,
// the motor runs in reverse for 3 seconds then stops.
Thank you.
I cannot attach code on iPad. I will post it as soon as I can.
[int fsrAnalogPin = 0; // FSR is connected to analog 0 with with 10k resistor to ground
// other end connected to 5v
int fsrReading; // the analog reading from the FSR resistor divider
int MotorspeedF;
int MotorspeedR;
// Motor A
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B
int enB = 3;
int in3 = 5;
int in4 = 4;
void setup() {
Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.print(fsrReading); // the raw analog reading
if (fsrReading < 800) {
// Turn on motor A
digitalWrite(in1, HIGH); // motors forward
digitalWrite(in2, LOW);
MotorspeedF = map(fsrReading, 0, 1023, 0, 200);
analogWrite(enA, MotorspeedF);
// Turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
MotorspeedF = map(fsrReading, 0, 1023, 0, 200);
analogWrite(enB, MotorspeedF);
delay(3000);
}
else if (fsrReading > 10) {
// Turn on motor A
digitalWrite(in1, LOW); // motors reverse
digitalWrite(in2, HIGH);
MotorspeedR = map(fsrReading, 0, 1023, 0, 200);
analogWrite(enA, MotorspeedR);
// Turn on motor B
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
MotorspeedR = map(fsrReading, 0,1023, 0, 200);
analogWrite(enB, MotorspeedR);
delay(10);
}
}]
You need a finite state machine to handle the state of the movement, and millis() based timing to keep track of how long stuff runs.
So you have an IDLE state, then when the pressure is sensed you enter a JUST_PRESSED state where the motors run, after some time a PRESSED state that just doesn't do much, then when the pressure drops a RELEASED_WAITING state, then after some time the RELEASED state where the motors run, which after some time goes to IDLE.
Something like that.
Look up finite state machines and millis() based timing (as in Blink Without Delay).
Thank you very much wvmarle I will definitely look that up and get into it.