Im a beginner and this is my first post so if i write something wrong please forgive me.Im writing a code that turns a servo with a potentiometer.That part works fine but i also want to add a button that turns the servo to 90 degree but when i press the button it doesn't turn.Only the potentiometer works.Im sure that arduino detects the button press because it prints it on the serial port.And here is the code:
#include <Servo.h>
Servo myservo;
int set = 1;
void setup() {
pinMode(2, INPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
myservo.attach(3);
}
void loop() {
int Val = analogRead(0);
int mappedVal = map(Val, 0, 455, 0, 180);
if (digitalRead(2) == HIGH) {
int set = 0;
Serial.println("PRESSED");
myservo.write(90);
}
else {
int set = 1;
Serial.println("NOT PRESSED");
}
if (set = 1) {
myservo.write(mappedVal);
delay(15);
}
}
If something is wrong with the code or missing please help me.Thank you for any advice.