Go Down

Topic: New to arduino need project help (Read 1 time) previous topic - next topic


My goal is to use two switches to run my (continuous rotation) servo motor. By pushing one button the servo will go CCW by pushing the other it will go CW and when neither button is pushed the servo will do nothing.

This is my code:

#include <Servo.h>
Servo myservo;

int Switch1=11;
int Switch2=12;
int val=0;

int buttonState = 0;
int servoPosition = 0;

void setup(){
  pinMode(Switch1, INPUT);
  digitalWrite(Switch1, HIGH);
  pinMode(Switch2, INPUT);
  digitalWrite(Switch2, HIGH);


void loop(){
  val = digitalRead(Switch1);        //Read switch value
  if (val == HIGH) {                         //If servo was already in other position
  if (val == LOW) {                 //If switch is pressed
    //if (servoPosition == 0) {      //and servo is in original position
    //  servoPosition = 1;           //and set servo state to other
  val = digitalRead(Switch2);        //Read switch value
  if (val == LOW) {                 //If switch is pressed
  // servoPosition = 0;           //and set servo state to original

I can get it so it recognizes the one switch but it won't recognize the other. I'm really clueless when it comes to coding so I sure that the solution is simple I just don't know it...

Thanks in advance :)

Go Up