LED Bar graph starting again and again

Hello everyone,

is it possible to let the LED bar graph (example project) start again from LED 1 instead of going back? I will explain: with the LED bar graph (it is an example project you can just download) you can light up LEDs 1 after 1. But if you programm it like the example it wil go from LED 1 to 2 to 3 to 4 to 5 to 6 to 7 to 8 to 9 to 10 (example video: https://www.youtube.com/watch?v=nW_k-VSYOU8) and then again back to 9 to 8 to 7 to 6 to 5 to 4 to 3 to 2 and to 1. But is it also possible that after LED 10 it goes to Led 1 again? like: Led 1 to 2 to 3 to 4 to 5 to 6 to 7 to 8 to 9 to 10 and then again to 1 to 2 to 3 and so on. So that it not returns from 10 to 9 to 8 etc. but that after 10 it again goes to LED 1?

I hope you understand it... Please help...!

Sounds easy enough. Post the code you have now. Make sure you use code tags!

Paul

I am just a beginner, so probably it’s a “bad” question, but this is the code:

/*
 LED bar graph

 Turns on a series of LEDs based on the value of an analog sensor.
 This is a simple way to make a bar graph display. Though this graph
 uses 10 LEDs, you can use any number by changing the LED count
 and the pins in the array.

 This method can be used to control any series of digital outputs that
 depends on an analog input.

 The circuit:
  * LEDs from pins 2 through 11 to ground

created 4 Sep 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/BarGraph
*/


// these constants won't change:
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 10;    // the number of LEDs in the bar graph

int ledPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[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(ledPins[thisLed], HIGH);
    }
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW);
    }
  }
}

What part of "Make sure you use code tags!" did you not understand? No help for you!

What do you mean with code tags?

Code tags, as explained in the "How to use the forum" thread.

Like this?

/*
  LED bar graph

  Turns on a series of LEDs based on the value of an analog sensor.
  This is a simple way to make a bar graph display. Though this graph
  uses 10 LEDs, you can use any number by changing the LED count
  and the pins in the array.

  This method can be used to control any series of digital outputs that
  depends on an analog input.

  The circuit:
   * LEDs from pins 2 through 11 to ground

 created 4 Sep 2010
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/BarGraph
 */


// these constants won't change:
 const int analogPin = A0;   // the pin that the potentiometer is attached to
 const int ledCount = 10;    // the number of LEDs in the bar graph

int ledPins[] = {
   2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};   // an array of pin numbers to which LEDs are attached


void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     pinMode(ledPins[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(ledPins[thisLed], HIGH);
     }
     // turn off all pins higher than the ledLevel:
     else {
       digitalWrite(ledPins[thisLed], LOW);
     }
   }
}

Half right. Fix that and edit your previous post too. We love to help around here, but we hate it when there are no code tags.

What should I change then?

Look at what you posted. All the code should be inside the tags so it appears inside the scrollable box. You put the tags in the wrong place, that's all. Its not rocket science. Try again.

I fixed it…
Can you now please help me to solve the problem of the lights?

Can you explain under what condition of the analogue input, the lights should go from LED 10 to LED 1?

Thanks for fixing those code tags. Now we can read your code comfortably.

With the code you posted, if you turn the potentiometer from minimum to maximum, leds 1 to 10 should light up in sequence. If you then turn the pot back down to minimum, the leds should go off again in reverse sequence. Is that what you see happening? In your original post, you said you want the leds to start at 1 again instead of going back. So do you mean you want the leds to go back to 1 when you start turning the pot down from max to min?

Yeah... Exactly! That's what I want!

Hello everyone…

Is it possible to let the light chase start again, so now it goes from 1 to 2 to 3 to 4 to 5 to 6 to 7 to 8 to 9 to 10 to 9 to 8 etc. But is it possible to let it go like this?: 1 to 2 to 3 to 4 to 5 to 6 to 7 to 8 to 9 to 10 to 1 to 2 etc. So that after light 10 it starts again at LED 1 instead of going back via LED 9?

Can you please also check the code (because I just found in on the internet) and can you please help me to solve my problem to let the lights start again?

Please help me!

Code:

// Project 5 - LED Chase Effect

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Create array for LED pins

int ledDelay(65); // delay between changes

int direction = 1; //starting direction

int currentLED = 0; //the current LED 

unsigned long changeTime; //stores a 32-bit variable

void setup() {

  //starts the sketch

for (int x=0; x<10; x++) { // set all pins to output

pinMode(ledPin[x], OUTPUT); }//for numbers less than 10, the pinMode increases by 1.

changeTime = millis();//time changes by milliseconds

}




void loop() {

  //loops the sketch

if ((millis() - changeTime) > ledDelay) { // if it has been ledDelay ms since last change

changeLED();

//function that's defined below

changeTime = millis();

}

}

void changeLED() {

for (int x=0; x<10; x++) { // turn off all LED's

digitalWrite(ledPin[x], LOW);

}

digitalWrite(ledPin[currentLED], HIGH); // turn on the current LED

currentLED += direction; // increment by the direction value

// change direction if we reach the end

if (currentLED == 9) {direction = -1;}//once the LED reaches the last position it goes back the other way

if (currentLED == 0) {direction = 1;}//once it reaches the first position, it goes the other way

Sure. You need to fix some syntax:

byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12};  // Create array for LED pins
unsigned long ledDelay = 65;  // delay between changes

pinMode(ledPin[x], OUTPUT); // deleted extra )
void loop() {
        if ((millis() - changeTime) >= ledDelay) {       // if it has been ledDelay ms since last change
                changeTime = millis();
digitalWrite (ledPin[x], LOW); // turn off current LED
currentLED = currentLED +1; // find next one
if (currentLED == 9){  // reset to beginning if needed
currentLED = 0;
}
digitalWrite (ledPin[currentLED], HIGH); // turn on next LED
 }

I took out the function call, blink without delay combined with a counter makes variables a little more complicated.

Is my problem now solved... so that it starts again and again? like: LED 1 to 2 to 3 to 4 to 5 to 6 to 7 to 8 to 9 to 10 and again to 1 to 2 etc.?

Would it work with this code?
That it goes from LED 1 to 2 to 3 to 4 to 5 to 6 to 7 to 8 to 9 to 10 and then again to 1 to 2 to 3 etc.?

// Project 5 - LED Chase Effect
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12};  // Create array for LED pins
unsigned long ledDelay = 65;  // delay between changes

pinMode(ledPin[x], OUTPUT); // deleted extra )

void setup() {
        for (int x=0; x<9; x++) {    // set all pins to output
                pinMode(ledPin(x), OUTPUT); )
        changeTime = millis();
        }

}

void loop() {
        if ((millis() - changeTime) >= ledDelay) {       // if it has been ledDelay ms since last change
                changeTime = millis();
digitalWrite (ledPin[x], LOW); // turn off current LED
currentLED = currentLED +1; // find next one
if (currentLED == 9){  // reset to beginning if needed
currentLED = 0;
}
digitalWrite (ledPin[currentLED], HIGH); // turn on next LED
 }

You seem to be cross-posting. You asked almost the same question in this topic. You seem determined to break every rule in this forum. You waste all our time by doing this.