Hello, in short I am doing a project which involves several servos that strum guitar strings by moving between two predefined positions. I am attempting to make a function which will move a servo from one of said positions to the other with successive calls to a strum() function, without needing to give the desired position as an input. Here is the code I am using:
#include <Servo.h>
Servo servo;
int pos = 0;
void setup() {
Serial.begin(9600);
servo.attach(8);
}
void loop() {
strum();
delay(500);
Serial.println(pos);
}
void strum() {
if (pos < 100) {
servo.write(110);
pos = 110;
}
if (pos > 100) {
servo.write(90);
pos = 90;
}
}
I am using an Arduino Mega, and a SG90 servo. My expectation is that the servo would move back and forth, however it moves once to the 90 position and then stays there. The serial monitor shows that the global variable "pos" is equal to 90 in each instance of loop() but I can't figure out why the " pos<100 " condition inside of strum() is not satisfied.
It is also important to note, that I plan to scale up this code to multiple servos using Adafruit's pca9685 board, which means I cannot use servo.read() as it uses I2C.
Any help is appreciated.