Arduino Forum

Forum 2005-2010 (read only) => Hardware => Interfacing => Topic started by: Jeremy1998 on Jun 15, 2010, 05:16 am

Title: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 05:16 am
I am trying to use a potentiometer to control a servo and a line of leds... the part that i need help with is the graph... i am going to have an led for every 5 degrees... i dont know how to average the values and then light the leds... please descrbe to me what i need to add...

also, can you  guys recommend some dip packages of transistors... i need at least 36 transistors total...

Code: [Select]
#include <Servo.h>

int val;

int potPin = 0;

Servo servoPin;

int LED1 = 54;   // 5    Degrees LED
int LED2 = 53;   // 10   Degrees LED
int LED3 = 52;   // 15   Degrees LED
int LED4 = 51;   // 20   Degrees LED
int LED5 = 50;   // 25   Degrees LED
int LED6 = 49;   // 30   Degrees LED
int LED7 = 48;   // 35   Degrees LED
int LED8 = 47;   // 40   Degrees LED
int LED9 = 46;   // 45   Degrees LED
int LED10 = 45;  // 50   Degrees LED
int LED11 = 44;  // 55   Degrees LED
int LED12 = 43;  // 60   Degrees LED
int LED13 = 42;  // 65   Degrees LED
int LED14 = 41;  // 70   Degrees LED
int LED15 = 40;  // 75   Degrees LED
int LED16 = 39;  // 80   Degrees LED
int LED17 = 38;  // 85   Degrees LED
int LED18 = 37;  // 90   Degrees LED
int LED19 = 36;  // 95   Degrees LED
int LED20 = 35;  // 100  Degrees LED
int LED21 = 34;  // 105  Degrees LED  
int LED22 = 33;  // 110  Degrees LED
int LED23 = 32;  // 115  Degrees LED
int LED24 = 31;  // 120  Degrees LED
int LED25 = 30;  // 125  Degrees LED
int LED26 = 29;  // 130  Degrees LED
int LED27 = 28;  // 135  Degrees LED
int LED28 = 27;  // 140  Degrees LED
int LED29 = 26;  // 145  Degrees LED
int LED30 = 25;  // 150  Degrees LED
int LED31 = 24;  // 155  Degrees LED
int LED32 = 23;  // 160  Degrees LED
int LED33 = 22;  // 165  Degrees LED
int LED34 = 21;  // 170  Degrees LED
int LED35 = 20;  // 175  Degrees LED
int LED36 = 19;  // 180  Degrees LED

void setup()
{
pinMode(potPin, INPUT);
 
servoPin.attach(13);
 
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT);
pinMode(LED13, OUTPUT);
pinMode(LED14, OUTPUT);
pinMode(LED15, OUTPUT);
pinMode(LED16, OUTPUT);
pinMode(LED17, OUTPUT);
pinMode(LED18, OUTPUT);
pinMode(LED19, OUTPUT);
pinMode(LED20, OUTPUT);
pinMode(LED21, OUTPUT);
pinMode(LED22, OUTPUT);
pinMode(LED23, OUTPUT);
pinMode(LED24, OUTPUT);
pinMode(LED25, OUTPUT);
pinMode(LED26, OUTPUT);
pinMode(LED27, OUTPUT);
pinMode(LED28, OUTPUT);
pinMode(LED29, OUTPUT);
pinMode(LED30, OUTPUT);
pinMode(LED31, OUTPUT);
pinMode(LED32, OUTPUT);
pinMode(LED33, OUTPUT);
pinMode(LED34, OUTPUT);
pinMode(LED35, OUTPUT);
pinMode(LED36, OUTPUT);
}

void loop()
{
val = analogRead(potPin);
val = map(val, 0, 1023, 0, 180);
servoPin.write(val);
}


Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 06:06 am
plz help me out...
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 07:01 am
i dont really understand that
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: raron on Jun 15, 2010, 07:32 am
You should really look into arrays http://arduino.cc/en/Reference/Array.

You could do another map() function, to map the 180 degrees to your 36 LEDs. No need to average the value (ok, it might be needed, but not for converting to the 36 LED graph). You only need to divide by the number of LED's (which is what the map() function does).
Code: [Select]
upperLed = map(val, 0, 1023, 1, 36);

Essentially that was what Richard Crowley did (if I'm not mistaken), except he took the output pin directly, so it looks more complicated. But it was possible since all your LED's was in sequental order on the output pins, only backwards from 54.

Then you light all the LEDs up to "upperLed". Or perhaps only that LED, depends on what you want. This is another place were arrays would be very handy, or else you are stuck to making another long list of some conditional statements (or switch statements).
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 08:14 am
im only 11 years ond... i dont know alot about the software side of arduino... i just dont understand that example... sorry for freaking out...

as for raron:

so i do a val1 and val2, and them map val2 to 0 and 36? than what do i do?

if i understand, i would do some sort of if val2 ==(insert val2 number here) and the do a digital write high?
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: raron on Jun 15, 2010, 08:33 am
Something like that, yes.

Except map it from 1 to 36. If you use arrays, you can use from 0 to 35 (as arrays are so called "zero based" and begin with element nr. zero)

Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 08:40 am
well, i just wrote the code, and it compiled! :)

