0

This is my very first arduino sketch and it's causing me a bit of a headache. Sounds so simple really all I want the sketch to do is digitalread from one of 4 digital input pins and then digitalwrite to one of 4 output pins in essence. So that when button 1 is pressed LED 1 lights up, button 2 pressed led 2 lights up and so on. I've added a serial write so I could check that the buttons are actually working when pressed.

The code I've written:

const int buttonPin1 = 7;     // number of pushbutton 1 pin
int buttonState1 = LOW;       // set the default variable value for pushbutton1 status
const int ledPin1 =  6;       // number of the LED 1 pin
const int buttonPin2 = 8;     // number of pushbutton 2 pin
int buttonState2 = LOW;       // set the default variable value for pushbutton2 status
const int ledPin2 =  9;       // number of the LED 2 pin
const int buttonPin3 = 4;     // number of pushbutton 3 pin
int buttonState3 = LOW;       // set the default variable value for pushbutton3 status
const int ledPin3 =  5;       // number of the LED 3 pin
const int buttonPin4 = 2;     // number of pushbutton 4 pin
int buttonState4 = LOW;       // set the default variable value for pushbutton4 status
const int ledPin4 =  3;       // number of the LED 4 pin

void setup() { // Set Pins to Outputs Or Inputs pinMode(buttonPin1, INPUT); // initialize the pushbutton pins as an inputs: pinMode(ledPin1, OUTPUT); // initialize the LED pins as an outputs: pinMode(buttonPin2, INPUT); // initialize the pushbutton pins as an inputs: pinMode(ledPin2, OUTPUT); // initialize the LED pins as an outputs: pinMode(buttonPin3, INPUT); // initialize the pushbutton pins as an inputs: pinMode(ledPin3, OUTPUT); // initialize the LED pins as an outputs: pinMode(buttonPin4, INPUT); // initialize the pushbutton pins as an inputs: pinMode(ledPin4, OUTPUT); // initialize the LED pins as an outputs: Serial.begin(9600); // initialize serial communication at 9600 baud }

void loop() {

buttonState1 = digitalRead(buttonPin1); // read current states of the pushbutton value: buttonState2 = digitalRead(buttonPin2); // read current states of the pushbutton value: buttonState3 = digitalRead(buttonPin3); // read current states of the pushbutton value: buttonState4 = digitalRead(buttonPin4); // read current states of the pushbutton value:

// check if the pushbutton is pressed buttonState# == HIGH/LOW // if pressed change buttonState == HIGH to turn on ledPin# // else if buttonState == LOW then digitalWrite(ledPin#, LOW) Keeps Led off.

if (buttonState1 == HIGH) { //check buttonState digitalWrite(ledPin1, HIGH); //if HIGH turn LED on: } else { digitalWrite(ledPin1, LOW); // turn LED off: } Serial.println(buttonState1); //Print buttonState to serial if (buttonState2 == HIGH) { //check buttonState digitalWrite(ledPin2, HIGH); //if HIGH turn LED on: } else { digitalWrite(ledPin2, LOW); // turn LED off: delay(10); } Serial.println(buttonState2); //Print buttonState to serial if (buttonState3 == HIGH) { //check buttonState digitalWrite(ledPin3, HIGH); //if HIGH turn LED on: } else { digitalWrite(ledPin3, LOW); // turn LED off: delay(10); Serial.println(buttonState3); //Print buttonState to serial } if (buttonState4 == HIGH) { //check buttonState digitalWrite(ledPin4, HIGH); //if HIGH turn LED on: } else { digitalWrite(ledPin4, LOW); // turn LED off: delay(10); Serial.println(buttonState4); //Print buttonState to serial }

The Leds all have a common GND and the buttons all have a common GND. Buttons are connected to +5V & Input pin# so one terminal has got two wires, Led's are connected to digital output pins then resistor and straight to GND. What happens is that ALL of the LED's come on when any one of the 4 buttons in pressed.

I've attempted to edit some of the code in the example sketches to get this to work.

Can anyone suggest anything that I'm doing wrong? Or ammend the code sp that I can see where I'm going wrong.

  • 2
    Show us your schematic. – user31481 Jul 1 '17 at 9:02
  • They way you have wired the buttons is probably not right. They also require resistors or else the digital input is open when a button is not pressed. An open input can do anything, it can even go along with a nearby pin. – Jot Jul 1 '17 at 11:08
1

As there is no schematic, it is difficult to help. Also, part of your description is difficult to understand:

...and the buttons all have a common GND. Buttons are connected to +5V & Input pin# so one terminal has got two wires,

Also, it is always better to not correct your question. Only edit your question to make it clearer. Like adding a schematic. Or cleaning up the above quoted statement.

(I will remove everything above this line once the OP gets a chance to improve their question.)

It is likely you have connected the switches incorrectly. Normally, people use single poll single throw (SPST) switches connected to 5 volts and the input pin of the Arduino. This will pull the input pin to 5 volts when the switch is closed. Then, to pull the input pin to ground when the switch is open, people add a large resistor between ground and the same processor pin.

enter image description here

Almost any large resistor will work here. Consider using 10K ohms (10,000 ohms) or there abouts.

Of course, if you are using a 3 volt Arduino, do not connect the switch to a 5 volt source. Instead connect it to a 3 volt source.

  • That's obviously the problem, the reisitor I'm using on switches isn't big enough. They are wired exactly as above except I'm not using a 10k resistor. It's of a smaller value. without cracking the multimeter out I can't remeber off the to of my head what rating I used. Does the code look alright though? I've broken it down into logical steps and done a flow chart to work out the logic. So was hoping that there was nothing wrong with the code..... – ArDweNOme Jul 1 '17 at 15:22
  • Didn't see any obvious problems with the code. But you should always start with something simple that works then build from there. The 10ms delays will not make much difference. Likely it will take longer to print out the serial data. – st2000 Jul 1 '17 at 17:28
  • Using a smaller resistor should work as well. But this will uselessly increase the current draw. It is possible you have used such a small resistor that you have exceeded the current capacity of your power supply or batteries. In such a case the voltage will drop and cause unexpected results. In extreme cases you can cause damage. – st2000 Jul 1 '17 at 17:33
  • "Normally, people use single poll single throw (SPST) switches connected to 5 volts and the input pin of the Arduino. " No, normally th internal pullup resistor is used, and the button only connectst the pin to Gnd. – CrossRoads Mar 20 '18 at 14:33
1

Get rid of the external resistors, use the internal pullup resistor.

In setup():

pinMode (pinX, INPUT_PULLUP);

Wire the button between input pin pinX and Gnd, then test for a LOW to see if the button is pressed in loop():

if (digitalRead (pinX) == LOW){ // button is pressed

// do something

}

else { // button is not pressed

// do something else

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.