Pages: [1] 2 3   Go Down
Author Topic: Potentiometer Controlling LED Graph and Servo  (Read 1462 times)
0 Members and 1 Guest are viewing this topic.
Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

plz help me out...
Logged

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i dont really understand that
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: June 15, 2010, 01:32:37 am by Jeremy1998 » Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)

Logged

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well, i just wrote the code, and it compiled! smiley

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

Code:
#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);
 }
}
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  pinMode(potPin, INPUT);

Why this:
Code:
val1 = analogRead(potPin);
 val2 = analogRead(potPin);
 val1 = map(val1, 0, 1023, 0, 180);
 val2 = map(val2, 0, 1023, 0, 36);
?
« Last Edit: June 15, 2010, 01:54:48 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: June 15, 2010, 01:54:40 am by Jeremy1998 » Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley-razz I wrote this before I saw your next post. I think it should work (except map from 1 to 36, not 0 to 36).
Logged

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: June 15, 2010, 02:02:37 am by Jeremy1998 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 15, 2010, 02:05:59 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Example. Instead of

Code:
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:
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 smiley Experiment
Logged

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Also, you may be right about that 0-36:)Experiment

well, i don't wanna argue with the master smiley-wink LOL JK JK

on a more serious note:
how do i do the series with my desired pins (54 - 19)
« Last Edit: June 15, 2010, 02:18:23 am by Jeremy1998 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

BZZZZT!
Logged

Per Arduino ad Astra

Pages: [1] 2 3   Go Up
Jump to: