1

I'm trying to understand Arduino with push buttons. But I can't get the test project I'm working on to work as how I want it to do.

Basically what I'm trying to do with the codes below is to make the push button either start or stop the servo motor. But so far, I couldn't get it to work. I'm also wondering why the servo motor won't run on loop? I tried several approach, but I couldn't get it working.

What am I missing here? Thanks.

By the way, I'm a beginner.

#include <Servo.h> //Servo Library.

int servoPin = 3; //Servo to Pin 2.
int led = 4; // LED to Pin 4.
int buttonState = 0;
int lastButtonState = 0;

Servo ServoOne; //Servo object created.

void setup() {
  Serial.begin(9600);
  ServoOne.attach(servoPin);
  pinMode(led, OUTPUT);
  pinMode(2, INPUT);
}

void loop() {
  buttonState = digitalRead(2);
  if (buttonState != lastButtonState) {
    servoRun();
  }
  lastButtonState = buttonState;
}

void servoRun() {
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      Serial.println("Running.");
      ServoOne.write(0); //Servo resets to 0 degrees.
      digitalWrite(led, LOW); //LED turns on.
      delay (1000);
      ServoOne.write(90); //Servo turns to 90 degrees.
      digitalWrite(led, HIGH); //LED turns on.
      delay (1000);
      ServoOne.write(180); //Servo turns to 180 degrees.
      digitalWrite(led, LOW); //LED turns on.
      delay (1000);
      ServoOne.write(90); //Servo turns to 90 degrees.
      digitalWrite(led, HIGH); //LED turns on.
      delay (1000);
      ServoOne.write(0); //Servo resets to 0 degrees.
      digitalWrite(led, LOW); //LED turns on.
    } else {
      Serial.println("Off.");
      ServoOne.write(0);
      digitalWrite(led, LOW);
    }
  }
}

Edit: I tried this new approach, but I can't get the button state change saved.

#include <Servo.h> //Servo Library.

//Constant.
const int pswitch = 2; //Switch to Pin 2.

//Variables.
int servoPin = 3; //Servo to Pin 3.
int led = 4; //LED to Pin 4.
int newState; //Current switch state.
int oldState = LOW; //Previous switch state.

Servo ServoOne; //Servo object created.

void setup() {
  Serial.begin(9600);
  ServoOne.attach(servoPin);
  pinMode(led, OUTPUT);
  pinMode(pswitch, INPUT_PULLUP);
}

void loop() {
  newState = digitalRead(pswitch);

  if (oldState == LOW && newState == HIGH) {
    Serial.println("ON and running.");
    ServoOne.write(90);
    digitalWrite(led, HIGH);
    delay(1000);
    ServoOne.write(0);
    digitalWrite(led, LOW);
    delay(1000);
    ServoOne.write(90);
    digitalWrite(led, HIGH);
    delay(1000);
    ServoOne.write(180);
    digitalWrite(led, LOW);
    delay(1000);
    ServoOne.write(90);
    digitalWrite(led, HIGH);
    delay(1000);
  } else {
    Serial.println("OFF.");
    delay(1000);
  }
  oldState = newState;
}
14
  • write a simple sketch that turn the motor on ... does the motor run?
    – jsotola
    May 2 '20 at 23:38
  • what is the purpose of the first if statement in servoRun()?
    – jsotola
    May 3 '20 at 0:22
  • So what I want it to do is to keep the servo running from 0, 90, 180, 90 and back to 0 on loop. The first if checks the state of the button. If it's pressed, it should supposed to 0 degrees and turn the LED off.
    – crispywisp
    May 3 '20 at 3:01
  • Basically I wanted to create a toggle switch button using one button. Is it possible? To toggle the servo to turn on and turn off? If it's toggled on, it will continue doing the 0, 90, 180, 90, 0 on loop, and if it's toggled off, it will stop. And if it's toggled on again, it will continue doing its job from where it stopped?
    – crispywisp
    May 3 '20 at 3:03
  • So, you want one button press to start the motor and keep it turning until the next button press? As far as I can see, one button press will now start the motor, will do a full revolution and then stop. Pressing the button again will start the motor again due to the button bouncing (or it may do nothing if the button is debounced externally). Correct?
    – StarCat
    May 3 '20 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.