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 pinvoid 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.