# Need help writing code

ok so what i am trying to do to help me understand the arduino language is to have 10 LEDs and have a potentiometer move witch led lights up. now here is my code. please be kind it is my first sketch. :'(

``````int potpin = 0;     //sets potentiometer to pin 0
int val     //value of the potentiometer
int led1pin = 1     //first led is in pin 1
int led2pin = 2     //second led is in pin 2
int led3pin = 3     //third led is in pin 3
int led4pin = 4     //forth led is in pin 4
int led5pin = 5     //yeah you know
int led6pin = 6
int led7pin = 7
int led8pin = 8
int led9pin = 9
int led10pin = 10

void setup()
{
``````

am i on the right track. haha hope so. any way i have no idea what next. HELP!!!!!!

OK, so the voltage on an analogue pin gives you a range of 0..1023. So, divide that range by one tenth the range (or by however many LEDs you have), and you're pretty much done.

Probably best to to arrange your LED pins in an array:

const byte LEDpins [] = {1, 2, 3, 4, 5, //you get the idea...};

which will make the turning on and off much easier.

I think something like this could set you on the ‘right’ track.

Resources for the words that might seem difficult:
http://arduino.cc/en/Reference/Const
http://arduino.cc/en/Reference/Array
http://arduino.cc/en/Reference/For
http://arduino.cc/en/Reference/Map

[UNTESTED CODE]

``````const byte NUMBER_OF_LEDS = 10;
const byte ledPins[NUMBER_OF_LEDS] = { 2,3,4,5,6,7,8,9,10,11 }; //connect leds to these pins
const byte potPin = 0; //connect a potentiometer to analog pin 0

void setup(){
pinMode(potPin,INPUT);
//make all ledPins OUTPUT
for (byte i=0; i<NUMBER_OF_LEDS; i++){ //loop from i==0 to i==9
pinMode(ledPins[i],OUTPUT);
}
}

void loop(){
//turn all LEDs off
for (byte i=0; i<NUMBER_OF_LEDS; i++){
digitalWrite(ledPins[i],LOW);
}
//turn the current LED on, based on the position of the potentiometer
digitalWrite(ledPins[currentLED],HIGH);
}
``````

Good luck!

And you could make it even simpler by missing out this line:

``````pinMode(potPin,INPUT);
``````

::)

And you could make it even simpler by missing out this line:

``````pinMode(potPin,INPUT);
``````

::)

Hehe. You could.

But I actually have a reason for leaving it in:

If one uses Arduino on a different uC (now or in the future), you are not guaranteed that it will default to INPUT. Therefore I always do an explicit pinMode :)

let me get this

``````const byte NUMBER_OF_LEDS = 10;
const byte ledPins[NUMBER_OF_LEDS] = { 2,3,4,5,6,7,8,9,10,11 }; //connect leds to these pins
const byte potPin = 0; //connect a potentiometer to analog pin 0
``````

that is telling the arduino what we are going to be using in this project and where.
*i dont understand why we use “const” even after i read the reference

``````void setup(){
pinMode(potPin,INPUT);
//make all ledPins OUTPUT
for (byte i=0; i<NUMBER_OF_LEDS; i++){ //loop from i==0 to i==9
pinMode(ledPins[i],OUTPUT);
}
}
``````

this make the potentiometer an input to arduino, and i think it set the incraments between each LED. right :-/ (by putting “byte” that means that the value range of the potentiometer is 0-255)
*dont 100% understand the usage of the “i”, but is it saying that if i=0 or i=<10 or i=i++ then increase it by incraments of 9.
*what does i=<10 and i=i++ mean?

