In the code I am using two buttons and a servomotor, the buttons are to increase and decrease, the sg90 servo moves from 0 to 180, with the two buttons I want to control the initial angle that is 0, that is, when increasing or decreasing the starting angle, this moves automatically according to that starting angle, for example, if I change it with the buttons to 30, the servomotor moves from 30 to 180 and returns from 180 to 10.
Only the starting angle is modified, and then I want to read it in the serial terminal where it shows me the starting angle that moves and the ending angle that is 180.
#include <Servo.h>
Servo myservo;
int sum=8;
int subtraction=4;
int pos = 0;
int initial = 0;
void setup(){
Serial.begin(9600);
pinMode(sum,INPUT);
pinMode(subtraction,INPUT);
myservo.attach(9);
myservo.write(pos);
}
void loop(){
myservo.write(pos);
for(int pos=initial; pos<=180; pos++){
myservo.write(pos);
Serial.println(pos);
if(digitalRead(8) == HIGH && initial < 180){
initial = initial + 1;
}
if(digitalRead(4) == HIGH && initial > 0){
initial = initial -1 ;
}
}
}
The problem of the code, use a for loop and it does not read well with serial terminal and the movement is not from 0 to 180 and 180 to 0, obviously changing the initial angle according to the buttons, someone can help me.
Thank you