i can't trst it rigth now, but can someone tell me if it SHOULD work?

Code: [Select]
#include <Servo.h>

int val1;
int val2;

int potPin = 0;

Servo servoPin;

int LED1 = 54;   // 5    Degrees LED
int LED2 = 53;   // 10   Degrees LED
int LED3 = 52;   // 15   Degrees LED
int LED4 = 51;   // 20   Degrees LED
int LED5 = 50;   // 25   Degrees LED
int LED6 = 49;   // 30   Degrees LED
int LED7 = 48;   // 35   Degrees LED
int LED8 = 47;   // 40   Degrees LED
int LED9 = 46;   // 45   Degrees LED
int LED10 = 45;  // 50   Degrees LED
int LED11 = 44;  // 55   Degrees LED
int LED12 = 43;  // 60   Degrees LED
int LED13 = 42;  // 65   Degrees LED
int LED14 = 41;  // 70   Degrees LED
int LED15 = 40;  // 75   Degrees LED
int LED16 = 39;  // 80   Degrees LED
int LED17 = 38;  // 85   Degrees LED
int LED18 = 37;  // 90   Degrees LED
int LED19 = 36;  // 95   Degrees LED
int LED20 = 35;  // 100  Degrees LED
int LED21 = 34;  // 105  Degrees LED  
int LED22 = 33;  // 110  Degrees LED
int LED23 = 32;  // 115  Degrees LED
int LED24 = 31;  // 120  Degrees LED
int LED25 = 30;  // 125  Degrees LED
int LED26 = 29;  // 130  Degrees LED
int LED27 = 28;  // 135  Degrees LED
int LED28 = 27;  // 140  Degrees LED
int LED29 = 26;  // 145  Degrees LED
int LED30 = 25;  // 150  Degrees LED
int LED31 = 24;  // 155  Degrees LED
int LED32 = 23;  // 160  Degrees LED
int LED33 = 22;  // 165  Degrees LED
int LED34 = 21;  // 170  Degrees LED
int LED35 = 20;  // 175  Degrees LED
int LED36 = 19;  // 180  Degrees LED

void setup()
{
pinMode(potPin, INPUT);
 
servoPin.attach(13);
 
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT);
pinMode(LED13, OUTPUT);
pinMode(LED14, OUTPUT);
pinMode(LED15, OUTPUT);
pinMode(LED16, OUTPUT);
pinMode(LED17, OUTPUT);
pinMode(LED18, OUTPUT);
pinMode(LED19, OUTPUT);
pinMode(LED20, OUTPUT);
pinMode(LED21, OUTPUT);
pinMode(LED22, OUTPUT);
pinMode(LED23, OUTPUT);
pinMode(LED24, OUTPUT);
pinMode(LED25, OUTPUT);
pinMode(LED26, OUTPUT);
pinMode(LED27, OUTPUT);
pinMode(LED28, OUTPUT);
pinMode(LED29, OUTPUT);
pinMode(LED30, OUTPUT);
pinMode(LED31, OUTPUT);
pinMode(LED32, OUTPUT);
pinMode(LED33, OUTPUT);
pinMode(LED34, OUTPUT);
pinMode(LED35, OUTPUT);
pinMode(LED36, OUTPUT);
}

void loop()
{
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 0, 180);
val2 = map(val2, 0, 1023, 0, 36);

servoPin.write(val1);

if (val2 >= 1){              
 digitalWrite(LED1, HIGH);
}
else{
 digitalWrite(LED1, LOW);
}

 if (val2 >= 2){              
 digitalWrite(LED2, HIGH);
}
else{
 digitalWrite(LED2, LOW);
}

 if (val2 >= 3){              
 digitalWrite(LED3, HIGH);
}
else{
 digitalWrite(LED3, LOW);
}

 if (val2 >= 4){              
 digitalWrite(LED4, HIGH);
}
else{
 digitalWrite(LED4, LOW);
}

 if (val2 >= 5){              
 digitalWrite(LED5, HIGH);
}
else{
 digitalWrite(LED5, LOW);
}

if (val2 >= 6){              
 digitalWrite(LED6, HIGH);
}
else{
 digitalWrite(LED6, LOW);
}

if (val2 >= 7){              
 digitalWrite(LED7, HIGH);
}
else{
 digitalWrite(LED7, LOW);
}

if (val2 >= 8){              
 digitalWrite(LED8, HIGH);
}
else{
 digitalWrite(LED8, LOW);
}

if (val2 >= 9){              
 digitalWrite(LED9, HIGH);
}
else{
 digitalWrite(LED9, LOW);
}

if (val2 >= 10){              
 digitalWrite(LED10, HIGH);
}
else{
 digitalWrite(LED10, LOW);
}

