Go Down

Topic: Potentiometer Controlling LED Graph and Servo (Read 2 times) previous topic - next topic

Jeremy1998

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);
}



Jeremy1998


Jeremy1998


raron

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).

Jeremy1998

#4
Jun 15, 2010, 08:14 am Last Edit: Jun 15, 2010, 08:32 am by Jeremy1998 Reason: 1
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?

raron

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)


Jeremy1998

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);
}
}

Groove

#7
Jun 15, 2010, 08:48 am Last Edit: Jun 15, 2010, 08:54 am by GrooveFlotilla Reason: 1
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);
?
Per Arduino ad Astra

Jeremy1998

#8
Jun 15, 2010, 08:53 am Last Edit: Jun 15, 2010, 08:54 am by Jeremy1998 Reason: 1
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...

raron

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).

Jeremy1998

#10
Jun 15, 2010, 08:59 am Last Edit: Jun 15, 2010, 09:02 am by Jeremy1998 Reason: 1
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?

Groove

#11
Jun 15, 2010, 09:03 am Last Edit: Jun 15, 2010, 09:05 am by GrooveFlotilla Reason: 1
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.
Per Arduino ad Astra

raron

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

Jeremy1998

#13
Jun 15, 2010, 09:15 am Last Edit: Jun 15, 2010, 09:18 am by Jeremy1998 Reason: 1
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)

Groove

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


BZZZZT!
Per Arduino ad Astra

Go Up