#include <Servo.h>
Servo servo1;
long num1 ;
const byte potpin = A3;
int deger;
void setup()
{
servo1.attach(10);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
num1 = Serial.parseInt();
servo1.write(num1);
while (Serial.available())
Serial.read();
}
deger = analogRead(potpin);
Serial.println(deger);
delay(2500);
if (digitalRead(3) == LOW)
{
if (deger == 214)
{
servo1.write(90);
}
else if (213 <= deger )
{
servo1.write(45);
}
else if (215 >= deger )
{
servo1.write(135);
}
}
if (digitalRead(4) == LOW)
{
if (deger == 300)
{
servo1.write(90);
}
else if (299 <= deger)
{
servo1.write(45);
}
else if (deger >= 301)
{
servo1.write(135);
}
}
if (digitalRead(5) == LOW )
{
if (deger == 400)
{
servo1.write(90);
}
else if (399 <= deger)
{
servo1.write(45);
}
else if (deger >= 401)
{
servo1.write(135);
}
}
if (digitalRead(3) != LOW && digitalRead(4) != LOW && digitalRead(5) != LOW )
{
servo1.write(90);
}
I have a pressure sensor, I want the servo motor to change the angle according to the values coming from there. But when you press the button, there is no change in the servo motor
Please don't post code with mismatched parentheses... Only post code that actually compiles without
error (unless you are asking about compiler errors!).
If you want to do things when a button is pressed, rather than while a button remains
pressed, use the approach of StateChangeDetection example.
Why do you have a 2.5 second delay after reading the analogs? I suspect this causes issues with button detection because you could press and release the button during that time and never see it. Also, you need to check if the button has been pressed not the state of the button. Check out the following tutorial:
Have you got code that JUST makes the servo sweep and works?
Have you got code that JUST reads the pressure sensor AND works?
Have you got code that JUST reads the button AND works?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Not a Fritzy picture, prefer a hand drawn circuit if you do not have a CAD program.