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:
- 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));
- }
- }
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.
December 30th, 2011 - 03:22
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.