Button for my project?

Hello everyone,

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?

Thank you!

byte ledPin[] = {1,2,3,4,5};
int ledDelay(650); 
int direction= 1;
int currentLED= 0;
unsigned long changeTime;
#include <LiquidCrystal.h>
#include <string.h>
 
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 
char message[] = "Welcome To The Store!!";
int previous = 0;
int pos = 0;
 
void setup() {
lcd.begin(16, 2);
}
void printLine(int refreshSeconds){
if((millis()/800) % refreshSeconds == 0 && previous != (millis()/800)){
previous = (millis()/800);
lcd.setCursor(0, 1);
char lcdTop[16];
int copySize = 16; 
if(strlen(message) < 16)
{
copySize = strlen(message);
}
int tempPos = pos;
if(tempPos < 0)
{
tempPos = -(tempPos);
}
memcpy(&lcdTop[0],&message[tempPos],copySize);
lcd.print(lcdTop);
pos += 1;
if(pos +16 >= strlen(message))
{
pos = -(pos);
}
for (int x=0; x<9; x++) {
  pinMode(ledPin[x], OUTPUT);}
  changeTime= millis();
  }
}
 
void loop() {
printLine(1);
if ((millis() - changeTime)> ledDelay){
  changeLED();
  changeTime= millis();
}
}
void changeLED() {
  for (int x=0; x<9; x++) {
    digitalWrite(ledPin[x],LOW);
  }
  digitalWrite(ledPin[currentLED],HIGH);
  currentLED += direction;
  if (currentLED==8) {direction=-1;}
  if (currentLED==0) {direction=1;}
}

So I been attempting to insert the button but it is not having any affect on my LED lights, they are still running.

does my code look okay? i need to know if its just my connections :slight_smile:

Thank you!

const int BUTTON=12;
int val= 0;
byte ledPin[] = {1,2,3,4,5};
int ledDelay(650); 
int direction= 1;
int currentLED= 0;
unsigned long changeTime;
#include <LiquidCrystal.h>
#include <string.h>
 
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
 
char message[] = "Welcome to Christmas Town!";
int previous = 0;
int pos = 0;
 
void setup() {
lcd.begin(16, 2);
}
void printLine(int refreshSeconds){
if((millis()/800) % refreshSeconds == 0 && previous != (millis()/800)){
previous = (millis()/800);
lcd.setCursor(0, 1);
char lcdTop[16];
int copySize = 16; 
if(strlen(message) < 16)
{
copySize = strlen(message);
}
int tempPos = pos;
if(tempPos < 0)
{
tempPos = -(tempPos);
}
memcpy(&lcdTop[0],&message[tempPos],copySize);
lcd.print(lcdTop);
pos += 1;
if(pos +16 >= strlen(message))
{
pos = -(pos);
}
for (int x=0; x<9; x++) {
  pinMode(ledPin[x], OUTPUT);}
  changeTime= millis();  
  pinMode(BUTTON,INPUT);
}
}
 
void loop() {
printLine(1);
if ((millis() - changeTime)> ledDelay){
  changeTime= millis();
val= digitalRead(BUTTON);
if (val==HIGH){
  
  for (int x=0; x<9; x++) {
    digitalWrite(ledPin[x],LOW);
  }
  digitalWrite(ledPin[currentLED],HIGH);
  currentLED += direction;
  if (currentLED==8) {direction=-1;}
  if (currentLED==0) {direction=1;}}
}}

Why are

//this loop
        for (int x=0; x<9; x++) {
            pinMode(ledPin[x], OUTPUT);
        }

//and
        pinMode(BUTTON,INPUT);

not in setup()?

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

The way you have your code right now, this part:

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.