Go Down

Topic: Code for a 7 Panel LED Display (Read 417 times) previous topic - next topic

dpickttu

I am trying to write a code to make a mock 7 panel LED display out of 14 seperate LED's. I want to be able to turn the knob on a potentiometer and have the output from that mapped from 0-99. The correspoding mapped number should be displayed on the LED's. I have a code that is able to be compiled but when uploaded to the Arduino Uno, only LED6 (Terminal 5) illuminates. Can you please check my coding for possible errors? I may have just messed up my wiring rather than my coding....

Code: [Select]
int d = 0;
int y = 0;
int x = 0;
int number = 0;
int sensorvalue = 0;
int pin0 = 0;
int pin1 = 1;
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int pin9 = 9;
int pin10 = 10;
int pin11 = 11;
int pin12 = 12;
int pin13 = 13;
void one(int d);
void setup()
{
   pinMode(pin0, OUTPUT);
pinMode(pin1, OUTPUT);   
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
pinMode(pin9, OUTPUT);
pinMode(pin10, OUTPUT);
pinMode(pin11, OUTPUT);
pinMode(pin12, OUTPUT);
pinMode(pin13, OUTPUT);
}


void loop() {
sensorvalue=analogRead(2);
number=map(sensorvalue,0,1023,0,99);
y=number%10;
x=(number/10)-y;
switch (x){
case 1: one(1); break;
case 2: two(1); break;
case 3: three(1); break;
case 4: four(1); break;
case 5: five(1); break;
case 6: six(1); break;
case 7: seven(1); break;
case 8: eight(1); break;
case 9: nine(1); break;
case 10: zero(1); break;
}
switch (y){
case 1: one(2); break;
case 2: two(2); break;
case 3: three(2); break;
case 4: four(2); break;
case 5: five(2); break;
case 6: six(2); break;
case 7: seven(2); break;
case 8: eight(2); break;
case 9: nine(2); break;
case 10: zero(2); break;
}
}


void one(int d){
  if (d==1)
{digitalWrite(0,LOW);
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
}else if (d==2)
{digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);}
}
void two(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);}
}
void three(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);}
}
void four(int d)
{if (d==1)
{ digitalWrite(0,LOW);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}else if (d==2)
{digitalWrite(7,LOW);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);}
}
void five(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);}
}
void six(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,LOW);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);}
}
void seven(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);}
}
void eight(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);}
}
void nine(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);}
}
void zero(int d)
{if (d==1)
{ digitalWrite(0,HIGH);
digitalWrite(1,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
}else if (d==2)
{digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);}
}

dpickttu

Also, how would I get a read out of what the potentiometer voltage is? That way i can check my LED display to see if it displaying the correct number

wildbill

Change your wiring to avoid use of pins 0 and 1 and then you can use serial.Print to tell you the pot reading. Don't forget that you can use the analog pins as digital too.

CrossRoads

If you left D0, D1 free you could use the serial monitor as a debug aid and output the analogRead value:

float volts =  sensorvalue * (5/1023); // ~4.88mV per bit when using 5V as the Aref value.
Serial.println(volts);

I don't know about the rest of it. I guess it makes sense if this is correct:

y=number%10;  
x=(number/10)-y;  << maybe (number - y)/10 instead  to get 10,20,30,40, etc. divided by 10.
re-check the way  % works.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

dpickttu

to do that, i plugged the wires from 0 and 1 in to A5 and A4 respectively, then changed my code to where int pin0 = A5 and int pin1 = A4... Doing this makes it so that the LED's do not illuminate

wildbill

One thing to try is turning on your LEDs in setup, followed by a few seconds delay, so you can see whether they are working irrespective of the pot. Do you have current limiting resistors protecting the LEDs from burning out?

Grumpy_Mike

Quote
int pin0 = A5 and int pin1 = A4.

No change it to:-
Code: [Select]

int pin0 = 19; // A5
int pin1 = 18; // A4


In general your code is very much longer than it need be. See:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Go Up