Help with programming flex sensor to trigger a relay

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//
}

This line says "do nothing for 10 seconds", which also means not reading the sensor. That leads to very unresponsive programs.

  delay(10000);            //for time//

This line says "anytime the sensor value is greater than or equal to 200, do the following"

  if (sensorValue >= 200)

A better approach is to study the Arduino State Change Detection example (Files > Examples > ...), where you will learn to detect when a switch becomes closed, or a sensor value becomes or exceeds 200, take appropriate action, then do something else.

The other thing you should learn is to not use delay(). The Blink Without Delay example shows you how to time actions, but also continue to read sensors and generally, be responsive.

Hi, @tbay4
Welcome to the forum.

Thanks for using code tags. :+1: :+1: :+1:

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
A hand drawn and photographed circuit is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.