if (val2 >= 11){              
 digitalWrite(LED11, HIGH);
}
else{
 digitalWrite(LED11, LOW);
}

if (val2 >= 12){              
 digitalWrite(LED12, HIGH);
}
else{
 digitalWrite(LED12, LOW);
}

if (val2 >= 13){              
 digitalWrite(LED13, HIGH);
}
else{
 digitalWrite(LED13, LOW);
}

if (val2 >= 14){              
 digitalWrite(LED14, HIGH);
}
else{
 digitalWrite(LED14, LOW);
}

if (val2 >= 15){              
 digitalWrite(LED15, HIGH);
}
else{
 digitalWrite(LED15, LOW);
}

if (val2 >= 16){              
 digitalWrite(LED16, HIGH);
}
else{
 digitalWrite(LED16, LOW);
}
if (val2 >= 17){              
 digitalWrite(LED17, HIGH);
}
else{
 digitalWrite(LED17, LOW);
}

if (val2 >= 18){              
 digitalWrite(LED18, HIGH);
}
else{
 digitalWrite(LED18, LOW);
}

if (val2 >= 19){              
 digitalWrite(LED19, HIGH);
}
else{
 digitalWrite(LED19, LOW);
}

if (val2 >= 20){              
 digitalWrite(LED20, HIGH);
}
else{
 digitalWrite(LED20, LOW);
}

if (val2 >= 21){              
 digitalWrite(LED21, HIGH);
}
else{
 digitalWrite(LED21, LOW);
}

if (val2 >= 22){              
 digitalWrite(LED22, HIGH);
}
else{
 digitalWrite(LED22, LOW);
}

if (val2 >= 23){              
 digitalWrite(LED23, HIGH);
}
else{
 digitalWrite(LED23, LOW);
}

if (val2 >= 24){              
 digitalWrite(LED24, HIGH);
}
else{
 digitalWrite(LED24, LOW);
}

if (val2 >= 25){              
 digitalWrite(LED25, HIGH);
}
else{
 digitalWrite(LED25, LOW);
}

if (val2 >= 26){              
 digitalWrite(LED26, HIGH);
}
else{
 digitalWrite(LED26, LOW);
}

if (val2 >= 27){              
 digitalWrite(LED27, HIGH);
}
else{
 digitalWrite(LED27, LOW);
}

if (val2 >= 28){              
 digitalWrite(LED28, HIGH);
}
else{
 digitalWrite(LED28, LOW);
}

if (val2 >= 29){              
 digitalWrite(LED29, HIGH);
}
else{
 digitalWrite(LED29, LOW);
}

if (val2 >= 30){              
 digitalWrite(LED30, HIGH);
}
else{
 digitalWrite(LED30, LOW);
}

if (val2 >= 31){              
 digitalWrite(LED31, HIGH);
}
else{
 digitalWrite(LED31, LOW);
}

if (val2 >= 32){              
 digitalWrite(LED32, HIGH);
}
else{
 digitalWrite(LED32, LOW);
}

if (val2 >= 33){              
 digitalWrite(LED33, HIGH);
}
else{
 digitalWrite(LED33, LOW);
}

if (val2 >= 34){              
 digitalWrite(LED34, HIGH);
}
else{
 digitalWrite(LED34, LOW);
}

if (val2 >= 35){              
 digitalWrite(LED35, HIGH);
}
else{
 digitalWrite(LED35, LOW);
}

if (val2 >= 36){              
 digitalWrite(LED36, HIGH);
}
else{
 digitalWrite(LED36, LOW);
}
}
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 08:48 am
Please, for the sake of my poor scrolling finger, learn about arrays!
If you find you're writing the same thing over again, it's time for a  "for" loop and an array.

Don't do this:
Code: [Select]
  pinMode(potPin, INPUT);

Why this:
Code: [Select]
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 0, 180);
val2 = map(val2, 0, 1023, 0, 36);
?
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 08:53 am
ok then? do i just not describe the potPin then, or what do i do?

and i have always wanted to learn, but i never did...
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: raron on Jun 15, 2010, 08:56 am
If you insist on using the long list method (which is OK, if it works it works, and thats what counts. But really, arrays would make this much more readable, shorter and elegant)

half code half descriptions (pseudo code):
Code: [Select]
void loop()
{
val = analogRead(potPin);
val = map(val, 0, 1023, 0, 180);
servoPin.write(val);

val2 = map(val, 0, 1023, 1, 36); // This will be your LED nr


// Turn all LEDs off (simplest, but another long list)
  digitalWrite(LED1,LOW)
  digitalWrite(LED2,LOW)
  digitalWrite(LED3,LOW)
.
. all the way to
.
  digitalWrite(LED36,LOW)

// Now turn on one LED. If you want a bar-graph type, turn this list upside-down (36 to 1) and get rid of the break commands.
switch (val2) {
 case 1:
  digitalWrite(LED1,HIGH)
   break;
 case 2:
  digitalWrite(LED2,HIGH)
   break;
.
. all the way to
.
 case 36:
  digitalWrite(LED36,HIGH)
   break;
 default:
}

}


