Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const byte hexadecimalNumbers[16][7] = { { 0,0,0,0,0,0,1}, // = 0
- { 1,0,0,1,1,1,1 }, // = 1
- { 0,0,1,0,0,1,0 }, // = 2
- { 0,0,0,0,1,1,0 }, // = 3
- { 1,0,0,1,1,0,0 }, // = 4
- { 0,1,0,0,1,0,0 }, // = 5
- { 0,1,0,0,0,0,0 }, // = 6
- { 0,0,0,1,1,1,1 }, // = 7
- { 0,0,0,0,0,0,0 }, // = 8
- { 0,0,0,1,1,0,0 }, // = 9
- { 0,0,0,1,0,0,0 }, // = A
- { 0,0,0,0,0,0,0 }, // = B
- { 0,1,1,0,0,0,1 }, // = C
- { 0,0,0,0,0,0,1 }, // = D
- { 0,1,1,0,0,0,0 }, // = E
- { 0,1,1,1,0,0,0 } // = F
- };
- byte currentNumbersToDisplay[6][7];
- byte toDisplay[4][7] = {{ 1,1,1,1,1,1,1}, { 1,1,1,1,1,1,1}, { 1,1,1,1,1,1,1}, { 1,1,1,1,1,1,1}};
- byte brightness[4] = {LOW, LOW, LOW, LOW};
- unsigned long timeDelay = 0;
- int currentWorkingIndex = 0;
- boolean notRead = true;
- 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, OUTPUT);
- pinMode(11, OUTPUT);
- pinMode(12, OUTPUT);
- Serial.begin(9600);
- }
- void checkIfCorrectCommand(char firstCharacter){
- if(firstCharacter == 'T'){
- numbersToDisplay();
- }
- else if(firstCharacter == 'D'){
- timeOfDisplaying();
- }
- else if(firstCharacter == 'B'){
- brightnessOfSegments();
- }
- else{
- Serial.println("Unknown command");
- }
- notRead = true;
- }
- void numbersToDisplay(){
- String numbersSequence = Serial.readString();
- if(checkNumbersToDisplay(numbersSequence)){
- setNumbersToDisplay(numbersSequence);
- }
- }
- boolean checkNumbersToDisplay(String numbersSequence){
- if(numbersSequence.length() == 6){
- char characterToTest;
- for(int i = 0; i<5; i++){
- characterToTest = numbersSequence.charAt(i);
- if((characterToTest>='a' && characterToTest<='f') ||
- (characterToTest>='A' && characterToTest<='F') ||
- (characterToTest>='0' && characterToTest<='9')){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- }
- else{
- Serial.println("Too few characters");
- return false;
- }
- return true;
- }
- void setNumbersToDisplay(String numbersSequence){
- char characterToDisplay;
- for(int i=0; i<5; i++){
- characterToDisplay = numbersSequence.charAt(i);
- copyRowOfMatrix(i, parseCharToInt(characterToDisplay), currentNumbersToDisplay, hexadecimalNumbers);
- }
- }
- void copyRowOfMatrix(int dest, int from, byte (*firstArray)[7], byte (*secondArray)[7]){
- for(int i = 0; i<6; i++){
- (*firstArray)[dest] = (*secondArray)[from];
- firstArray++;
- secondArray++;
- }
- }
- int parseCharToInt(char hexadecimalCharacter){
- currentWorkingIndex = 0;
- if(hexadecimalCharacter>='0' && hexadecimalCharacter<='9'){
- return hexadecimalCharacter - '0';
- }
- else if(hexadecimalCharacter>='a' && hexadecimalCharacter<='f'){
- return hexadecimalCharacter - 'a';
- }
- else{
- return hexadecimalCharacter - 'A';
- }
- }
- void timeOfDisplaying(){
- String time = Serial.readString();
- if(checkTimeOfDisplaying(time)){
- timeDelay = time.toInt();
- }
- }
- boolean checkTimeOfDisplaying(String time){
- if(time.length() == 4){
- char characterToTest;
- for(int i = 0; i<3; i++){
- characterToTest = time.charAt(i);
- if((characterToTest>='0' && characterToTest<='9' && i!=0)
- || (characterToTest>='1' && characterToTest<='9' && i==0)){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- }
- else{
- Serial.println("Too few characters");
- return false;
- }
- return true;
- }
- void brightnessOfSegments(){
- String brightness = Serial.readString();
- if(checkBrightnessOfSegments(brightness)){
- setBrightness(brightness);
- }
- }
- boolean checkBrightnessOfSegments(String brightness){
- if(brightness.length() == 4){
- char characterToTest;
- for(int i = 0; i<3; i++){
- characterToTest = brightness.charAt(i);
- if(characterToTest>='0' && characterToTest<='2'){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- }
- else{
- Serial.println("Too few characters");
- return false;
- }
- return true;
- }
- void setBrightness(String brightnessString){
- char brightnessLevel;
- for(int i=0; i<3; i++){
- brightnessLevel = brightnessString.charAt(i);
- if(brightnessLevel == '0'){
- brightness[i] = HIGH;
- }
- else if(brightnessLevel == '1'){
- brightness[i] = LOW;
- }
- else{
- brightness[i] = LOW;
- }
- }
- }
- void moveNumbers(){
- if((Serial.available() > 0) && (notRead)){
- checkIfCorrectCommand(Serial.read());
- notRead = false;
- }
- for(int i=0; i<2; i++){
- copyRowOfMatrix(i, i+1, toDisplay, toDisplay);
- }
- copyRowOfMatrix(3, currentWorkingIndex++, toDisplay, currentNumbersToDisplay);
- if(currentWorkingIndex == 6){
- currentWorkingIndex = 0;
- }
- display();
- delay(timeDelay);
- }
- void display(){
- for(int i = 0; i < 3; i++){
- prepareOneSegment(i);
- displayOneSegment(i);
- setAppropriateBrightness();
- }
- }
- void prepareOneSegment(int whichOne){
- for(int i = 0; i < 3; i++){
- if(i == whichOne){
- digitalWrite(i+9, LOW);
- }
- else{
- digitalWrite(i+9, HIGH);
- }
- }
- }
- void displayOneSegment(int whichOne){
- byte pin = 2;
- for (byte segCount = 0; segCount < 7; ++segCount) {
- digitalWrite(pin, toDisplay[whichOne][segCount]);
- ++pin;
- }
- }
- void setAppropriateBrightness(){
- for(int i = 0; i < 3; i++){
- digitalWrite(i+9, brightness[i]);
- }
- }
- void loop() {
- moveNumbers();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement