Created
August 14, 2015 23:43
-
-
Save CodeZombie/3d8c3fe6dc985bb7db2f to your computer and use it in GitHub Desktop.
AlphaNumeric seven segment arduino sketch with Bitshifting/bitmasking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static unsigned char alphanum[37] = {0xEE, 0x3E, 0x9C, 0x7A, 0x9E, 0x8E, 0xBC, 0x6E, 0x0C, 0x78, 0xAE, 0x1C, 0xA8, 0xEC, 0xFC, 0xCE, 0xE6, 0xCC, 0xB6, 0x1E, 0x7C, 0x74, 0x54, 0x6E, 0x76, 0xD2, 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6, 0x00}; | |
int pin_offset = 2; //the pin your 7seg starts at. | |
//used for cycling through characters | |
int iterator = 200; //just for demonstration purposes. | |
void printCharacter(char c_) { | |
//if the character is a lowercase alpha, convert it to an uppercase. | |
if(c_ >= 97 && c_ <= 122) { | |
c_ -= 32;//lowercase letters appear 32 places higher in the ascii table | |
} | |
//convert ascii character indexes to our array index | |
if(c_ >=65 && c_ <= 90) {//within the alpha range (A-Z) | |
c_-=65; //in the ascii table, A is 65, and in our array, A is 0. | |
}else if( c_ >= 48 && c_ <= 57){//within the number range (0-9) | |
c_-=22; //in the ascii table, '0' is 48, but in our table, '0' is 26. We just use the difference of these two numbers. | |
}else{ //something else | |
c_ = 36; //this is an empty character. It lights up no segments. | |
} | |
for(int i = 0; i < 7; i++){//for each segment | |
//take the byte representing each character | |
//mask each bit, starting with the rightmost, and use these results as a bool. | |
digitalWrite(i+pin_offset, 0x01 &( alphanum[c_] >> (7-i))); //we want the bit to be processed from the right to the left. so we use 7-i. | |
} | |
} | |
void setup() { | |
for (int i = pin_offset; i <= 7 + pin_offset; i++) { | |
pinMode(i, OUTPUT); | |
} | |
Serial.begin(9600); | |
} | |
void loop() { | |
iterator++; | |
if (iterator > 48 + 9) { iterator = 48; }//num | |
//if (iterator > 65 + 25) { iterator = 65; }//alpha | |
printCharacter(iterator); | |
delay(800); | |
} |
//todo: reduce the digitalWrite forloop to 6, as the 8th bit is ALWAYS 0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//needs to be cleaned up a bit