Hi everybody, I'm quite new in the Arduino world and I'm deeply sorry if I do mistakes.
So here's my problem :
I want to control the angle position of a Servo motor using a potentiometer, when it is > or < at some point, I want the servo motor to run continuously in the good direction. So the potentiometer doesn't controle exactly the servo motor but a variable which increases or decreases.
For that I tried to make a first code only for the control of this variable by the potentiometer.
Also I took care of having a dead zone (between 545 and 490) for the potentiometer so when I turn back into the dead zone, it stops the variable frome increasing or decreasing.
The increase phase works, when I turn back into the Dead Zone the variable stops but if I try to decrease or increase it again, it doesn't want to continue.
What did I do wrong ?
Should I do it an other way ?
here is the code :
int val = 0; // Variable which will control the servo motor
int pot = 0; // potentiometer
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
pot = analogRead(A0);
if (pot > 545) {
if (val > 0) {
while (!(val <= 0)) {
if (pot > 545) {
val = (val - 1);
Serial.println(val);//This is to follow the count on Tinkercad serial monitor
delay(100); // Wait for 100 millisecond(s)
pot = analogRead(A0);
}
}
}
}
pot = analogRead(A0);
if (pot < 490) {
if (val < 180) { //Max degree for Servomotor
while (!(val >= 180)) {
if (pot < 490) {
val = (val + 1);
Serial.println(val);
delay(100); // Wait for 100 millisecond(s)
pot = analogRead(A0);
}
}
}
}
}
Sorry for my english, i'm not a native speaker and thank you for reading this