I have been working on a code for my arduino, I have all my connections but no button yet. I was wondering if anyone can recommend a button code for my LED lights. Specially, just a simple push button code for my LED lights is all I am really asking.
What I am trying to do it have a push button turn on the LED lights not the LCD display.
Can anyone help me make a button code for my LED lights?
ledPin[] is a 5-element array, the loop above indexes outside its bounds. This is never a good thing and can cause serious and inexplicable problems that are difficult to debug. A better approach:
for (int x=0; x<sizeof(ledPin)/sizeof(ledPin[0]); x++) {
pinMode(ledPin[x], OUTPUT);
}
Edit: The button is assigned to a pin that's also used for the LCD. The button will also need a pull-up resistor (or a pull-down resistor, but the MCU has internal pullups, so I usually opt for those).
void loop() {
printLine(1);
if ((millis() - changeTime)> ledDelay){
changeTime= millis();
val= digitalRead(BUTTON);
if (val==HIGH) {
digitalWrite(ledPin[currentLED],LOW); // set the old one OFF
currentLED += direction;
digitalWrite(ledPin[currentLED],HIGH); // set the new one ON
if (currentLED==4) {
direction=-1;
}
if (currentLED==0) {
direction=1;
}
}
}
}
is written such that it moves to the next led every 650ms, but only while you are holding the button down. It is not triggered by pressing the button (a LOW-HIGH transition), rather it is triggered while the button is pressed. Is that what you intended?
Some general notes, I modified the indexes as previously noted. I also reduced the complexity a little by simply turning off the old led then moving, rather than looping through and turning off all the leds. Generally, it's a good idea to use good indentation as it makes the code blocks easier to read and reduces obvious errors.