Arduino Nano to control 2 servos with 2 push buttons, code problems

Hi everyone!

I have a (hopefully) quick question about my code. I am trying to use an Arduino Nano to control the movement of two servo motors (both Tower Pro SG90's) via 2 touch buttons. Basically, I need the servo to move from a specific angle to another in slow steps so long as the button is in the HIGH (closed/pressed) state and move back to the original position at roughly the same speed when in the LOW (open/released) state. The angles in the code below aren't the ones I need but are just for an initial test to see if it works, I'll sort the exact angles later.

When I go into the serial monitor, the Serial.print(...) commands don't actually print any value and the servos don't move at all whether the button is pressed or not. Buttons are connected between GND and D2/D3, the servos are connected to +5V, power GND and A0/A1. Are my pin numbers in the code correct? The servo is powered as when I turn it on I hear it stutter slightly. I also don't plan to keep it powered through the +5Vpin as I'm aware it can't deliver more than 40mA (servo needs a few 100mA when moving, with spikes of up to 1A, right?). When the code works and I put the circuit into the final product it will have its own 4.5V power supply directly from a 3xAA battery pack.

CODE!!:

#include <Servo.h>

Servo Servo1;  // raise lips
Servo Servo2;  // sniffing nose

const int Button1Pin = 2;     // the number of the pushbutton pin
const int Button2Pin = 3;
const int Servo1Pin = 23;     // the number of the LED pin
const int Servo2Pin = 24;

// variables will change:
int Button1State = 0;
int Button2State = 0;
int pos1 = 0;
int pos2 = 0;

void setup() {
  Serial.begin(9600);
  Servo1.attach(Servo1Pin);
  Servo2.attach(Servo2Pin);
}

void loop() {
  Button1State = digitalRead(Button1Pin);
  Button2State = digitalRead(Button2Pin);

  if (Button1State == HIGH) {
    for(pos1 = 0; pos1 <= 90; pos1 += 2)
    Servo1.write(pos1);
    Serial.print("Button 1 State = ");
    Serial.print(Button1State);
    delay(15);
  }
  else  {
    pos1 = 0;
    Servo1.write(pos1);
  }

  if (Button2State == HIGH) {
    for(pos2 = 0; pos2 <= 45; pos2 += 1)
    Servo2.write(pos2);
    Serial.print("Button 2 State = ");
    Serial.println(Button2State);
    delay(15);
  }
  else  {
    pos2 = 0;
    Servo2.write(pos2);
  }
}

Any help and tips/tricks are much appreciated as hopefully they will also help someone else :slight_smile:

Cheers!
N

Try using pinMode() to make D2 and D3 digital inputs. They default to being analogue inputs so probably need to be forced to be digital inputs.

You say D2 and D3 are wired to ground through the switches? Yet you test for a HIGH? That will never happen.

Instead, test for a LOW after enabling the internal pullup resistors with pinMode().

Try using pinMode() to make D2 and D3 digital inputs. They default to being analogue inputs so probably need to be forced to be digital inputs.

Digital pins 2 and 3 do not default to being analog inputs.

The internal pullup resistors DO need to be turned on, using mode INPUT_PULLUP.

Pins 23 & 24 on Nano ? Aren't the highest pins A6 & A7, 20 & 21?

outsider:
Pins 23 & 24 on Nano ? Aren't the highest pins A6 & A7, 20 & 21?

The highest pin is 19. A6 and A7 don't really have corresponding digital pin numbers on the Nano.

Basic two button servo increment test code from the past. Note that servos need an external power supply. Powering the servos from tha arduino will probably crash the arduino.

//zoomkat servo button sweep test 12-23-2013
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 5; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 6; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
int pos = 90; // variable to store and set the servo position 

void setup()
{
  Serial.begin(9600);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  servo1.write(pos); //starting position
  digitalWrite(5, HIGH); //enable pullups to make pin high
  digitalWrite(6, HIGH); //enable pullups to make pin high
  Serial.println("servo button sweep test 12-23-2013");
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    pos=(pos+1);
    if(pos>180) pos=180; //limit upper value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement 
  }    

  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    pos=(pos-1);
    if(pos<0) pos=0; //limit lower value
    Serial.println(pos); //for serial monitor debug
    servo1.write(pos); // tell servo to go to position in variable 'pos' 
    delay(150); // waits 150ms to slow servo movement
  }
}