you would also need to turn the other LEDs off in between. Simplest is to turn all off in the loop, it would be indistinguishable because it is so fast. You will not see them blink.

I'm not sure if you just want one LED on at a time, or all the LEDs up to the degree of the servo, so modify as you want (of course).


You are quick :P I wrote this before I saw your next post. I think it should work (except map from 1 to 36, not 0 to 36).
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 08:59 am
why 1 - 36?

i would think 0 - 36 so that when the pot is at 0 degrees, no led's are on... with 1 - 36, the 5 degree led would always be on, right?

EDIT: to be honest, i would like to clean it up... can you guys guide me through it?
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 09:03 am
Quote
do i just not describe the potPin then, or what do i do?


"potPin" refers to an analogue input, but "pinMode" relates to digital pins.

If you do "pinMode (potPin, INPUT);", what you're really saying is "pinMode (0, INPUT);", and digital pin zero is serial Rx.
This won't cause you a problem here, but it's a bad habit to acquire.

BTW, I just rewrote your sketch with an array.
Nicely formatted, it comes in at 31 lines.
Remember, smaller programs give the bugs fewer dark corners to hide in.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: raron on Jun 15, 2010, 09:10 am
Example. Instead of

Code: [Select]
int LED1 = 54;   // 5    Degrees LED
int LED2 = 53;   // 10   Degrees LED
int LED3 = 52;   // 15   Degrees LED
int LED4 = 51;   // 20   Degrees LED
int LED5 = 50;   // 25   Degrees LED
int LED6 = 49;   // 30   Degrees LED
int LED7 = 48;   // 35   Degrees LED
int LED8 = 47;   // 40   Degrees LED
int LED9 = 46;   // 45   Degrees LED
int LED10 = 45;  // 50   Degrees LED
int LED11 = 44;  // 55   Degrees LED
int LED12 = 43;  // 60   Degrees LED
int LED13 = 42;  // 65   Degrees LED
int LED14 = 41;  // 70   Degrees LED
int LED15 = 40;  // 75   Degrees LED
int LED16 = 39;  // 80   Degrees LED
int LED17 = 38;  // 85   Degrees LED
int LED18 = 37;  // 90   Degrees LED
int LED19 = 36;  // 95   Degrees LED
int LED20 = 35;  // 100  Degrees LED
int LED21 = 34;  // 105  Degrees LED
int LED22 = 33;  // 110  Degrees LED
int LED23 = 32;  // 115  Degrees LED
int LED24 = 31;  // 120  Degrees LED
int LED25 = 30;  // 125  Degrees LED
int LED26 = 29;  // 130  Degrees LED
int LED27 = 28;  // 135  Degrees LED
int LED28 = 27;  // 140  Degrees LED
int LED29 = 26;  // 145  Degrees LED
int LED30 = 25;  // 150  Degrees LED
int LED31 = 24;  // 155  Degrees LED
int LED32 = 23;  // 160  Degrees LED
int LED33 = 22;  // 165  Degrees LED
int LED34 = 21;  // 170  Degrees LED
int LED35 = 20;  // 175  Degrees LED
int LED36 = 19;  // 180  Degrees LED



do this:

Code: [Select]

int ledPin[35];  // 0 - 35, 36 LEDs


void setup()
{
 int ledPinNr = 54;

 for (int ledNr=0; ledNr<=36; ledNr++)
 {
   ledPin[ledNr] = ledPinNr-- ;
 }
}


Should be the same thing unless I missed something (and as you have your output pins in sequence).
Then you would need to use it like this:
digitalWrite(ledPin[LEDnr],state);
were the "LEDnr" needs to be one less (it starts at 0), and can be a constant (a number), or a variable (in a loop, for example).

Also, you may be right about that 0-36 :) Experiment
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 09:15 am
Quote
Also, you may be right about that 0-36:)Experiment


well, i don't wanna argue with the master ;) LOL JK JK

on a more serious note:
how do i do the series with my desired pins (54 - 19)
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 09:23 am
Code: [Select]
int ledPin[35];  // 0 - 35, 36 LEDs


BZZZZT!
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 09:25 am
so that will use it as my desired pins??? that makes me think it does pins 0 - 35...
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: raron on Jun 15, 2010, 09:28 am
Hehe. No master I'm afraid, I'm an extremely slow learner :p I have to look up syntax all the time, especially if it's been a while since last time.

Btw seems like Groove has figured out a sketch for you :)

Quote
how do i do the series with my desired pins (54 - 19)

Not sure I understand what you mean. I showed an example of assigning lednr to a "ledPin[]" array (untested though, so I hope it works). If there is some syntax you are unsure of there is the extended reference (http://arduino.cc/en/Reference/Extended) section to look at.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 09:29 am
Zero to thirty-five is a thirty-six element array.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 09:31 am
well, i mean that the series thing makes me think that the leds would be on 0 - 35??? i dont know...
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: raron on Jun 15, 2010, 09:47 am
Ok, well you need to take a look at the array (http://arduino.cc/en/Reference/Array) thing!

In my example, ledPin[0] will have the value 54. ledPin[1] will have the value 53, and so on. So your first LED is nr. 0 (zero).

So if you do a
Code: [Select]
digitalWrite(ledPin[1], HIGH)
your second LED will light up (that is, on pin 53)

Can be a little confusing having to shift the LED nr. down from 0 to 35 in the beginning. I suppose you could waste a byte (or two, as it is an integer array), and have the array going from 0 to 36, and simply not use array element 0.
As a start, to get your array programming going that is. probably easier to think about the LEDs from nr. 1 rather than "nr. 0"..  It's not really a good habit though, so don't say I said that  :P
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 10:10 am
ok, i changed the led pin setup... the next step would be to change the pinMode's...

i am not even joking... i am getting tunnel vision from modifing hundreds of lines of code... i REALLY need your help in cleaning this up...

the compiled working (i dont even have any of this hooked up... im just writing the code in advanced and testing it by compiling it...) code:

Code: [Select]
#include <Servo.h>

int val1;
int val2;

int potPin = 0;

Servo servoPin;

int LED[35];

void setup()
{
servoPin.attach(13);

pinMode(LED[0], OUTPUT);
pinMode(LED[1], OUTPUT);
pinMode(LED[2], OUTPUT);
pinMode(LED[3], OUTPUT);
pinMode(LED[4], OUTPUT);
pinMode(LED[5], OUTPUT);
pinMode(LED[6], OUTPUT);
pinMode(LED[7], OUTPUT);
pinMode(LED[8], OUTPUT);
pinMode(LED[9], OUTPUT);
pinMode(LED[10], OUTPUT);
pinMode(LED[11], OUTPUT);
pinMode(LED[12], OUTPUT);
pinMode(LED[13], OUTPUT);
pinMode(LED[14], OUTPUT);
pinMode(LED[15], OUTPUT);
pinMode(LED[16], OUTPUT);
pinMode(LED[17], OUTPUT);
pinMode(LED[18], OUTPUT);
pinMode(LED[19], OUTPUT);
pinMode(LED[20], OUTPUT);
pinMode(LED[21], OUTPUT);
pinMode(LED[22], OUTPUT);
pinMode(LED[23], OUTPUT);
pinMode(LED[24], OUTPUT);
pinMode(LED[25], OUTPUT);
pinMode(LED[26], OUTPUT);
pinMode(LED[27], OUTPUT);
pinMode(LED[28], OUTPUT);
pinMode(LED[29], OUTPUT);
pinMode(LED[30], OUTPUT);
pinMode(LED[31], OUTPUT);
pinMode(LED[32], OUTPUT);
pinMode(LED[33], OUTPUT);
pinMode(LED[34], OUTPUT);
pinMode(LED[35], OUTPUT);
}

void loop()
{
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 0, 180);
val2 = map(val2, 0, 1023, 0, 36);

servoPin.write(val1);

 if (val2 >= 1){              
 digitalWrite(LED[0], HIGH);
}
else{
 digitalWrite(LED[0], LOW);
}

if (val2 >= 2){              
 digitalWrite(LED[1], HIGH);
}
else{
 digitalWrite(LED[1], LOW);
}

 if (val2 >= 3){              
 digitalWrite(LED[2], HIGH);
}
else{
 digitalWrite(LED[2], LOW);
}

 if (val2 >= 4){              
 digitalWrite(LED[3], HIGH);
}
else{
 digitalWrite(LED[3], LOW);
}

 if (val2 >= 5){              
 digitalWrite(LED[4], HIGH);
}
else{
 digitalWrite(LED[4], LOW);
}

 if (val2 >= 6){              
 digitalWrite(LED[5], HIGH);
}
else{
 digitalWrite(LED[5], LOW);
}

if (val2 >= 7){              
 digitalWrite(LED[6], HIGH);
}
else{
 digitalWrite(LED[6], LOW);
}

if (val2 >= 8){              
 digitalWrite(LED[7], HIGH);
}
else{
 digitalWrite(LED[7], LOW);
}

if (val2 >= 9){              
 digitalWrite(LED[8], HIGH);
}
else{
 digitalWrite(LED[8], LOW);
}

if (val2 >= 10){              
 digitalWrite(LED[9], HIGH);
}
else{
 digitalWrite(LED[9], LOW);
}

if (val2 >= 11){              
 digitalWrite(LED[10], HIGH);
}
else{
 digitalWrite(LED[10], LOW);
}

if (val2 >= 12){              
 digitalWrite(LED[11], HIGH);
}
else{
 digitalWrite(LED[11], LOW);
}

if (val2 >= 1){              
 digitalWrite(LED[12], HIGH);
}
else{
 digitalWrite(LED[12], LOW);
}

if (val2 >= 14){              
 digitalWrite(LED[13], HIGH);
}
else{
 digitalWrite(LED[13], LOW);
}

if (val2 >= 15){              
 digitalWrite(LED[14], HIGH);
}
else{
 digitalWrite(LED[14], LOW);
}

if (val2 >= 16){              
 digitalWrite(LED[15], HIGH);
}
else{
 digitalWrite(LED[15], LOW);
}

if (val2 >= 17){              
 digitalWrite(LED[16], HIGH);
}
else{
 digitalWrite(LED[16], LOW);
}
if (val2 >= 18){              
 digitalWrite(LED[17], HIGH);
}
else{
 digitalWrite(LED[17], LOW);
}

if (val2 >= 19){              
 digitalWrite(LED[18], HIGH);
}
else{
 digitalWrite(LED[18], LOW);
}

if (val2 >= 20){              
 digitalWrite(LED[19], HIGH);
}
else{
 digitalWrite(LED[19], LOW);
}

if (val2 >= 21){              
 digitalWrite(LED[20], HIGH);
}
else{
 digitalWrite(LED[20], LOW);
}

if (val2 >= 22){              
 digitalWrite(LED[21], HIGH);
}
else{
 digitalWrite(LED[21], LOW);
}

if (val2 >= 23){              
 digitalWrite(LED[22], HIGH);
}
else{
 digitalWrite(LED[22], LOW);
}

if (val2 >= 24){              
 digitalWrite(LED[23], HIGH);
}
else{
 digitalWrite(LED[23], LOW);
}

if (val2 >= 25){              
 digitalWrite(LED[24], HIGH);
}
else{
 digitalWrite(LED[24], LOW);
}

if (val2 >= 26){              
 digitalWrite(LED[25], HIGH);
}
else{
 digitalWrite(LED[25], LOW);
}

if (val2 >= 27){              
 digitalWrite(LED[26], HIGH);
}
else{
 digitalWrite(LED[26], LOW);
}

if (val2 >= 28){              
 digitalWrite(LED[27], HIGH);
}
else{
 digitalWrite(LED[27], LOW);
}

if (val2 >= 29){              
 digitalWrite(LED[28], HIGH);
}
else{
 digitalWrite(LED[28], LOW);
}

if (val2 >= 30){              
 digitalWrite(LED[29], HIGH);
}
else{
 digitalWrite(LED[29], LOW);
}

if (val2 >= 31){              
 digitalWrite(LED[30], HIGH);
}
else{
 digitalWrite(LED[30], LOW);
}

if (val2 >= 32){              
 digitalWrite(LED[31], HIGH);
}
else{
 digitalWrite(LED[31], LOW);
}

if (val2 >= 33){              
 digitalWrite(LED[32], HIGH);
}
else{
 digitalWrite(LED[32], LOW);
}

if (val2 >= 34){              
 digitalWrite(LED[33], HIGH);
}
else{
 digitalWrite(LED[33], LOW);
}

if (val2 >= 35){              
 digitalWrite(LED[34], HIGH);
}
else{
 digitalWrite(LED[34], LOW);
}

if (val2 >= 36){              
 digitalWrite(LED[35], HIGH);
}
else{
 digitalWrite(LED[35], LOW);
}
}
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 10:23 am
I wouldn't go down this:
Code: [Select]
for (int ledNr=0; ledNr<=36; ledNr++)
 {
   ledPin[ledNr] = ledPinNr-- ;
 }
route.

The LEDs as you have them numbered are consecutive anyway, so this approach is pointless - the whole thing could be done without arithmetically, without an array.

However, imagine you've (accidentally) soldered a couple of LEDs out of sequence, or there's a break in the sequence because you needed a pin for PWM or serial or something.
Arithmetic doesn't work anymore, so a "const byte" array is the way to do it.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: raron on Jun 15, 2010, 10:34 am
You forgot to assign values to the LED[] array, so its no way of knowing what these values are (and what pins you set as OUTPUT).
Code: [Select]

#include <Servo.h>

int val1;
int val2;

int potPin = 0;

Servo servoPin;

int LED[35];

void setup()
{
servoPin.attach(13);

pinMode([glow]LED[0][/glow], OUTPUT);
pinMode([glow]LED[1][/glow], OUTPUT);
pinMode([glow]LED[2][/glow], OUTPUT);
.
.
.
etc


To use my former example slightly modified
Code: [Select]

int LED[35];  // 0 - 35, 36 LEDs. important to know what you use this value for! Here: output pin nr. A better name imho is LEDpin.
// Also distinguish the array's element's values, from the array index value (0 - 35 here)


void setup()
{
 // Assign output pin nr. to LED nr.
 int ledPinNr = 54; // start output pin nr.
 for (int ledNr=0; ledNr<36; ledNr++)
 {
   LED[ledNr] = ledPinNr-- ;
 }

 // Make all LED pins outputs
 for (int i = 0; i<36; i++)
 {
   pinMode(LED[i],OUTPUT);
 }
}


This should make an array 0-36, filled with the values 54-19, and also set the pin mode to output.


Ah, [glow]Groove is completely correct[/glow]. I knew I was missing something really basic.. The array route for this particular case is pointless (sorry!). But for another day, if you use output pins not in sequence, for example, it would be.
And arrays are useful to know anyway.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 10:42 am
Code: [Select]
const byte LED[[glow]36[/glow]] = {54, 53, 52, //etc };
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 10:44 am
in noob language please?

edit: you beat me by like 3 seconds
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 10:50 am
alrigth, so i changed it to a constant byte... now how do i tackle the pinMode situation???
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 10:56 am
Code: [Select]
for (int i = 0; i < sizeof(LED) / sizeof(LED[0]); ++i) {
 pinMode (LED[i], OUTPUT);
}
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 11:14 am
well, i have no clue how it works, but it works!!!

last but ont least the void loop...

current code:

Code: [Select]
#include <Servo.h>

int val1;
int val2;

int potPin = 0;

Servo servoPin;

const byte LED[36] = {54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19};

void setup()
{
servoPin.attach(13);

for (int i = 0; i < sizeof(LED) / sizeof(LED[0]); ++i) {
 pinMode (LED[i], OUTPUT);
}
}

void loop()
{
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 0, 180);
val2 = map(val2, 0, 1023, 0, 36);

servoPin.write(val1);

 if (val2 >= 1){              
 digitalWrite(LED[0], HIGH);
}
else{
 digitalWrite(LED[0], LOW);
}

if (val2 >= 2){              
 digitalWrite(LED[1], HIGH);
}
else{
 digitalWrite(LED[1], LOW);
}

 if (val2 >= 3){              
 digitalWrite(LED[2], HIGH);
}
else{
 digitalWrite(LED[2], LOW);
}

 if (val2 >= 4){              
 digitalWrite(LED[3], HIGH);
}
else{
 digitalWrite(LED[3], LOW);
}

 if (val2 >= 5){              
 digitalWrite(LED[4], HIGH);
}
else{
 digitalWrite(LED[4], LOW);
}

 if (val2 >= 6){              
 digitalWrite(LED[5], HIGH);
}
else{
 digitalWrite(LED[5], LOW);
}

if (val2 >= 7){              
 digitalWrite(LED[6], HIGH);
}
else{
 digitalWrite(LED[6], LOW);
}

if (val2 >= 8){              
 digitalWrite(LED[7], HIGH);
}
else{
 digitalWrite(LED[7], LOW);
}

if (val2 >= 9){              
 digitalWrite(LED[8], HIGH);
}
else{
 digitalWrite(LED[8], LOW);
}

if (val2 >= 10){              
 digitalWrite(LED[9], HIGH);
}
else{
 digitalWrite(LED[9], LOW);
}

if (val2 >= 11){              
 digitalWrite(LED[10], HIGH);
}
else{
 digitalWrite(LED[10], LOW);
}

if (val2 >= 12){              
 digitalWrite(LED[11], HIGH);
}
else{
 digitalWrite(LED[11], LOW);
}

if (val2 >= 1){              
 digitalWrite(LED[12], HIGH);
}
else{
 digitalWrite(LED[12], LOW);
}

if (val2 >= 14){              
 digitalWrite(LED[13], HIGH);
}
else{
 digitalWrite(LED[13], LOW);
}

if (val2 >= 15){              
 digitalWrite(LED[14], HIGH);
}
else{
 digitalWrite(LED[14], LOW);
}

if (val2 >= 16){              
 digitalWrite(LED[15], HIGH);
}
else{
 digitalWrite(LED[15], LOW);
}

if (val2 >= 17){              
 digitalWrite(LED[16], HIGH);
}
else{
 digitalWrite(LED[16], LOW);
}
if (val2 >= 18){              
 digitalWrite(LED[17], HIGH);
}
else{
 digitalWrite(LED[17], LOW);
}

if (val2 >= 19){              
 digitalWrite(LED[18], HIGH);
}
else{
 digitalWrite(LED[18], LOW);
}

if (val2 >= 20){              
 digitalWrite(LED[19], HIGH);
}
else{
 digitalWrite(LED[19], LOW);
}

if (val2 >= 21){              
 digitalWrite(LED[20], HIGH);
}
else{
 digitalWrite(LED[20], LOW);
}

if (val2 >= 22){              
 digitalWrite(LED[21], HIGH);
}
else{
 digitalWrite(LED[21], LOW);
}

if (val2 >= 23){              
 digitalWrite(LED[22], HIGH);
}
else{
 digitalWrite(LED[22], LOW);
}

if (val2 >= 24){              
 digitalWrite(LED[23], HIGH);
}
else{
 digitalWrite(LED[23], LOW);
}

if (val2 >= 25){              
 digitalWrite(LED[24], HIGH);
}
else{
 digitalWrite(LED[24], LOW);
}

if (val2 >= 26){              
 digitalWrite(LED[25], HIGH);
}
else{
 digitalWrite(LED[25], LOW);
}

if (val2 >= 27){              
 digitalWrite(LED[26], HIGH);
}
else{
 digitalWrite(LED[26], LOW);
}

if (val2 >= 28){              
 digitalWrite(LED[27], HIGH);
}
else{
 digitalWrite(LED[27], LOW);
}

if (val2 >= 29){              
 digitalWrite(LED[28], HIGH);
}
else{
 digitalWrite(LED[28], LOW);
}

if (val2 >= 30){              
 digitalWrite(LED[29], HIGH);
}
else{
 digitalWrite(LED[29], LOW);
}

if (val2 >= 31){              
 digitalWrite(LED[30], HIGH);
}
else{
 digitalWrite(LED[30], LOW);
}

if (val2 >= 32){              
 digitalWrite(LED[31], HIGH);
}
else{
 digitalWrite(LED[31], LOW);
}

if (val2 >= 33){              
 digitalWrite(LED[32], HIGH);
}
else{
 digitalWrite(LED[32], LOW);
}

if (val2 >= 34){              
 digitalWrite(LED[33], HIGH);
}
else{
 digitalWrite(LED[33], LOW);
}

if (val2 >= 35){              
 digitalWrite(LED[34], HIGH);
}
else{
 digitalWrite(LED[34], LOW);
}

if (val2 >= 36){              
 digitalWrite(LED[35], HIGH);
}
else{
 digitalWrite(LED[35], LOW);
}
}
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: GrooveFlotilla on Jun 15, 2010, 12:32 pm
Code: [Select]

void loop()
{
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 0, 180);
val2 = map(val2, 0, 1023, 0, 36);

servoPin.write(val1);

for (int i = 0; i < sizeof(LED) / sizeof(LED[0]); ++i) {
   digitalWrite (LED[0], val2 >= i + 1);
}



or probably even simpler...
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 12:54 pm
you sir are an absolute jenious!!! i compiled it, and it worked, but i cant test it... i don't have an arduino mega yet...  :-[
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 01:11 pm
now the only thing i need is transistors... can you guys recommend some dip packages of transistors... i would like anywhere between 5 and 10 transistors per package...
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: borref on Jun 15, 2010, 05:57 pm
Have a look at your code again Richard. It will not compile as is, you do not declare your variables, nor do you hint on what they ought to be.

Then there is this part:

Code: [Select]
if (55 - (val / 5)) = pinnbr {
   outval = HIGH;
}


It looks as if you assign the value of "pinnbr" to an expression? This is not valid C-syntax. There may be some good intention here, but it's certainly hard to spot.

For a guy who is 11 years old, I'm pretty impressed by his skills and ability to stay focused through some "challenging" advice.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 07:41 pm
Is the "protocal" the same for these? As in do I still use a 1k ohm resistor between the base and arduino?

I still haven't decided on one yet, but i want them with all normal npn transistors. I am still reading the datasheets.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: AWOL on Jun 15, 2010, 08:20 pm
Quote
i compiled it, and it worked, but i cant test it...


Compiled != worked.

You have much to learn, my young padawan.    ;)

(Keep it up!)
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: borref on Jun 15, 2010, 09:48 pm
Quote
... and sometimes forget which ones use == vs. = for comparison.

If you fix that, there is still an issue with parentheses. As written, the conditional expression in your code is "(55 - (val / 5)). In "C" you need to enclose the full conditional expression in parentheses.
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 15, 2010, 11:27 pm
So I think I will get the ULN2004A's. If I am correct, the left pins (pins 1-7) are the base, the right pins (pins 10 - 16) are the collectors, and pin 9 is the common emitter? Please tell me if I am right.  
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 16, 2010, 12:21 am
Ok, so I found these:

http://www.st.com/stonline/books/pdf/docs/1536.pdf

They are cheap, and they look like they should work. From what I understand, the ULN2801A is for use with external ressitors, and the ULN2803A is for use with 5 volt signals? Please correct me if I am wrong.

Also, what would I hook the common load connection to? Would it be 5 volts because that's what I am driving my led's with?
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 16, 2010, 04:09 am
Yay! I am finally understanding! LOL!

It's a website called Newark, I'm sure you have heard of it?

And as far as FINDING then, i just Google searched "transistor array" and went into images. Then i found a schematic that I liked, and looked up the corresponding IC in google shopping. http://computerchristmas.com/tmp/myimages/7-2803.jpg
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 16, 2010, 07:16 am
i searched it in gogle shopping, and thay actually ahve it cheapest... but i know what you mean... i wanted some crystal oacillators, and they had em super cheap! the catch was that they were going to charge me a $25 fee just to get them off of the stupid shelf! i said forget that!
Title: Re: Potentiometer Controlling LED Graph and Servo
Post by: Jeremy1998 on Jun 16, 2010, 11:51 pm
Alright, so I finished the Fritzing design... It took me 2 dsya and a total of over 12 hours!

Can someone tell me how i pulpad the JPG image? I am prety sure that I have to upload it to photobucket or flickr or some other photo server?