Yet another FNG

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Yet another FNG

Ok, basics: I am a newbie, duh. Just getting my first glimps of the worlds of possibility contained within Arduino and such. I have in my possession: Adafruits experimenters kit (ARDX), Arduino Cookbook (2nd Ed.), and a decent background in electrical troubleshooting and theory. The Navy trained me how to follow diagrams and wires to make things ping and chirp on command but very rarely delved into the programming of why when I push this button XYZ happens as a result. I can usually track the hardware faults, now I want to learn some of the software side.

Basics done, thanks for the patience this far.

I made the blinky light. Yay for me. I made a motor turn and a piezo speaker go on and off when I shone a flashlight on a sensor. What I am working on now is why they do such things. So, I decided to branch out. I can make one led blink, how about more than one? How do I tell my Arduino that there are three leds to blink on and off instead of one? There are code examples that I can plug in and upload in moments, but that wont get me to the root of why.

So, here is a picture of my current setup and the sketch that I thought should work. All i get in one single blinking led (red). Now, I can simply wire the three leds out of one pin, but I'm trying to understand the software/code/sketch aspect of this issue.

Also, under the setup function, I thought that I needed to tell it to intialize three pins as output so I told it start at zero and go until i<2 so that 0, 1, 2, got setup. That resulted in a dimly lit red led. However, when I copied a different example and wound up with i<3 the red led goes back up to full strength.

12 - 1

Code: Select all
`int ledPins[] = {4,7,9};void setup() {                  for(int i = 0; i < 3; i++){  pinMode(ledPins[i], OUTPUT);       }}void loop() {  digitalWrite(ledPins[0,1,2], HIGH);  delay(250);                digitalWrite(ledPins[0,1,2], LOW);      delay(250);               }`
Atomic Buddhist

Posts: 2
Joined: Sat Aug 11, 2012 8:21 pm

Re: Yet another FNG

I told it start at zero and go until i<2 so that 0, 1, 2, got setup.

the "i<2" means that i never gets to 2. You need "i<=2" or (as you have discovered) "i<3".

This line will not work as intended:
Code: Select all
`  digitalWrite(ledPins[0,1,2], HIGH);`

With digitalWrite, you can only set one pin at a time as in:

Code: Select all
`  digitalWrite(ledPins[0], HIGH);  digitalWrite(ledPins[1], HIGH);  digitalWrite(ledPins[2], HIGH);`

You can use direct port manipulation to write multiple pins simultaneously: http://www.arduino.cc/en/Reference/PortManipulation

You might want to take a look through the Arduino Tutorials. They take you step-by-step through several examples - including some led sequencing in lesson 5.

Posts: 24016
Joined: Sat Feb 07, 2009 9:11 am

Re: Yet another FNG

How did I miss seeing the tutorials? Wow. Ok, lots of reading material.

So, the pins can be initialized as a group
Code: Select all
`int ledPins[] = {4,7,9};`

but must be turned on and off individually...ok, will research port manipulation and start working through the tutorials.

Thanks for the quick help.
Atomic Buddhist

Posts: 2
Joined: Sat Aug 11, 2012 8:21 pm

Re: Yet another FNG

So, the pins can be initialized as a group

Code: Select all
`    int ledPins[] = {4,7,9};`

Actually, that is just an array declaration. It doesn't affect the pins directly.

You initialize the pin modes one at a time in your loop:

Code: Select all
`  for(int i = 0; i < 3; i++){  pinMode(ledPins[i], OUTPUT);       }`