[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/diy/ - Do It Yourself

Search:


View post   

>> No.1379209 [View]
File: 58 KB, 617x517, Board_Layout_BP.jpg [View same] [iqdb] [saucenao] [google]
1379209

How would I program an arduino to sequentially cycle through 5 different LEDs individually when a button is pressed?
Also, how would you have a buzzer make a sound on the 5th button push.

> I found the following code for something similar:

Demonstrates the use of an array to hold pin numbers.
Each time the push button is pressed, next LED in the
sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = 2; // the pin number of the pushbutton input pin
int ledPins[] = {3, 4, 5, 6, 7};
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 5;
void setup() {
// initialize the LED pin as an output:
for (int i = 0; i < numberOfLED; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
for (int i = 0; i < numberOfLED; i++) {
if (buttonPressCount % numberOfLED == i) {
// turn LED on:
digitalWrite(ledPins[i], HIGH);
} else {
// turn LED off:
digitalWrite(ledPins[i], LOW);
}
}
buttonPressCount++;
delay(400);
}
}

>> No.1379197 [View]
File: 58 KB, 617x517, Board_Layout_BP.jpg [View same] [iqdb] [saucenao] [google]
1379197

How would I program an arduino to sequentially cycle through 5 different LEDs individually when a button is pressed?
Also, how would you have a buzzer make a sound on the 5th button push.

> I found the following code for something similar:

Demonstrates the use of an array to hold pin numbers.
Each time the push button is pressed, next LED in the
sequence will be turned on while the rest of LEDs are off.
*/
const int buttonPin = 2; // the pin number of the pushbutton input pin
int ledPins[] = {3, 4, 5, 6, 7};
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 5;
void setup() {
// initialize the LED pin as an output:
for (int i = 0; i < numberOfLED; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
for (int i = 0; i < numberOfLED; i++) {
if (buttonPressCount % numberOfLED == i) {
// turn LED on:
digitalWrite(ledPins[i], HIGH);
} else {
// turn LED off:
digitalWrite(ledPins[i], LOW);
}
}
buttonPressCount++;
delay(400);
}
}

Navigation
View posts[+24][+48][+96]