Digital pins 2 and 3 do not default to being analog inputs.

My whoopsie ! I have no idea why I wrote what I did. Sorry for any confusion.

Thanks everyone for your replies! Much appreciated :slight_smile:
I'm going to try the pinMode suggestion as well as double check the pin numbers. Either I'm a bit stupid or the nano pin diagrams aren't very clear as to which number to use in the code to use a specific pin :stuck_out_tongue:
If it works I'll reply with the working code for other people to use if needed.
Cheers!

Sooo, I have changed the pin numbers to the correct ones (D2 = 5, D3 = 6, A0 = 14, A1 = 15) and included the pinMode commands to turn the button pins to INPUTS and make them initially LOW (and turn HIGH when pressed). The servos are also now powered directly from a 9xAA battery pack that runs through a 5V regulator I soldered together with some capacitors. The servos now jitter but don't do anything when the button is pressed. Also, the button state (HIGH/LOW) doesn't get printed into the serial monitor (commands in the void loop if sections) so I'm wondering if there is a problem with my if statement.

// Control 2 servos with 1 button each to move between 2 positions as long as the button is presed
// Thanks to zoomkat for their code to adjust mine

#include <Servo.h>

Servo Servo1;  // raise lips
Servo Servo2;  // sniffing nose

const int Button1 = 5;     // D2
const int Button2 = 6;     // D3
const int Servo1Pin = 14;  // A0
const int Servo2Pin = 15;  // A1

// variables will change:
int Button1State = 0;
int Button2State = 0;
int pos1 = 0;
int pos2 = 0;

void setup() {
  Serial.begin(9600);
  Servo1.attach(Servo1Pin);
  Servo1.write(pos1);
  Servo2.attach(Servo2Pin);
  Servo2.write(pos2);
  pinMode(Button1, INPUT);
  pinMode(Button2,INPUT);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  Serial.println("2 Servos, 2 Buttons, 31.12.2015");
}

void loop() {
  Button1State = digitalRead(Button1);
  Button2State = digitalRead(Button2);

  if (Button1State == HIGH)
  {
    pos1 = (pos1+1);
    if(pos1 > 90) pos1 = 90;
    Serial.print("Button 1 State = ");
    Serial.println(Button1State);
    Servo1.write(pos1);
    delay(15);
  }
  else  {
    pos1 = 0;
    Servo1.write(pos1);
  }

  if (Button2State == HIGH) {
    pos2 = pos2+1;
    if(pos2 > 45) pos2 = 45;
    Serial.print("Button 2 State = ");
    Serial.println(Button2State);
    Servo2.write(pos2);
    delay(15);
  }
  else  {
    pos2 = 0;
    Servo2.write(pos2);
  }
}[CODE]

Desolator69:
The servos are also now powered directly from a 9xAA battery pack that runs through a 5V regulator I soldered together with some capacitors. The servos now jitter but don't do anything when the button is pressed.

Do the servos work with a simple sweep example?

To which pins are your buttons connected? It looks like your using the button assignments from zoomkat's code. Did you move the buttons?

You should test your setup with the example button code in the IDE. Based on your code I think you need a refresher on how to use buttons.

Hey everyone!

Sorry for the late update, haven't been able to work on this recently but I solved my problem!

The code above in post #9 works just fine, the problem was with my buttons, I hadn't wired them up correctly (just had it wired to GND and input pin >.< ).

Thanks so much for all the replies and help from everyone! I hope this code is useful to others too :slight_smile: