(help) single Array Leds

hello guys, im new to arduino and i have been trying to do a single line of 6 Leds each with increasing Brightness(certain brightness) then move the brightest led (which is the last led of the 6) to cycle among the array
pinArray[] = {3, 5, 6, 9, 10, 11};
^ ^ ^ ^ ^ ^
the brightness of element 5 move to element0 and the element0 was suppose to move to element 1

anyone can kindly help me ? =)
sorry for my poor english

You have a 2nd array that holds the analogWrite value you are sending to the pins in the first array?

no i dont have a 2nd array i only have the PinLed Array

The array he was talking about is an array of variables inside the arduino.It is used to hold the values you write to the hardware.

byte pinArray[] = {3, 5, 6, 9, 10, 11};
byte brightnessArray[]= {0,50,100,150, 200,255;};
byte x;

void setup(){
for (x=0; x<6; x=x+1){
pinMode (pinsArray[x], OUTPUT);
analogWrite (pinsArray[x], 0); // outputs off to start, assuming High out = LED on.
}
void loop(){
// time to update the brightnesses? Or however you determine that.
for (x=0; x<6; x=x+1){
analogWrite (pinArray[x], brightnessArray[x]);
}
// adjust brightnessArray for next time change
...
}

hey thanks for your code up there but your code up there doesn't shift the brightest led

here the version of mine

int pinArray[] = {3, 5, 6, 9, 10, 11};
int brightnessArray[]= {25,50,100,150, 200,255};
int x;


void setup(){
  Serial.begin(9600);
    for (x=0; x<6; x=x+1)
    {
    pinMode (pinArray[x], OUTPUT);
    analogWrite (pinArray[x], 0); 
    }
  }
void loop()
  {
  for (x=0; x<6; x=x+1)
  {
    analogWrite (pinArray[x], brightnessArray[x]);
    delay(550);
    Serial.println(brightnessArray[x]);
   }
  for(x=0; x<6; x=x+1)
  for (int y =5; y>-1;y=-1) 
  {
    analogWrite (pinArray[x], brightnessArray[y]);
    delay(550);
    Serial.println(brightnessArray[y]);
  }
  }
  1. put your code in code tags - read the how to use the forum sticky and edit that last post.
  2. try what Crossroads put and fill in his blanks
  3. ask about any blank you are unsure of.

hey mike, i have tried crossroads code already as i mention above but his code doesn't complete my requirement. i still need the brightest led to move among those leds

brightnessLevel0, brightnessLevel1, brightnessLevel2, brightnessLevel3, brightnessLevel4, brightnessLevel5
then i need to
brightnessLevel5, brightnessLevel0, brightnessLevel1, brightnessLevel2, brightnessLevel3, brightnessLevel4
brightnessLevel4, brightnessLevel5, brightnessLevel3, brightnessLevel2, brightnessLevel1, brightnessLevel0

im sorry if im confusing u guys

tony9349258:
hey mike, i have tried crossroads code already as i mention above but his code doesn't complete my requirement. i still need the brightest led to move among those leds

brightnessLevel0, brightnessLevel1, brightnessLevel2, brightnessLevel3, brightnessLevel4, brightnessLevel5
then i need to
brightnessLevel5, brightnessLevel0, brightnessLevel1, brightnessLevel2, brightnessLevel3, brightnessLevel4
brightnessLevel4, brightnessLevel5, brightnessLevel3, brightnessLevel2, brightnessLevel1, brightnessLevel0

im sorry if im confusing u guys

something like this perhaps?

const byte pinArray[6] = {3, 5, 6, 9, 10, 11};
byte brightnessArray[10]= { 0, 50, 100, 150, 200, 255, 200, 150, 100, 50};

void setup()
{
  Serial.begin(115200);
  for (int i = 0; i < 6; i++)
  {
    pinMode(pinArray[i], OUTPUT);// not necessary using analogWrite
    analogWrite (pinArray[i], 0);
  }
}

void loop()
{
  //update the array
  Serial.println(brightnessArray[0]);
  for (int i = 0; i < 6; i++)
  {
    analogWrite(pinArray[i], brightnessArray[i]);
  }
  shiftMyArray();
  delay(150);
}
  
 void shiftMyArray()
{ 
  byte byteBucket = brightnessArray[0];
  for (int i = 0; i <  9; i++)
  {
    brightnessArray[i] = brightnessArray[i + 1];
  }
  brightnessArray[9] = byteBucket;
}

hey mike, i have tried crossroads code already as i mention above but his code doesn't complete my requirement.

Yes, he left you a bit of a blank to see if you could work it out. He told you what you needed when he said:-

// adjust brightnessArray for next time change
...

So what BulldogLowell showed you was a way of adjusting the values in that array by shifting them one place to the left. He did this using a loop and made sure the original last value ended up round to the very right hand value of the array.

That is a very efficient way of doing it but maybe not one you would come up with yourself. So for extra marks how would you do it there are a few ways, also how would you make it move the other way?

Note the brightness moves round and round, as the bright one falls of the end it appears on the other side.
How can you make it bounce backwards and forwards?

Finally what about having a brightness array that is much longer than the actual number of LEDs you have? What effects could you generate with this.

These are the sorts of things you can play with to learn about coding.

hey, sorry for the late reply
thank you so much guys the code work perfectly
to move the other way is just by changing to

 void shiftMyArray()
{ 
  byte byteBucket = brightnessArray[9];//can u help me with this line and 
  for (int i = 9; i >=0  ; i--)
  {
    brightnessArray[i] = brightnessArray[i - 1];
  }
  brightnessArray[0] = byteBucket;//this line here, how do the two relate ?
}

i cant seem to make it work re bounce

const byte pinArray[] = {3, 5, 6, 9, 10, 11};
byte brightnessArray[]= { 0, 50, 100, 150, 200, 255, 200, 150, 100, 50};

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < 6; i++)
  {
    pinMode(pinArray[i], OUTPUT);// not necessary using analogWrite
    analogWrite (pinArray[i], 0);
  }
}

