Go Down

Topic: buttons to motors don't match up at certain times (short circuit? ) (Read 442 times) previous topic - next topic

Hi All,

I am working on a project that consists of nine button switches to nine motors. All wiring is fine and servo all work as I tested it several times. Though, sometimes when I step on a button (or just activate a button) to the associated servo - it turns on another servo - so button #9 should go to servo #9 (tested and worked, but when a servo motor gets interrupted by a gear, I think maybe the servo order gets out of order??) Would debouncing help also?? When I step on a button and remain at the same spot, the servo resets itself to begin rotating from the beginning?

Thank you in advance !!!!!!!

Code: [Select]
#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;
Servo myservo8;
Servo myservo9;

int buttonPin1 = 22;
int buttonPin2 = 24;
int buttonPin3 = 26;
int buttonPin4 = 28;
int buttonPin5 = 30;
int buttonPin6 = 32;
int buttonPin7 = 40;
int buttonPin8 = 36;
int buttonPin9 = 38;


int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
int buttonState8 = 0;
int buttonState9 = 0;


void setup()
{
 
  Serial.begin(9600);

 
   //1//
   myservo1.attach(2);
  myservo1.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin1, INPUT);

  //digialWrite(buttonPin, HIGH);
 
     //2//
   myservo2.attach(3);
  myservo2.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin2, INPUT);
 
     //3//
   myservo3.attach(4);
  myservo3.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin3, INPUT);
 
     //4//
   myservo4.attach(5);
  myservo4.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin4, INPUT);
 
     //5//
   myservo5.attach(6);
  myservo5.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin5, INPUT);
 
     //6//
   myservo6.attach(7);
  myservo6.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin6, INPUT);
 
     //7//
   myservo7.attach(11);
  myservo7.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin7, INPUT);
 
     //8//
   myservo8.attach(9);
  myservo8.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin8, INPUT);
 
       //9//
   myservo9.attach(10);
  myservo9.writeMicroseconds(1525);  // set servo to mid-point
  pinMode(buttonPin9, INPUT);
}

