Hi! I'm working displaying numbers on 7 segment LED display by multiplexing. I want to convert hex values to binary for the 7 segments. I have an idea as to how to do this in other languages like Java but am unsure as to how to go about this using C/C++ in Arduino. I would also like to know if there's a way to display the segments with just the hex values. All help is much appreciated! Below is my code:
Array of hex values to be converted to binary:
char digitPatternsHex[PATTERN_COUNT][2] = { "3F", "06", "5B", "4F", "6E", "6B", "7D", "07", "7F", "6F", "00" };
What I have so far:
void hexaToBinaryArray(char[][] hexaArray){
char digitPatterns[PATTERN_COUNT][SEGMENT_COUNT];
for(int i=0; i<PATTERN_COUNT; i++){
for(int j=1; j>=0;j--){
switch(char[i][j]){ //3F = 0111111
case '0':
}
}
}
}
UPDATE:
Thanks for all the help! I got it working using bitRead() :). My full code:
#define PATTERN_COUNT 11 //number of segment patterns, 0-9 and all off
#define SEGMENT_COUNT 7 //how many segments I'm controlling
#define DIGIT_COUNT 4 //how many digits I'm controlling
// A,B,C,D,E,F,G
int segmentPins[SEGMENT_COUNT] = {2,3,4,5,6,7,8};
//the pins for each digit
int digitPins[] = { 9, 10, 11, 12 };
// 0 1 2 3 4 5 6 7 8 9 OFF
byte digitPatternsHex[PATTERN_COUNT] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x00 };
//counter
int counter = 0;
void setup(){
Serial.begin(9600);
//init segment pins to output
for(int thisPin=0; thisPin<SEGMENT_COUNT; thisPin++){
pinMode(segmentPins[thisPin], OUTPUT);
}
//init digit pins to output
for(int thisPin=0; thisPin<DIGIT_COUNT; thisPin++){
pinMode(digitPins[thisPin], OUTPUT);
}
}
//turn all digits off, HIGH because common cathode
void allDigitsOff(){
for(int thisPin = 0; thisPin < DIGIT_COUNT; thisPin++){
digitalWrite(digitPins[thisPin], HIGH);
}
}
//turn specific digit on
void digitOn(int digitNum){
digitalWrite(digitPins[digitNum], LOW);
delay(2);
}
void setPattern(int pattern){
allDigitsOff();
//data pins, 8bits
for(int thisPin=0; thisPin<SEGMENT_COUNT; thisPin++){
if(bitRead(digitPatternsHex[pattern], thisPin) == 1){
digitalWrite(segmentPins[thisPin], HIGH);
}else{
digitalWrite(segmentPins[thisPin], LOW);
}
}
}
void showNumber(int currentNumber){
//display digit from right to left
int digitInFront = currentNumber/10;
for(int currentDigit = 0; currentDigit < DIGIT_COUNT; currentDigit++){
//get number in the ones place
int number = currentNumber % 10; //5
currentNumber /= 10;
setPattern(number);
digitOn(currentDigit);
}
}
void loop(){
//return first 2 digits of ms, 1 sec = 1000 millisec, 1.5 sec = 1500 millisec
int currentNumber = (millis()/1000);
showNumber(currentNumber);
}