Easen.co.uk The home of Marc Easen

28Dec/111

My first Arduino sketch – Playing about with 8 LEDs

Well it's about time I've got back into electronics, I've spent a bit too long writing software and just ignoring how the underlying technology works. Here's my first Arduino sketch, pretty simple really. It switches on 8 LEDs in a simple counting pattern

Were 'X' is a solid LED and the numbers represent the other LEDs being turned on after a given delay (1  second divided by the remaining LEDs).

 

(1) (2) (3) (4) (5) (6) (7) (8)

(x) (1) (2) (3) (4) (5) (6) (7)

(x) (x) (1) (2) (3) (4) (5) (6)

...

(x) (x) (x) (x) (x) (x) (x) (x)

 

Code:

  1. int ledPins[] = {2,3,4,5,6,7,8,9};
  2. int numberOfLeds = 8;
  3.  
  4. void setup()
  5. {
  6.   for(int i = 0; i < 8; i++){
  7.       pinMode(ledPins[i], OUTPUT);
  8.   }
  9. }
  10.  
  11. void loop()
  12. {
  13.   for (int i = 0; i < 8; i++) {
  14.     countLeds(i);
  15.   }
  16. }
  17.  
  18. void countLeds(int starting)
  19. {
  20.   // Switch them off
  21.   for(int i = starting; i < numberOfLeds; i++) {
  22.     digitalWrite(ledPins[i], LOW);
  23.   }
  24.  
  25.   // Switch on
  26.   for(int i = starting; i < numberOfLeds; i++) {
  27.       digitalWrite(ledPins[i], HIGH);
  28.       delay(1000 / (numberOfLeds - starting));
  29.   }
  30. }
int ledPins[] = {2,3,4,5,6,7,8,9};
int numberOfLeds = 8;

void setup()
{
  for(int i = 0; i < 8; i++){
      pinMode(ledPins[i], OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < 8; i++) {
    countLeds(i);
  }
}

void countLeds(int starting)
{
  // Switch them off
  for(int i = starting; i < numberOfLeds; i++) {
    digitalWrite(ledPins[i], LOW);
  }

  // Switch on
  for(int i = starting; i < numberOfLeds; i++) {
      digitalWrite(ledPins[i], HIGH);
      delay(1000 / (numberOfLeds - starting));
  }
}

 

Nothing special really, but I quite enjoyed getting back to basics. Watch this space for more Arduino hackery.

Comments (1) Trackbacks (0)
  1. I hope other folks consider your post listed here as helpful as I have. I manage a blog site myself and would be delighted for you or the visitors on your web site to visit. Please feel free to look through my website like I have with your own and post a remark or two if you find anything interesting. Thanks again.


Leave a comment

(required)

No trackbacks yet.