``````void loop(){
//turn all LEDs off
for (byte i=0; i<NUMBER_OF_LEDS; i++){
digitalWrite(ledPins[i],LOW);
}
//turn the current LED on, based on the position of the potentiometer
digitalWrite(ledPins[currentLED],HIGH);
``````

this i dont not understand at all

one more thing.
THIS IS TO AWOL
*what is the voltage of an analog pin? i dont understand your “0…1023” thing?!?
*And what is the voltage on a digital pin?

“const” is used whenever you don’t intend changing the values specified. It’s short for “constant”. http://arduino.cc/en/Reference/Const
http://arduino.cc/en/Reference/Array

“for” each value of “i” from zero (initialization); while “i” is less than ("<") NUMBER_OF_LEDS (condition); increment (add 1) to “i” (increment).
http://arduino.cc/en/Reference/For

All this means is generate all values of “i” between zero and nine, inclusive.
Using “i” as an index into the array (or table) called “ledPins”, set each pin specified by ledPins to be an output.
NB. it doesn’t say “i <= NUMBER_OF_LEDS”, nor does it say “i = i++”.
Can’t see where you get an increment of 9.
// read the voltage on the analogue input.
byte currentLED = map (analogValue, 0, 1023, 0, (NUMBER_OF_LEDS) - 1);
// take the value in analogValue, which will be between 0 and 1023, and scale it so it becomes in the range 0 to 9.
http://arduino.cc/en/Reference/Map
digitalWrite (ledPins [currentLED], HIGH);
// From ledPins, read the "currentLED"th entry (which is the number of an output pin), and set the corresponding digital output to HIGH.
> *what is the voltage of an analog pin? i dont understand your “0…1023” thing?!?
> *And what is the voltage on a digital pin?
The voltage on an analogue pin is whatever you put on it. Just makes sure it doesn’t exceed the supply voltage of the Arduino, or fall below the Gnd voltage.
The voltage on a digital output pin is nominally the ground potential (usually 0V) for a LOW, and 5V (or the supply voltage) for a HIGH.

Analog Inputs: http://www.arduino.cc/en/Reference/AnalogRead When your potentiometer is connected correctly, you will be able to read a value from 0 to 1023 from the analog pin. This corresponds to a voltage of 0 to 5V. This is a simple tutorial for attaching a pot. http://www.arduino.cc/en/Tutorial/Potentiometer You can then set range for each led to turn on. 10 led's therefore a range of approx 102units per led. (1023/10leds) So your ranges will be: Led1 0-102 Led2 103-205 Led3 206-308 Led4 309-411 and so on. So in your program loop() you will check what the value of the pot is and then you can check which led range it is in, and then turn it on.

The digital pins a pretty simple. Example:http://www.arduino.cc/en/Reference/DigitalWrite First you have to either set them as inputs or outputs. In your case they will be outputs. This is done in the void setup() using pinMode(). Then in your loop(), you can turn the pins on and off using digitalWrite() HIGH or LOW.

Take a look at the examples I linked to above. Also you may want to start up with this. http://www.arduino.cc/en/Tutorial/Blink to get the hang of some stuff. The previous posts showed a simplified way of setting up all your initial settings, but for now I would concentrate on the basics.

Hope this helps. This can be a little confusing when you just start, but stick with it.

thanks guys very much im starting to get this i will pull all of your info together and try to recreate the sketch. thanks Ill Mill you helped clear up some stuff in AWOL’s post.

woo hoo!!! i think im gettin this, cause it only took me like 10 minutes to write (kinda long but not for me). here it is i feel pretty confident with it.

``````const byte ledPin = { 2,3,4,5,6,7,8,9,10,11 }
const byte potPin = 14

void setup()
{
pinmode(potPin, INPUT);
pinmode(ledPin, OUTPUT);
}

void loop()
{
digitalWrite(ledPin),LOW;
//turns all leds off
// reads potpin and scales it from 0-1023 to 0-10 (do i need this?)
//creates currentLED based on position of potpin
digitalWrite(ledPin[currentLED],HIGH;
//sets current led to high
}
``````

You’re probably wondering about those error messages.

``````const byte ledPin [] = { 2,3,4,5,6,7,8,9,10,11 };
const byte potPin = 14;
``````

Note, semicolons and square brackets. “ledPin” is an array.

`````` pinMode(potPin, INPUT);
pinMode(ledPin, OUTPUT);
``````

Note, case.

``````digitalWrite(ledPin,LOW);
``````

Parentheses.

``````var (analogRead (potPin),0,1023,0,10);
``````

“var”?
int var = map (analogRead (potPin),0,1023,0,9);

Note, range 0…9

``````digitalWrite(ledPin[currentLED],HIGH);
``````

Parentheses.

``````const byte ledPin [] = { 2,3,4,5,6,7,8,9,10,11 };
const byte potPin = 14;
#define N_LEDS (sizeof (ledPin) / sizeof (ledPin[0]))

void setup()
{
pinMode(potPin, INPUT);
for (int i = 0; i < N_LEDS; ++i) {
pinMode(ledPin [i], OUTPUT);
}
}

void loop()
{
for (int i = 0; i < N_LEDS; ++i) {
digitalWrite(ledPin [i],LOW);
}
//creates currentLED based on position of potpin
digitalWrite(ledPin[currentLED],HIGH);
//sets current led to high
}
``````

NB, uncompiled, unchecked.

I’ve corrected and commented your code.
You are definitly getting the hang of this, just keep coding. And study the code that you find.

//you’ll need to let the compiler know that this is an array by using after the variable name
const byte ledPin = { 2,3,4,5,6,7,8,9,10,11 }; //remember the semicolon
const byte potPin = 0;//digital pin 14 == analog pin 0, remember semicolon

/*
Arduino is case-sensitive, so it does not know of any function called pinmode.
You might say: ‘But, I’ve seen the pinmode function before!’
Arduino replies: ‘I’ve got no pinmode function, but I do have a pinMode function, does that help?’
[Who knew Arduino was polite too!?]

There is also an issue with your output declaration. The statement: pinmode(ledPin,OUTPUT); will not do what you think,
if the ledPin is an array.
You’ll need a for loop in order to pinMode all pins
*/
void setup()
{
pinMode(potPin, INPUT);

//set all pins as output
for (byte index=0; index<10; index++){
pinMode(ledPin[index], OUTPUT);
}
}

/*
This: digitalWrite(ledPin),LOW; will not behave as you want it to.
Again, you’ll need a for loop in order to index all elements in an array
*/
void loop()
{
//digitalWrite(ledPin),LOW;
//set all pins as output
for (byte index=0; index<10; index++){
digitalWrite(ledPin[index], LOW);
}

/ignore this//var (analogRead (potPin),0,1023,0,10); //this does nothing/

//reads potpin and scales it from 0-1023 to 0-10 (do i need this?)
//-YES, you need it because you have 10 elements in your array (i.e 10 LEDs), not 1023.
//creates currentLED based on position of potpin
byte currentLED = map(analogRead(potPin),0,1023,0,9);//potpin must be potPin, I corrected it

//set the led corresponding to the currentLED index HIGH.
//if your pot is turned halfway you’ll get an analogRead of approx. 512
//this will scale to 4 or 5
//the value at index 4 or 5 of the ledPin array is either SIX or SEVEN, this means either pin SIX OR SEVEN is HIGH
//thus setting the middle led on, creating the effect you want
digitalWrite(ledPin[currentLED],HIGH);//remember the closing paranthesis
}

Did you try this code: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245124714/0#2 ?
AWOL beat me to it :)[/edit]

well at first i didnt understand why you used the ‘‘for loop’’ but i researched and now i understand. its a way of referring to an array. and telling it to apply your statement to every led (or object) in your array ONE AT A TIME.
correct?

well i dont have my arduino yet. but just getting an early start. and i guess that code is basicly what i just redid. for some practice can you make up a project that uses the skills that i have learned here today. PLEASE!!! ;D haha

well at first i didnt understand why you used the ''for loop'' but i researched and now i understand. its a way of referring to an array. and telling it to apply your statement to every led (or object) in your array ONE AT A TIME. correct?

Spot on my friend :)