Okay, done. The code now looks like this:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int ServoPin = 9;
const int ButtonPin = 8;
void setup()
{
pinMode(ButtonPin, INPUT_PULLUP);
myservo.attach(ServoPin); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
Serial.println("Setup");
}
void loop()
{
if (digitalRead(ButtonPin) == LOW) {
for(int pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println("Loop1");
}
}
if (digitalRead(ButtonPin) == HIGH) {
for(int pos = 0; pos <180; pos += 0) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println("Loop2");
}
}
}
And the monitor outputs:
Setup
Loop2
Loop2
Loop2
Loop2
etc. etc.
The first time I pressed the button a few uploads ago, it moved once to its current position, and since then has not responded. I'm inclined to think the code is telling it, if it is in one position AND the button is pressed, then move to the other position then STOP, but not the opposite, so once it's in the current position the code has no provision for moving it, but I'm not sure what that change would look like