[Random] Fluctuation of LED bar graph - help please!

Hi Guys, hoping someone out there can help me? Let me start by saying I am a complete beginner when it comes to Arduino and have been getting help from a friend who is unfortunately not around to help me with an issue i'm having at the moment.

I have designed a furniture product (my real talent!) which has a semi circle of 10 LEDs built into it. In the past, my friend had the LEDs linked up to a slide potentiometer which sequentially turned on all 10 of the LEDs as the slider moved to the right and sequentially turned them off as the slider moved to the left.

The code looked like this :

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);
}
}
}

The purpose of this was so I could use the slide potentiometer to demonstrate a hypothetical reality, in which the LEDs would fluctuate on their own based on a data input.

The project is on show at an event next week but I won't be there to demonstrate the LEDs using the slide potentiometer.

Basically I want the ring of 10 LEDs to come on and off sequentially (always ascending or descending, adding or subtracting one at a time) in a random order. I would like them to fluctuate 'randomly' while still following the correct order (1-10) with only one LED changing at a time.

When I say 'random' I mean I don't want them to go; 1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1 on a loop.. I want something along the lines of: 1,2,3,4,5,6,5,4,5,6,7,8,9,8,7,8,7,6,5,6,7,8,9,10,9 etc.. 'randomly' moving up and down the scale one at a time.

This is a youtube product video I made to demonstrate the idea. it should give a clearer idea of what i want to achieve LUX - A SAD lamp to smile about. - YouTube

Any help in achieving this would be hugely appreciated! Thanks.

To progress I would like to advance from having to change the amount of LEDs manually to an automated system whereby the LEDs would fluctuate themselves slowly over the course of an hour or something..

So, what is the problem? If you know what part of the code changes the LED display based on the value read from an analog pin, then changing that input to a random number instead is a one line of code change.

I got a lot of help with this code so I can't say i'd be sure on what I was doing..

sswanton:
I got a lot of help with this code so I can't say i'd be sure on what I was doing..

Can you at least identify the part of the code that reads from an analog pin? Hint: the function will have the words read and analog in the name.

Once you do that, you can comment out that statement, and add a new one that assigns a value to the same variable, based on a call to a random number generator (strangely enough, the Arduino includes a function called random(). Can you guess what it does?)

Hi Guys, hoping someone out there can help me? Let me start by saying I am a complete beginner when it comes to Arduino and have been getting help from a friend who is unfortunately not around to help me with an issue i'm having at the moment.

I have designed a furniture product (my real talent!) which has a semi circle of 10 LEDs built into it. In the past, my friend had the LEDs linked up to a slide potentiometer which sequentially turned on all 10 of the LEDs as the slider moved to the right and sequentially turned them off as the slider moved to the left.

The code looked like this :

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);
}
}
}

The purpose of this was so I could use the slide potentiometer to demonstrate a hypothetical reality, in which the LEDs would fluctuate on their own based on a data input.

The project is on show at an event next week but I won't be there to demonstrate the LEDs using the slide potentiometer.

Basically I want the ring of 10 LEDs to come on and off sequentially (always ascending or descending, adding or subtracting one at a time) in a random order. I would like them to fluctuate 'randomly' while still following the correct order (1-10) with only one LED changing at a time.

When I say 'random' I mean I don't want them to go; 1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1 on a loop.. I want something along the lines of: 1,2,3,4,5,6,5,4,5,6,7,8,9,8,7,8,7,6,5,6,7,8,9,10,9 etc.. 'randomly' moving up and down the scale one at a time.

This is a youtube product video I made to demonstrate the idea. it should give a clearer idea of what i want to achieve LUX - A SAD lamp to smile about. - YouTube

Any help in achieving this would be hugely appreciated! Thanks.

Hi Steve

Try this code to start with. It prints LED numbers to serial monitor, ramping up and down between random numbers, with 1s delay between each step.

If this is giving the behaviour you need, then it can be combined with your current code.