void loop()
{
  //update the array
  Serial.println(brightnessArray[0]);
  for (int i = 0; i < 6; i++)
  {
    analogWrite(pinArray[i], brightnessArray[i]);    
  }
 shiftMyArrayRebounce();
 shiftMyArray();
}
  


void shiftMyArrayRebounce()
{
  byte byteRebounce = brightnessArray[9];
  for (int i = 9; i >=0; i--)
  {
    brightnessArray[i] = brightnessArray[i-1];
  }
  brightnessArray[0] = byteRebounce;
  delay(150);
}

 void shiftMyArray()
{ 
  byte byteBucket = brightnessArray[0];
  for (int i = 0; i <  9; i++)
  {
    brightnessArray[i] = brightnessArray[i + 1];
  }
  brightnessArray[9] = byteBucket;
}

it will stop moving the led at all

Read your code.
In the loop function you set the LEDs to the required brightness. Then you shift the brightness in one direction and then shift it in the other direction.
Result is it is in exactly the same state as before.
One way round it is to have a variable that indicates if you are shifting left or right and use that in an if statement to decide which of the two shifting functions to call.

hey mike, thanks for your hint on IF statement. i will try it tmr when i come to my school
you guys are so great, thanks for helping me ! :smiley:

Need Help with IF else statement

const byte pinArray[6] = {3, 5, 6, 9, 10, 11};
byte brightnessArray[6]= { 0, 50, 100, 150, 200, 255};
int pos = 0 ;

void setup()
{
  for (int i = 0; i < 6; i++)
  {
    pinMode(pinArray[i], OUTPUT);// not necessary using analogWrite
    analogWrite (pinArray[i], 0);
  }
  Serial.begin(9600);
}

void loop()
{
  for (int i = 0; i < 6; i++)
  {
    analogWrite(pinArray[i], brightnessArray[i]);
  }
  shift();
 delay(150);
}
 
 void shift(){
if( pos < 6)
//shiftMyArrayLeft()
{ 

 byte byteBucket = brightnessArray[0];
  for (int i = 0; i < 6; i++)
  {
    brightnessArray[i] = brightnessArray[i + 1];
    pos = i +1;
  Serial.println(pos); 
   delay(30);
  }
  brightnessArray[5] = byteBucket;
}

  else 
 //shiftMyArrayRight()
{ 
  byte byteBucket = brightnessArray[5];
  for (int i = 7; i >0; i--)
  {
    brightnessArray[i] = brightnessArray[i - 1];
     pos = i - 1;
    Serial.println(pos);
    delay(30);
  }
  brightnessArray[0] = byteBucket;
}
 }

i dont get why my code is not working

Need Help with IF else statement

No you need help with incrementing your pos variable. You only do that once per display of the LEDs.

Why have you stopped using functions for your shift left and right? Using functions breaks up the code and makes it much more easy for us humans to understand.

you are going about this in a strange fashion, so it aces it complicated...

try this:

const byte pinArray[6] = {
  3, 5, 6, 9, 10, 11};
byte brightnessArray[6]= { 
  0, 50, 100, 150, 200, 255};

void setup()
{
  for (int i = 0; i < 6; i++)
  {
    pinMode(pinArray[i], OUTPUT);// not necessary using analogWrite
    analogWrite (pinArray[i], 0);
  }
  Serial.begin(115200);
}

void loop()
{
  Serial.print("Brightness of Zero=");
  Serial.println(brightnessArray[0]);
  for (int i = 0; i < 6; i++)
  {
    analogWrite(pinArray[i], brightnessArray[i]);
  }
  shiftMyArray();
  delay(150);
}

void shiftMyArray()
{
  static int counter;
  if (counter <= 4)
  { 
    byte byteBucket = brightnessArray[0];
    for (int i = 0; i < 5; i++)
    {
      brightnessArray[i] = brightnessArray[i + 1];
      delay(30);
    }
    brightnessArray[5] = byteBucket;
    counter++;
  }
  else if (counter <= 9 )
  { 
    byte byteBucket = brightnessArray[5];
    for (int i = 5; i >= 0; i--)
    {
      brightnessArray[i] = brightnessArray[i-1];
      delay(30);
    }
    brightnessArray[0] = byteBucket;
    counter++;
  }
  if (counter > 9) counter = 0;
}