Servo Challenge Win

Jim Kenyon would like to share a win!

  • LCDs, Servos, and Twitter4J

The Win!

Not only did I succeed at the challenge but I think that I excelled. Hope you like my solution.  (I know, I need to work on my notes)

Maybe not the most elegant but works:

const byte myServo = 9;
int servoPosition = 1500;
const byte leftButton = 2;
const byte rightButton = 4;
int traverseRate = 50;

void setup() {
  Serial.begin(9600);
  pinMode(myServo, OUTPUT);
  pinMode(leftButton, INPUT);
  pinMode(rightButton, INPUT);
}

void loop() {

  bool leftButtonReading = digitalRead(leftButton);
  bool rightButtonReading = digitalRead(rightButton);
  if (leftButtonReading == HIGH && rightButtonReading == LOW) {
    servoPosition = servoPosition - traverseRate;
  }
  if (rightButtonReading == HIGH && leftButtonReading == LOW) {
    servoPosition = servoPosition + traverseRate;
  }
  if (rightButtonReading == LOW && leftButtonReading == LOW) {
    servoPosition = 1500;
  }

  Serial.print("Left Button is: ");
  Serial.print(leftButtonReading);
  Serial.print(" Right Button is: ");
  Serial.print(rightButtonReading);
  Serial.print(" servoPosition is: ");
  Serial.println(servoPosition);

  digitalWrite(myServo, HIGH);
  delayMicroseconds(servoPosition);
  digitalWrite(myServo, LOW);
  delayMicroseconds(20000 - servoPosition);
  delay(20);
}

[fvplayer src=”https://vimeo.com/334051775″ splash=”https://i.vimeocdn.com/video/780219075_1920x1082.jpg?r=pad” caption=”jim kenyon”]

Great Job!

Nice job on this Jim –  I do like your solution!

Leave a Comment