const int ledCount = 10;    // the number of LEDs in the bar graph

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  static int currentPosition = 0;
  static int direction = 1; // +1 for going up, -1 for going down
  static int targetPosition = 0;
  
  if (direction > 0)
  {
    targetPosition = random(currentPosition + 1, ledCount);
  }
  else
  {
    targetPosition = random(0, currentPosition);
  }
  Serial.print("\r\ntarget: ");
  Serial.println(targetPosition);
  Serial.println();
  
  while (currentPosition != targetPosition)
  {
    currentPosition += direction;
    Serial.print("current: ");
    Serial.println(currentPosition);
    
    // turn on LEDs
    
    delay(1000);
  }
  
  direction = -direction;
  
}

Regards

Ray

Hackscribble:
Hi Steve

Try this code to start with. It prints LED numbers to serial monitor, ramping up and down between random numbers, with 1s delay between each step.

If this is giving the behaviour you need, then it can be combined with your current code.

const int ledCount = 10;    // the number of LEDs in the bar graph

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  static int currentPosition = 0;
  static int direction = 1; // +1 for going up, -1 for going down
  static int targetPosition = 0;
 
  if (direction > 0)
  {
    targetPosition = random(currentPosition + 1, ledCount);
  }
  else
  {
    targetPosition = random(0, currentPosition);
  }
  Serial.print("\r\ntarget: ");
  Serial.println(targetPosition);
  Serial.println();
 
  while (currentPosition != targetPosition)
  {
    currentPosition += direction;
    Serial.print("current: ");
    Serial.println(currentPosition);
   
    // turn on LEDs
   
    delay(1000);
  }
 
  direction = -direction;
 
}




Regards

Ray

Hi Ray,
Thats great thanks! I've been looking at the serial monitor for that and looks like what I want to achieve. Any advice on how to get it into my code? I have tried but am getting a lot of error messages.

Any help greatly appreciated!

something like this?

  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 ledLevel=0;
  
  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 dif=0;
    
    if (ledLevel==0) //the only way is up
      dif=1;
    if (ledLevel==9) //the only way is down
      dif= -1;
  
    //if it hasn't been defined
    if(dif==0)
      dif=(random(0,2)>1)? -1:1;//either up or down
      
    //so now impose iton sensorReading
    ledLevel=ledLevel+dif;
    
  /*  Not using this any more
    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);
      }
    }
  }

KenF:
something like this?

  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 ledLevel=0;
 
  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 dif=0;
   
    if (ledLevel==0) //the only way is up
      dif=1;
    if (ledLevel==9) //the only way is down
      dif= -1;
 
    //if it hasn't been defined
    if(dif==0)
      dif=(random(0,2)>1)? -1:1;//either up or down
     
    //so now impose iton sensorReading
    ledLevel=ledLevel+dif;
   
  /*  Not using this any more
    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);
      }
    }
  }

Hi KenF,
thanks for your help, for some reason when I upload this code, 9 out of the 10 LEDs come on permanently and sty that way indefinitely?.. Any ideas?

Yep

  dif=(random(0,2)>1)? -1:1;//either up or down

should read

  dif=(random(0,2))? -1:1;//either up or down

(remove the >1)

still the same! :disappointed_relieved:

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() 
{
  Serial.begin(9600);
  for (int thisLed = 0; thisLed < ledCount; thisLed++) 
  {
    pinMode(ledPins[thisLed], OUTPUT); 
  }
}

void loop() 
{
  static int currentPosition = 0;
  static int direction = 1; // +1 for going up, -1 for going down
  static int targetPosition = 0;

  if (direction > 0)
  {
    targetPosition = random(currentPosition + 1, ledCount);
  }
  else
  {
    targetPosition = random(0, currentPosition);
  }
  Serial.print("\r\ntarget: ");
  Serial.println(targetPosition);
  Serial.println();

  while (currentPosition != targetPosition)
  {
    currentPosition += direction;
    Serial.print("current: ");
    Serial.println(currentPosition);

    // 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 <= currentPosition) 
      {
        digitalWrite(ledPins[thisLed], HIGH);
      }
      // turn off all pins higher than the ledLevel:
      else 
      {
        digitalWrite(ledPins[thisLed], LOW);
      }
    }

    delay(1000);
    
  }

  direction = -direction;

}