problem:control 5 servos independently with pot with buttonstatechange

so heres my idea:
on an arduino mega 2560
5 servos total (9g sg90 microservos)
one button using buttonPushCounter
1 push = servo1 controlled by potentiometer
2nd push servo2 controlled by pot
3rd " "
4th " "
5th " "

so i went ahead and frankensteined together servo knob example, buttonStateChange example, and i forget how many others…

since i’ll only be using one servo at a time i assume the arduino 5v pin would suffice to power all parts

the problem I’m having is that when i upload the sketch and try to use it the way ive intended it controls what seems to be random groups of the servos instead of one servo at a time in the order I intended.

how many things am i doing wrong and how can i fix them?

heres the code
(ill fritz it soon)

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

const int buttonPin = 2;
const int potPin = A0;

int vala;
int valb;
int valc;
int vald;
int vale;

// int valf; if needed for value of 0/low/off

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup()
{
 myservo1.attach(4);
 myservo2.attach(5);
 myservo3.attach(6);
 myservo4.attach(7);
 myservo5.attach(8);

 pinMode(buttonPin, INPUT);
 pinMode(potPin, INPUT);
 


 Serial.begin(9600);
 }

void loop()
{
 buttonState= digitalRead(buttonPin);
   {
   if (buttonState != lastButtonState);
   {
     if (buttonState == HIGH)
     {
       buttonPushCounter++;
       Serial.println("on");
       Serial.print("number of button pushes:  ");
       Serial.println(buttonPushCounter);
       }
       else
       {
         Serial.println("off");
       }
   }
 }
 lastButtonState = buttonState;

 
 if (buttonPushCounter % 1 == 0)
 {
   vala = analogRead(potPin);
   vala = map(vala, 0, 1023, 0, 179);
   myservo1.write(vala);
   delay(15);
 }

  if (buttonPushCounter % 2 == 0)
 {
   valb = analogRead(potPin);
   valb = map(valb, 0, 1023, 0, 179);
   myservo2.write(valb);
   delay(15);
 }

  if (buttonPushCounter % 3 == 0)
 {
   valc = analogRead(potPin);
   valc = map(valc, 0, 1023, 0, 179);
   myservo3.write(valc);
   delay(15);
 }

  if (buttonPushCounter % 4 == 0)
 {
   vald = analogRead(potPin);
   vald = map(vald, 0, 1023, 0, 179);
   myservo4.write(vald);
   delay(15);
 }

  if (buttonPushCounter % 5 == 0)
 {
   vale = analogRead(potPin);
   vale = map(vale, 0, 1023, 0, 179);
   myservo5.write(vale);
   delay(15);
 }
}

first_frankenstein.ino (1.7 KB)

since i’ll only be using one servo at a time i assume the arduino 5v pin would suffice to power all parts

NO.

Even though you may only move one servo at a time they will all draw current.
And, in any case, it is not a good idea to power even a single servo from the 5v pin.

Please modify your Post and post your program using the code button </>

so it looks like this

and does not have any smileys.

Why are you doing something as complicated as

if (buttonPushCounter % 3 == 0)

when

if (buttonPushCounter  == 3)

seems simpler and easier to understand

…R

Hi, You are using your press button to put 5V on the input pin, do you have a pull down resistor on the input pin to pull the input to gnd when the button is OFF. 10K resistor will do the trick.

Tom... :)

Simple servo button code. Also, 7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#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);
  }
}

Robin2:
NO.

" "
…R

thank you, ive changed it

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

const int buttonPin = 2;
const int potPin = A0;

int vala;
int valb;
int valc;
int vald;
int vale;

// int valf; if needed for value of 0/low/off

int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

void setup()
{
  myservo1.attach(4);
  myservo2.attach(5);
  myservo3.attach(6);
  myservo4.attach(7);
  myservo5.attach(8);

  pinMode(buttonPin, INPUT);
  pinMode(potPin, INPUT);
  
 

  Serial.begin(9600);
  }

void loop()
{
  buttonState= digitalRead(buttonPin);
    {
    if (buttonState != lastButtonState);
    {
      if (buttonState == HIGH)
      {
        buttonPushCounter++;
        Serial.println("on");
        Serial.print("number of button pushes:  ");
        Serial.println(buttonPushCounter);
        }
        else
        {
          Serial.println("off");
        }
    }
  }
  lastButtonState = buttonState;

  
  if (buttonPushCounter == 1)
  {
    vala = analogRead(potPin);
    vala = map(vala, 0, 1023, 0, 179);
    myservo1.write(vala);
    delay(15);
  }

   if (buttonPushCounter == 2)
  {
    valb = analogRead(potPin);
    valb = map(valb, 0, 1023, 0, 179);
    myservo2.write(valb);
    delay(15);
  }

   if (buttonPushCounter == 3)
  {
    valc = analogRead(potPin);
    valc = map(valc, 0, 1023, 0, 179);
    myservo3.write(valc);
    delay(15);
  }

   if (buttonPushCounter == 4)
  {
    vald = analogRead(potPin);
    vald = map(vald, 0, 1023, 0, 179);
    myservo4.write(vald);
    delay(15);
  }

   if (buttonPushCounter == 5)
  {
    vale = analogRead(potPin);
    vale = map(vale, 0, 1023, 0, 179);
    myservo5.write(vale);
    delay(15);
  }
}

TomGeorge:
Hi,
You are using your press button to put 5V on the input pin, do you have a pull down resistor on the input pin to pull the input to gnd when the button is OFF.
10K resistor will do the trick.

Tom… :slight_smile:

like the fritzing pic?


im pretty sure ive wired and coded it correctly as of now
but any more advice would be appreciated

Hi,

If you are using the long version of the protoboard, check that the power bus red and black are continuous down the long sides, some have breaks at halfway point.

Tom… :slight_smile:

Protoboards.jpg

Protoboards1.jpg

thanks all