4 buttons, 4 servos, one board arduino uno R3 ? Help!!!

Hello,

Im a beginner in Arduino... Could someone give me an example for my card arduino uno R3 to drive 4 servo with 4 push buttons? (assembled with a resistance 10KOhm, of course!)

The four buttons are on pins 2, 3, 4, 5 4 servos will be on pins 9, 10, 11, 12

These four buttons, imposes an angle (in degrees) to 4 servos, either:

When push button1: servo1: 0 ° - servo2: 0 ° - servo3: 0 ° - servo4: 0 ° When push button2: servo1: 45 ° - servo2: -45 ° - servo3: 45 ° - servo4: -45 ° When push button3: servo1: 45 ° - servo2: 18 ° - servo3: -45 ° - servo4: -18 ° When push Button4: servo1: -18 ° - servo2: -45 ° - servo3: 18 ° - servo4: 45 °

Thank you in advance for helping me, your help will not be free, I promise!

This code is for just one button, but you can use arrays to check multiple buttons.

const byte LED = 13;
const byte Button = 2; // use array for multiple buttons. "= {2,3,4,5};"
byte ButtonState;  // use array for the multiple button states
byte lastState = LOW; //-------------------------
unsigned long oldTime = 0; //-------------------------

// **Fill in for other buttons. Hint: use a for loop
void setup() {
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState) // check to see if button is pressed and if the state has changed
  {
    oldTime = millis(); //set the time the button was first pressed
    lastState = ButtonState; // update laststate
  }

  if((millis() - oldTime ) > 50UL) // check to see if enough time has passed 
  {
    if(ButtonState)  // if button is pressed and state is HIGH 
    {
      // Do something here
      digitalWrite(LED, HIGH);
    }
    else 
    {
      // Do something else
      digitalWrite(LED, LOW);
    }
  }
}

Servo button test code that probably be modified to do what you want, and no resistor required.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Oh thanks for that I have write that:


#include <Servo.h>
int button2 = 2; //button pin, connect to ground to move servo
int press2 = 0;
int button3 = 3; //button pin, connect to ground to move servo
int press3 = 0;
int button4 = 4; //button pin, connect to ground to move servo
int press4 = 0;
int button5 = 5; //button pin, connect to ground to move servo
int press5 = 0;
Servo servoFL,servoFR,servoBL,servoBR;

void setup()
{
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
servoFL.attach(9);
servoFR.attach(10);
servoBL.attach(11);
servoBR.attach(12);
digitalWrite(2, HIGH); //enable pullups to make pin high
digitalWrite(3, HIGH); //enable pullups to make pin high
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
press2 = digitalRead(button2);
if (press2 == LOW)
{
servoFL.write(0);
servoFR.write(0);
servoBL.write(0);
servoBR.write(0);
}

press3 = digitalRead(button3);
if (press3 == LOW)
{
servoFL.write(45);
servoFR.write(-45);
servoBL.write(-45);
servoBR.write(45);
}
press4 = digitalRead(button4);
if (press4 == LOW)
{
servoFL.write(18);
servoFR.write(45);
servoBL.write(-18);
servoBR.write(-45);
}

press5 = digitalRead(button5);
if (press5 == LOW)
{
servoFL.write(-45);
servoFR.write(-18);
servoBL.write(45);
servoBR.write(18);
}
}


Thanks zoomkat!!!
Its for this rover: http://youtu.be/7xXbaiz_P3U

Pragmatism!!!

servoFL.write(-45);

Does the write method take negative numbers as an argument and do what you want ? I was under the impression that the value should be from 0 to 180 with 90 being the centre position. So -45 degrees should be 45 and +45 degrees should be 135.

http://arduino.cc/en/Reference/ServoWrite

Youre right! the initial position for me its 90° and not 0°! Therefore 45° and 135°! Thanks and best regards

When you feel the need to number variable names, you should be using arrays.

You've given pin 2 a name (not a good one, but it has a name). Why are you then using 2 explicitly to enable the pullup?

Do you really want the servos to move when the switch IS pressed, or when it BECOMES pressed? Do you really need to tell the servos to go to where they are?

Hello!!!

Sorry, another question!

In this program:

=========================================
#include <Servo.h>
int button1 = 0; //button pin, connect to ground to move servo
int press0 = 0;
int button2 = 1; //button pin, connect to ground to move servo
int press1 = 0;
int button3 = 2; //button pin, connect to ground to move servo
int press2 = 0;
int button4 = 3; //button pin, connect to ground to move servo
int press3 = 0;
Servo servoFL,servoFR,servoBL,servoBR;

void setup()
{
pinMode(button0, INPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
servoFL.attach(6);
servoFR.attach(7);
servoBL.attach(8);
servoBR.attach(9);
digitalWrite(0, HIGH); //enable pullups to make pin high
digitalWrite(1, HIGH); //enable pullups to make pin high
digitalWrite(2, HIGH); //enable pullups to make pin high
digitalWrite(3, HIGH); //enable pullups to make pin high
}

void loop()
{
press0 = digitalRead(button0);
if (press0 == LOW)
{
servoFL.write(0);
servoFR.write(0);
servoBL.write(0);
servoBR.write(0);
}

press1 = digitalRead(button1);
if (press1 == LOW)
{
servoFL.write(135);
servoFR.write(45);
servoBL.write(45);
servoBR.write(135);
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
servoFL.write(162);
servoFR.write(135);
servoBL.write(18);
servoBR.write(45);
}

press3 = digitalRead(button3);
if (press3 == LOW)
{
servoFL.write(45);
servoFR.write(18);
servoBL.write(135);
servoBR.write(162);
}
}

================================================================

I want to add a card 4 relay (see the picture) which is also plugged into the same circuit with the servos.

I have two temporary buttons and when they are turned on, according to the angles of servos, a 4 relay switches! How to satisfy the requirement?

summary:

The new temporary buttons:

  • button 5 => PIN4
  • button 6 => PIN5

4 relays:

  • relay 1 => PIN10
  • relay 2 => PIN11
  • relay 3 => PIN12
  • relay 4 => Pin13

The buttons are used independently:

I would like to conditionally relay based servo!

An example, if the servos are oriented:

servoFL.write (135);
servoFR.write (45);
servoBL.write (45);
servoBR.write (135);

and which is based on:

  • The button 5 (pin4), relay 1 is on, relay 2, 3, 4 off
  • The button 6 (pin5), relay 2 is on, the relay 1, 3, 4 is off
    etc.

How to fulfill the condition correctly?

Best regards

Have a look at servo.read(). It returns the angle most recently written to the servo enabling you to check where the servos have been sent to when you new buttons are pressed so that you activate the relevant relay(s).

Alternatively instead of

if (press1 == LOW)
  {
    servoFL.write(135);
    servoFR.write(45);
    servoBL.write(45);
    servoBR.write(135);
  }

set 4 variables to the required servo angles and use the variables with servo.write(). Then, when your new buttons are pressed you can check the values of the variables an activate the relevant relay(s). Doing it this way would also mean that you could reduce the servo.write() commands to a single block of code instead of repeating it.