I have a linear actuator that I would like to trigger based on a flex sensor and a relay.
I have a 10k resistor attached to the flex sensor and when straight it shows 303 resistance, when bent it is about 197 resistance.
My flex sensor is connected to analog 0 while my relays are connected to pin 7 and 8
I would like the relay to trigger the actuator to extend if the flex sensor is bent with the 197 resistance and for the relay to trigger retract if the flex sensor is straight with the 303 resistance.
I found a code that I'm trying to adapt to fit my needs but I honestly have no idea what to change for this to do what I want or if I need to use something else. Right now it keeps looping to extend the actuator and retract and I need it to just extend once if triggered and retract when triggered.
if someone can please help me, I will have the coolest Maleficent costume ever with wings extending if I bend my finger (like a power gesture) and when my hand is relaxed or fingers straight the wings collapse
here is the code:
#define CW1 7 //motor 1 pin 7//
#define CW2 8 //motor 2 pin 8//
int analogInPin = A0;
int sensorValue = 0;
int outputValue = 0;
void setup() { //Setup runs once//
pinMode(CW1, OUTPUT); //Set CW1 as an output//
pinMode(CW2, OUTPUT); //Set CW2 as an output//
// pinMode(CCW1, OUTPUT); //Set CCW1 as an output//
// pinMode(CCW2, OUTPUT); //Set CCW2 as an output//
}
void loop() { //Loop runs forever//
sensorValue = analogRead(analogInPin) / 4;
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(CW2, sensorValue);
if (sensorValue >= 200)
digitalWrite(CW1, HIGH); //Motor runs clockwise//
digitalWrite(CW2, LOW); //Motor runs clockwise//
// digitalWrite(CCW1, HIGH); //Motor off//
// digitalWrite(CCW2, HIGH); //Motor off//
delay(10); //for time//
digitalWrite(CW1, HIGH); //Motor on//
digitalWrite(CW2, LOW); //Motor on//
// digitalWrite(CCW1, LOW); //Motor on//
// digitalWrite(CCW2, LOW); //Motor on//
delay(10000); //for time//
if (sensorValue <= 225)
digitalWrite(CW1, LOW); //Motor runs clockwise//
digitalWrite(CW2, HIGH); //Motor runs clockwise//
// digitalWrite(CCW1, HIGH); //Motor off//
// digitalWrite(CCW2, HIGH); //Motor off//
delay(10); //for time//
digitalWrite(CW1, LOW); //Motor on//
digitalWrite(CW2, HIGH); //Motor on//
// digitalWrite(CCW1, LOW); //Motor on//
// digitalWrite(CCW2, LOW); //Motor on//
delay(10000); //for time//
}