void loop()
{
buttonState1 = digitalRead(buttonPin1);

if (buttonState1 == HIGH) {
myservo1.writeMicroseconds(1000);
delay(6050);
myservo1.writeMicroseconds(1700);
delay(6700);
myservo1.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState3 == LOW;
  buttonState4 == LOW;
  buttonState5 == LOW;
  buttonState6 == LOW;
  buttonState7 == LOW;
  buttonState8 == LOW;
  buttonState9 == LOW;
  }

buttonState2 = digitalRead(buttonPin2);

if (buttonState2 == HIGH) {
myservo2.writeMicroseconds(1000);
delay(6000);
myservo2.writeMicroseconds(1700);
delay(6200);
myservo2.writeMicroseconds(1525);
delay(1000);

  buttonState1 == LOW;
  buttonState3 == LOW;
  buttonState4 == LOW;
  buttonState5 == LOW;
  buttonState6 == LOW;
  buttonState7 == LOW;
  buttonState8 == LOW;
  buttonState9 == LOW;
  }

  buttonState3 = digitalRead(buttonPin3);

if (buttonState3 == HIGH) {
myservo3.writeMicroseconds(1000);
delay(4060);
myservo3.writeMicroseconds(1700);
delay(4060);
myservo3.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState1 == LOW;
  buttonState4 == LOW;
  buttonState5 == LOW;
  buttonState6 == LOW;
  buttonState7 == LOW;
  buttonState8 == LOW;
  buttonState9 == LOW;
  }

  buttonState4 = digitalRead(buttonPin4);

if (buttonState4 == HIGH) {
myservo4.writeMicroseconds(1000);
delay(4000);
myservo4.writeMicroseconds(1700);
delay(4000);
myservo4.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState3 == LOW;
  buttonState1 == LOW;
  buttonState5 == LOW;
  buttonState6 == LOW;
  buttonState7 == LOW;
  buttonState8 == LOW;
  buttonState9 == LOW;
  }

  buttonState5 = digitalRead(buttonPin5);

if (buttonState5 == HIGH) {
myservo5.writeMicroseconds(1000);
delay(5200);
myservo5.writeMicroseconds(1600);
delay(5000);
myservo5.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState3 == LOW;
  buttonState4 == LOW;
  buttonState1 == LOW;
  buttonState6 == LOW;
  buttonState7 == LOW;
  buttonState8 == LOW;
  buttonState9 == LOW;
  }

  buttonState6 = digitalRead(buttonPin6);

if (buttonState6 == HIGH) {
myservo6.writeMicroseconds(1000);
delay(5500);
myservo6.writeMicroseconds(1700);
delay(5500);
myservo6.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState3 == LOW;
  buttonState4 == LOW;
  buttonState5 == LOW;
  buttonState1 == LOW;
  buttonState7 == LOW;
  buttonState8 == LOW;
  buttonState9 == LOW;
  }

  buttonState7 = digitalRead(buttonPin7);

if (buttonState7 == HIGH) {
myservo7.writeMicroseconds(1000);
delay(5200);
myservo7.writeMicroseconds(1700);
delay(5200);
myservo7.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState3 == LOW;
  buttonState4 == LOW;
  buttonState5 == LOW;
  buttonState6 == LOW;
  buttonState1 == LOW;
  buttonState8 == LOW;
  buttonState9 == LOW;
  }

  buttonState8 = digitalRead(buttonPin8);

if (buttonState8 == HIGH) {
myservo8.writeMicroseconds(1000);
delay(5000);
myservo8.writeMicroseconds(1600);
delay(5000);
myservo8.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState3 == LOW;
  buttonState4 == LOW;
  buttonState5 == LOW;
  buttonState6 == LOW;
  buttonState7 == LOW;
  buttonState1 == LOW;
  buttonState9 == LOW;
  }

  buttonState9 = digitalRead(buttonPin9);

if (buttonState9 == HIGH) {
myservo9.writeMicroseconds(1000);
delay(4500);
myservo9.writeMicroseconds(1700);
delay(4500);
myservo9.writeMicroseconds(1525);
delay(1000);

  buttonState2 == LOW;
  buttonState3 == LOW;
  buttonState4 == LOW;
  buttonState5 == LOW;
  buttonState6 == LOW;
  buttonState7 == LOW;
  buttonState8 == LOW;
  buttonState1 == LOW;
}

Serial.println(buttonState1);
Serial.println(buttonState2);
Serial.println(buttonState3);
Serial.println(buttonState4);
Serial.println(buttonState5);
Serial.println(buttonState6);
Serial.println(buttonState7);
Serial.println(buttonState8);
Serial.println(buttonState9);


}





johnwasser

How are the switches wired?   Could any of the inputs be floating?

I see that you have commented out code to enable the internal pull-up on the pin for Button1.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Thanks for the reply!

The ground goes to 1k resistor and to arduino pin. Power goes to 5v pin (I have everything powered at 9V)

I commented the pull up resistor because it didn't make a difference when I tested it out...

Terry King

Hi,

As I read your code, if a button stays pressed the motor keeps going through the same sequence over and over again.

IF you want it to "run once", then you need to check that the button is released again before allowing the motor to run with a new button press..

???

You might want to experiment with Alexander's HAL library : http://www.arduino.cc/playground/Code/HardwareAbstraction

The BUTTON function http://arduino.cc/playground/Code/Button gives several ways of looking at button presses....
Regards, Terry King  ..On the Red Sea at KAUST.edu.sa
terry@yourduino.com  LEARN! DO! (Arduino Boards, Sensors, Parts @ http://yourduino.com

Great! I will experiment with press commands - do you think this may be the reason for buttons activating their non-associated servo?

Go Up