Buttons Activating other Buttons and Just not Functioning

My buttons for my arduino are set up correctly and I've checked multiple times, but my buttons just won't work. I'll press the button on pin 2(the blue btn) and it will start to work but then stop, and when I let go it activates the red button or the green button for no apparent reason. The green and red buttons will work sometimes, but sometimes they'll activate each other. The code is below, can someone help?

#include <Servo.h>

int red_led = 8;
int green_led = 9;

int blue_btn = 2;
int red_btn = 3;
int green_btn = 4;

int btn_state = 0;

int motor_pin = 11;
Servo motor;

void setup() {
  pinMode(red_led, OUTPUT);
  pinMode(green_led, OUTPUT); 
  
  pinMode(blue_btn, INPUT);
  pinMode(red_btn, INPUT);
  pinMode(green_btn, INPUT);
  
  digitalWrite(red_led, HIGH);
  digitalWrite(green_led, LOW); 
  delay(1000); 
  digitalWrite(red_led, LOW);
  digitalWrite(green_led, HIGH); 
  delay(1000);
  digitalWrite(green_led, LOW);
  
  motor.attach(motor_pin);
  motor.write(95);
}

void loop() {
  btn_state = 0;
  btn_state = digitalRead(blue_btn);
  
  if(btn_state == HIGH) {
    motor.write(93);
    delay(15);
    motor.write(95);
  } else {
    delay(100);
    btn_state = 0;
    
    btn_state = digitalRead(red_btn);
    
    if(btn_state == HIGH) {
      motor.write(94);
      delay(1000);
      
      motor.write(95);
      
      digitalWrite(red_led, HIGH);
      delay(2000);
      digitalWrite(red_led, LOW);
      
      motor.write(93);
      delay(2000);
    } else {
      btn_state = 0;
      
      delay(100);
      
      btn_state = digitalRead(green_btn);
      
      if(btn_state == HIGH) {
        motor.write(94);
        delay(1000);
        
        motor.write(95);
        
        digitalWrite(green_led, HIGH); 
        delay(2000);
        digitalWrite(green_led, LOW);
        
        motor.write(94);
        delay(1250);
        motor.write(95);
        
        digitalWrite(green_led, HIGH); 
        delay(2000);
        digitalWrite(green_led, LOW);
        
        motor.write(93);
        delay(1000);
        motor.write(95);
      }
    }
  }
  
  motor.write(95);
  
  delay(15);
}

Thank you.

How are the buttons wired ?
Are you using pulldown resistors on the inputs ?
Is the servo powered from the Arduino ?

   btn_state = 0;
    
    btn_state = digitalRead(red_btn);

You only have one btn_state variable but you have multiple buttons. Could that be a problem ?

Making multiple button states didn't change anything. The buttons use a pulldown resistor set up like the arduino tutorial for buttons here: Arduino Button Tutorial. And yes the continuous servo is powered from the arduino.

Here is a schematic I drew up in Paint:

located here if you can't see it: http://blueokiris.webs.com/ferris%20wheel%20schematic.png

Sorry to state the obvious, but pressing ANY button will activate ALL of your digital inputs since they are connected if your diagram is correct.....

Regards,

Graham

At least it's an original mistake. I don't think I've ever seen this one before :slight_smile:

Well I just figured that since they all have to go to ground through the pulldown resistor, and they all have to go through Vcc, I would connect all of them to the same power and ground connections, as the leds seem to work connected this way perfectly

Try something along these lines..... :stuck_out_tongue:

Regards,

Graham

Switches.jpg

Thanks, that seems to work. It appears using three resistors to ground instead of one is a better way to do things. :slight_smile:

:wink: You can share one resistor for both of your LED's though, since you appear to use only one at a time. But usually it is good practice to use a resistor per LED also.

Glad you fixed it! :smiley:

Regards,

Graham