Go Down

Topic: LED Chasing Lights Progamming (Read 2 times) previous topic - next topic

Fennel Rye

Quote
for (byte j = 0; j < 14; j++)
  doCircle (20);


Little confused about bytes.  Where are you getting the names 'i' and 'j' from for these bytes?  I understand that they are there to store a number, but I'm getting confused on where the letters are coming from.
Fennel Rye

Nick Gammon

Try this, it fades a group of LEDs on and off. After that do a bit of experimenting. :)

Code: [Select]
const int numberOfLEDs = 14;

volatile byte wantedPins [numberOfLEDs] =  { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 };   // which pin to pulse on and off

const byte PWMiterations = 40;
byte PWMdutyCycle;   // <-- should be less than PWMiterations

volatile byte pwmCount;

ISR(TIMER1_COMPA_vect)
{
byte i;

  if (pwmCount > PWMdutyCycle)
    {
    // turn all off
    for (i = 0; i < numberOfLEDs; i++)
      digitalWrite(i, LOW);     
    }
  else
    {
    // turn wanted ones on
    for (i = 0; i < numberOfLEDs; i++)
      if (wantedPins [i])
        digitalWrite(i, HIGH);     
    }
   
   
if (++pwmCount >= PWMiterations)
   pwmCount = 0;
}

void setup()
  {               
  for (byte i = 0; i < numberOfLEDs; i++) 
    pinMode(i, OUTPUT);
   
  // set up Timer 1
  TCCR1A = 0;          // normal operation
  TCNT1 = 0;           // make sure we start at zero
  TCCR1B = _BV(WGM12) | _BV(CS10) | _BV (CS12);   // CTC, scale to clock / 1024
  OCR1A =  5;       // compare A register value (1000 * clock speed / 1024)
  TIMSK1 = _BV (OCIE1A);             // interrupt on Compare A Match
   
  }  // end of setup


void loop()
  {
  for (PWMdutyCycle = 0; PWMdutyCycle < PWMiterations; PWMdutyCycle++)
    delay (100);

  for (PWMdutyCycle = PWMiterations; PWMdutyCycle > 0; PWMdutyCycle--)
    delay (100);
   
  } // end of loop



Nick Gammon

Quote
Where are you getting the names 'i' and 'j' from for these bytes?



Code: [Select]
for (byte j = 0; j < 14; j++)

That declares "j" as in:

Code: [Select]
byte j

Fennel Rye

But what is byte j? Are you creating and naming a byte with that declaration or is byte j something that already exists that you are calling upon? 
Fennel Rye

Nick Gammon

It is created at that point as a loop variable.

Go Up