This code will take a 4 bit input from switches and convert it to a hexadecimal number to be displayed using the 7 seg led. For example, there will be for switches if one is pushed depending on where it will display the corresponding number, an unpressed switched is 0, when the switch is pressed it becomes 1.
So in this example: switch1, switch2, switch3, switch4 looks like 0,0,0,1 = So a 1 will be displayed by the 7 segment LED. Here is my code below,use it as you wish.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /////////////////////////// // Ronald A. Richardson // 2.28.2011 // 4bit to hex /////////////////////////// byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 }, // = 0 { 0,1,1,0,0,0,0 }, // = 1 { 1,1,0,1,1,0,1 }, // = 2 { 1,1,1,1,0,0,1 }, // = 3 { 0,1,1,0,0,1,1 }, // = 4 { 1,0,1,1,0,1,1 }, // = 5 { 1,0,1,1,1,1,1 }, // = 6 { 1,1,1,0,0,0,0 }, // = 7 { 1,1,1,1,1,1,1 }, // = 8 { 1,1,1,0,0,1,1 } // = 9 }; void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, INPUT); pinMode(11, INPUT); pinMode(12, INPUT); pinMode(13, INPUT); } void displayDigit(byte digit) { for (byte segCount = 0; segCount < 7; ++segCount) { int pin =2; digitalWrite(pin, seven_seg_digits[digit][segCount]); pin++; } } void loop() { int buttonOne = digitalRead(10); int buttonTwo = digitalRead(11); int buttonThree = digitalRead(12); int buttonFour = digitalRead(13); if(buttonOne && buttonTwo && buttonThree && buttonFour == LOW){ displayDigit(0); }else if(buttonOne && buttonTwo && buttonThree == LOW){ if(buttonFour == HIGH){ displayDigit(1); } } else if(buttonOne && buttonTwo && buttonFour == LOW){ if(buttonThree == HIGH){ displayDigit(2); } } else if(buttonOne && buttonTwo == LOW){ if(buttonThree && buttonFour == HIGH){ displayDigit(3); } } else if(buttonOne && buttonThree && buttonFour == LOW){ if(buttonTwo == HIGH){ displayDigit(4); } } else if(buttonOne && buttonThree == LOW){ if (buttonTwo && buttonFour == HIGH){ displayDigit(5); } } else if(buttonOne && buttonFour == LOW){ if(buttonTwo && buttonThree == HIGH){ displayDigit(6); } } else if(buttonOne == LOW){ if(buttonTwo && buttonThree && buttonFour == HIGH){ displayDigit(7); } } else if(buttonTwo && buttonThree && buttonFour == LOW){ if(buttonOne == HIGH){ displayDigit(8); } } else if(buttonTwo && buttonThree == LOW){ if(buttonOne && buttonFour == HIGH){ displayDigit(9); } } } |
Leave a Reply