Friday, March 12, 2010

LED Project

This project takes a serial input of a single hex digit and displays it on a 7 segment display. The display is driven by only 2 data pins by the use of a shift register.

Fritzing Diagram:


Code:
int clock = 13;
int data = 12;
int input;

byte value[] = {B01111110,B00010010,B10111100,B10110110,B11010010,B11100110,B11101110,B00110010,B11111110,B11110110,B11111010,B11001110,B10001100,B10011110,B11101100,B11101000,B00000000}; //This is the byte patterns for the shift register stored in an array.

void setup()
{
pinMode(clock, OUTPUT);
pinMode(data , OUTPUT);
Serial.begin(9600);
}

void loop()
{
int i = Serial.read();
switch (i){ //This case statement takes the ASCII value read from serial and translates it to its corresponding index value in the array
case 48:{ input = 0;}
break;
case 49:{ input = 1;}
break;
case 50:{ input = 2;}
break;
case 51:{ input = 3;}
break;
case 52:{ input = 4;}
break;
case 53:{ input = 5;}
break;
case 54:{ input = 6;}
break;
case 55:{ input = 7;}
break;
case 56:{ input = 8;}
break;
case 57:{ input = 9;}
break;
case 65:{ input = 10;} //Values beyond 9 are letters A-F (65-70) and a-f(97-102)
break;
case 66:{ input = 11;}
break;
case 67:{ input = 12;}
break;
case 68:{ input = 13;}
break;
case 69:{ input = 14;}
break;
case 70:{ input = 15;}
break;
case 97:{ input = 10;}
break;
case 98:{ input = 11;}
break;
case 99:{ input = 12;}
break;
case 100:{ input = 13;}
break;
case 101:{ input = 14;}
break;
case 102:{ input = 15;}
break;
default: {input = 16;} //If no value is input this value will be chosen
}
if (input == 16){ //this loop flashes the centre bar of the display when no value has been read
shiftOut(data, clock, LSBFIRST, B10000000);
delay(1000);
shiftOut(data, clock, LSBFIRST, value[input]);
delay(1000);
}
else
{ //if a value has been read, it will display it on the display for 2 seconds then clear the display for 1
shiftOut(data, clock, LSBFIRST, value[input]);
delay(2000); //with no delay all segments stay on
shiftOut(data, clock, LSBFIRST, value[16]);
delay(1000);
}
}

No comments:

Post a Comment