Go Down

Topic: Light leds using an array (Read 2287 times) previous topic - next topic

worldisorder

My C is very rusty and I'm trying to light a row of leds one at a time with a common ground. Any help is appreciated.
Code: [Select]

int ledPin0 =  13;    // LED connected to gnd digital pin 13

int ledR1 [8] = { 9, 3, 2, 12, 15, 11, 7, 6}; // LED connected to vcc for digital pin 13

int vcc = 255;
int gnd = 0;

// The setup() method runs once, when the sketch starts

void setup()   {                
 // initialize the digital and analog pins as an output:
pinMode(ledPin0, OUTPUT);
pinMode(9, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(12, OUTPUT);
pinMode(15, OUTPUT);
pinMode(11, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);

}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                    
{  
 
 digitalWrite(ledPin0, LOW);   // set common ground for row 1 (pin 13)  

 for (int i=0; i < 8; i++)
 {
  if(i=4){                               //Analog pin 15 is controlled by analogWrite function
     analogWrite(ledR1[i], vcc);
     delay(100);
     analogWrite(ledR1[i], gnd);  
  }
     
   else{                         //light up the rest of the leds
       digitalWrite(ledR1[i], HIGH);    // set the vcc to turn on 1,i
       delay(100);          // wait for a second
       digitalWrite(ledR1[i], LOW);   // set the gnd to turn off 1,i
       delay(100);
       }
 }


I have it working with a whole bunch of repetition and I'm just trying to minimize this code:
Code: [Select]

int ledPin0 =  13;    // LED connected to gnd digital pin 13

int ledPin1 =  9; // LED connected to vcc for digital pin 13
int ledPin3 =  3;
int ledPin5 =  2;
int ledPin7 =  12;
int ledPin9 =  15;
int ledPin11 =  11;
int ledPin13 =  7;
int ledPin15 = 6;

int vcc = 255;
int gnd = 0;

// The setup() method runs once, when the sketch starts

void setup()   {                
 // initialize the digital pin as an output:

}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                    
{
 pinMode(ledPin0, OUTPUT);
 pinMode(ledPin1, OUTPUT);
 digitalWrite(ledPin0, LOW);   // set common ground for row 1 (pin 13)  
 digitalWrite(ledPin1, HIGH);    // set the vcc to turn on 1,1 (pin 9)
 delay(100);          // wait for a second
 digitalWrite(ledPin1, LOW);   // set the gnd to turn off 1,1 (pin 9)
 delay(100);
 
 pinMode(ledPin3, OUTPUT);
 digitalWrite(ledPin3, HIGH);    // set the vcc for the LED
 delay(100);          // wait for a second
 digitalWrite(ledPin3, LOW); //
 delay(100);
 
 pinMode(ledPin5, OUTPUT);
 digitalWrite(ledPin5, HIGH);    // set the Ground for the LED
 delay(100);          // wait for a second
 digitalWrite(ledPin5, LOW);
 delay(100);
 
 pinMode(ledPin7, OUTPUT);
 digitalWrite(ledPin7, HIGH);    // set the Ground for the LED
 delay(100);          // wait for a second
 digitalWrite(ledPin7, LOW);
 delay(100);
 
 pinMode(ledPin9, OUTPUT);
 analogWrite(ledPin9, vcc);    // set the  for the LED
 delay(100);          // wait for a second
 analogWrite(ledPin9, gnd);
 delay(100);
 
 pinMode(ledPin11, OUTPUT);
 digitalWrite(ledPin11, HIGH);    // set the Ground for the LED
 delay(100);          // wait for a second
 digitalWrite(ledPin11, LOW);
 delay(100);
 
 pinMode(ledPin13, OUTPUT);
 digitalWrite(ledPin13, HIGH);    // set the Ground for the LED
 delay(100);          // wait for a second
 digitalWrite(ledPin13, LOW);
 delay(100);
 
 pinMode(ledPin15, OUTPUT);
 digitalWrite(ledPin15, HIGH);    // set the Ground for the LED
 delay(100);          // wait for a second
 digitalWrite(ledPin15, LOW);
 delay(100);
 
}

PaulS

That is pretty easy.

Code: [Select]

int ledPins[] = {9, 3, 2, 12, 15, 11, 7, 6}; // LED pins
int ledCnt = 8;

void setup()
{
   for(int p=0; p<lenCnt; p++)
   {
       pinMode(ledPins[p], OUTPUT); // Set the mode to OUTPUT
   }
}

void loop()
{
   for(int p=0; p<ledCnt; p++)
   {
       analogWrite(ledPins[p], HIGH); // Turn an LED pin on
       delay(100);   // Twiddle thumbs for a while
       analogWrite(ledPins[p], LOW); // Turn an LED pin off
       delay(100);   // Twiddle thumbs some more
   }
}


Inside the for block in the loop function, you can take special actions for specific pins, if needed.

Remember, though, that "if(i = 4)" is always true, while "if(i == 4)" is only sometimes true.

Do you have the common connection for all the LEDs connected to a pin? If so, why? The common connection should be to ground.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy