Hey So I am working on a basic project to have a flashing row of LEDs (I have 4 leds) that can turn off when a button is pressed. Also at the same time having a potentiometer that when turned it controls how many lights flash. If anyone can help that would be awesome! here is my code:
int leds [] = {4, 5, 6, 7};
int led_value = 4;
int button_pin = 2;
int button_deger = 0;
int button_start = 0;
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 4;
void setup ()
{
for (int i = 0; i <led_value; i ++)
{
pinMode (leds [i], OUTPUT);
}
pinMode (button_pin, INPUT);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(leds [thisLed], OUTPUT);
}
}
void loop ()
{
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(leds [thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(leds [thisLed], LOW);
}
}
button_deger = digitalRead (button_pin);
if (button_deger == HIGH)
{
for (int i = 0; i <led_value; i ++)
{
if (button_start % led_value == i){
digitalWrite (leds [i], HIGH);
}
else {
digitalWrite (leds [i], LOW);
}
}
button_start++;
delay (100);
}
}