Hi all this is my first project from scratch, I have made a few kits but decide it was about time I built something myself, the circuit is built and working, but I am having trouble with the code.
I am making a small device to help me at work, the device has 4 leds and 4 buttons, what I am trying to do is light up certain leds when I press a button which I have managed to do so far.
press button 1 and 2 yellow leds light up
press button 2 and 1 yellow led lights up
press button 3 and 1 red led lights up
and button 4 a green led lights up
I am using pushbuttons with 10k pulldown resistors.
what I am trying to do is have button 4 switch on the green led and then flash on and off every second until I cancel it by pressing button 4 again, also I want it to stay on when I press any of the other button. here is the code I have so far
// set pin numbers:
const int buttonPin1 = 2; // the number of the buttonPin1 pin
const int buttonPin2 = 3; // the number of the buttonPin2 pin
const int buttonPin3 = 4; // the number of the buttonPin3 pin
const int buttonPin4 = 5; // the number of the buttonPin4 pin
const int ledPin1 = 9; // the number of the ledPin1 pin
const int ledPin2 = 10; // the number of the ledPin2 pin
const int ledPin3 = 11; // the number of the ledPin3 pin
const int ledPin4 = 12; // the number of the ledPin4 pin
// variables will change:
int buttonState1 = 0; // variable for reading the buttonPin1 status
int buttonState2 = 0; // variable for reading the buttonPin2 status
int buttonState3 = 0; // variable for reading the buttonPin3 status
int buttonState4 = 0; // variable for reading the buttonPin4 status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the buttonPin pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void loop(){
// read the state of the buttonPin value:
buttonState1 = digitalRead(buttonPin1);
// switch on 2 yellow leds
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin4, LOW);
}
// read the state of the buttonPin value:
buttonState2 = digitalRead(buttonPin2);
// switch on 1 yellow led
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin4, LOW);
}
// read the state of the buttonPin value:
buttonState3 = digitalRead(buttonPin3);
// switch on red led
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState3 == HIGH) {
// turn LED on:
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, LOW);
}
// read the state of the buttonPin value:
buttonState4 = digitalRead(buttonPin4);
//switch on green led
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState4 == HIGH) {
// turn LED on:
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
}
}
I would be grateful of any help