Hello everyone,
I want to control two servos using a force sensitive resistor (FSR) and a potentiometer
So if I set the potentiometer in 1023, I can use the FSR to control the servos, if I set it between 513 and 1022 I can control the position of the servos according to the value of the potentiometer between 513 and 1022 and if I set it below 512 the servos moves every two seconds between two determined positions.
However I haven't been able to make it work properly, for example, the first "if" which moves the servos between two positions works and the variable to use the FSR works well too, but, the other one isn't working and I don't know what I'm doing wrong, I don't know if something is missing. I have tried using "else" but it's not working either. Between 513 and 1022, I still can use the FSR which I think it is quite rare although, to be fair, I'm barely new on this.
If you can help me with my code, I will appreciate it so much and please bear with me and excuse me for my poor english.
Here's the code so you can revise it, thanks.
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int pos;
int frs = 0; //Force sensitive resistor
int pot = 1; //Potentiometer
int force; // the analog reading from the FSR resistor divider
int val;
void setup()
{
Serial.begin(9600);
myservo1.attach(9); // pin the servo is connected to
myservo2.attach(10); // pin the servo is connected to
}
void loop()
{
int analogValuePot = analogRead(pot);
if(analogValuePot > 512) {
myservo1.write(10);
myservo2.write(10);
delay(2000);
myservo1.write(180);
myservo2.write(180);
delay(2000);
}
else if(analogValuePot = 1023)
{
force = analogRead(frs); // Reads the FSR
Serial.print("Analog reading = ");
Serial.println(force); // This will print the raw force value
pos = map(force, 0, 1023, 180, 10); // Scales the force reading to degrees for servo control
Serial.print("Adjusted reading = ");
Serial.println(pos); // This will print the adjusted servo reading (an angle)
myservo1.write(pos); // Write the new angle to the servo
myservo2.write(pos); // Write the new angle to the servo
delay(150); // Delay 150 milliseconds before taking another reading
}
else if(513 < analogValuePot && analogValuePot < 1022)
{
int valuePot = map(analogValuePot, 513, 1022, 10, 180); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(valuePot); // sets the servo position according to the scaled value
myservo2.write(valuePot); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
}
}