Go Down

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

Jeremy1998

so that will use it as my desired pins??? that makes me think it does pins 0 - 35...

raron

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 section to look at.

Groove

Zero to thirty-five is a thirty-six element array.
Per Arduino ad Astra

Jeremy1998

well, i mean that the series thing makes me think that the leds would be on 0 - 35??? i dont know...

raron

Ok, well you need to take a look at the 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

Jeremy1998

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

Groove

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.
Per Arduino ad Astra

raron

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.

Groove

Code: [Select]
const byte LED[[glow]36[/glow]] = {54, 53, 52, //etc };
Per Arduino ad Astra

Jeremy1998

#24
Jun 15, 2010, 10:44 am Last Edit: Jun 15, 2010, 10:45 am by Jeremy1998 Reason: 1
in noob language please?

edit: you beat me by like 3 seconds

Jeremy1998

alrigth, so i changed it to a constant byte... now how do i tackle the pinMode situation???

Groove

Code: [Select]
for (int i = 0; i < sizeof(LED) / sizeof(LED[0]); ++i) {
 pinMode (LED[i], OUTPUT);
}
Per Arduino ad Astra

Jeremy1998

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

Groove

#28
Jun 15, 2010, 12:32 pm Last Edit: Jun 15, 2010, 12:38 pm by GrooveFlotilla Reason: 1
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...
Per Arduino ad Astra

Jeremy1998

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...  :-[

Go Up