Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char TEXT[6];
- char toDisplay[4];
- int BRIGHT[4] = {0,0,0,0};
- unsigned long DISP = 1000;
- int currentWorkingIndex = 0;
- int pinA = 2;
- int pinB = 3;
- int pinC = 4;
- int pinD = 5;
- int pinE = 6;
- int pinF = 7;
- int pinG = 8;
- int pinD1 = 9;
- int pinD2 = 10;
- int pinD3 = 11;
- int pinD4 = 12;
- void setup() {
- pinMode(pinA, OUTPUT);
- pinMode(pinB, OUTPUT);
- pinMode(pinC, OUTPUT);
- pinMode(pinD, OUTPUT);
- pinMode(pinE, OUTPUT);
- pinMode(pinF, OUTPUT);
- pinMode(pinG, OUTPUT);
- pinMode(pinD1, OUTPUT);
- pinMode(pinD2, OUTPUT);
- pinMode(pinD3, OUTPUT);
- pinMode(pinD4 , OUTPUT);
- Serial.begin(9600);
- }
- void checkIfCorrectCommand(char *command){
- char firstCharacter = command[0];
- if(firstCharacter == 'T'){
- numbersToDisplay(command);
- }
- else if(firstCharacter == 'D'){
- timeOfDisplaying(command);
- }
- else if(firstCharacter == 'B'){
- brightnessOfSegments(command);
- }
- else{
- Serial.println("Unknown command");
- }
- }
- void numbersToDisplay(char *numbersSequence){
- if(checkNumbersToDisplay(numbersSequence)){
- setNumbersToDisplay(numbersSequence);
- }
- }
- boolean checkNumbersToDisplay(char *numbersSequence){
- char characterToTest;
- for(int i = 1; i<7; i++){
- characterToTest = numbersSequence[i];
- if((characterToTest>='a' && characterToTest<='f') ||
- (characterToTest>='A' && characterToTest<='F') ||
- (characterToTest>='0' && characterToTest<='9')){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- return true;
- }
- void setNumbersToDisplay(char *numbersSequence){
- char characterToDisplay;
- currentWorkingIndex = 0;
- for(int i=1; i<7; i++){
- TEXT[i-1] = numbersSequence[i];
- }
- }
- void timeOfDisplaying(char *time){
- if(checkTimeOfDisplaying(time)){
- DISP = 1000*(time[1]-'0')+100*(time[2]-'0')+10*(time[3]-'0')+(time[4]-'0');
- }
- }
- boolean checkTimeOfDisplaying(char *time){
- char characterToTest;
- for(int i=1; i<5; i++){
- characterToTest = time[i];
- if((characterToTest>='0' && characterToTest<='9' && i!=0)
- || (characterToTest>='1' && characterToTest<='9' && i==0)){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- return true;
- }
- void brightnessOfSegments(char *brightness){
- if(checkBrightnessOfSegments(brightness)){
- setBrightness(brightness);
- }
- }
- boolean checkBrightnessOfSegments(char *brightness){
- char characterToTest;
- for(int i = 1; i<5; i++){
- characterToTest = brightness[i];
- if(characterToTest>='0' && characterToTest<='2'){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- return true;
- }
- void setBrightness(char *brightnessString){
- char brightnessLevel;
- for(int i=1; i<5; i++){
- BRIGHT[i-1] = brightnessString[i] - '0';
- }
- }
- void moveNumbers(){
- receiveCommand();
- for(int i=0; i<3; i++){
- toDisplay[i] = toDisplay[i+1];
- }
- toDisplay[3] = TEXT[currentWorkingIndex++];
- if(currentWorkingIndex == 6){
- currentWorkingIndex = 0;
- }
- displayArray();
- }
- void receiveCommand(){
- char letter, *command;
- int howMany=1, i=0;
- boolean read = false;
- while((Serial.available()>0) && (i<howMany)){
- letter = Serial.read();
- delay(1);
- if(i==0){
- if(letter=='T'){
- howMany = 7;
- }
- else if((letter == 'B') || (letter=='D')){
- howMany = 5;
- }
- else{
- break;
- }
- command = (char*) malloc(howMany*sizeof(char));
- }
- command[i] = letter;
- i++;
- read = true;
- }
- if(read){
- checkIfCorrectCommand(command);
- }
- free(command);
- }
- void displayArray(){
- int startTime = millis();
- int endTime = startTime;
- while((endTime - startTime) <= DISP){
- for(int i = 0; i <4; i++){
- disableSegments();
- clearLEDs();
- digitalWrite(i+9, LOW);
- displayOneSegment(i);
- adjustBrightness(i);
- digitalWrite(i+9, HIGH);
- }
- endTime = millis();
- }
- }
- void adjustBrightness(int whichSegment){
- if(BRIGHT[whichSegment] == 1){
- delayMicroseconds(BRIGHT[whichSegment]*400);
- }
- else{
- delay(BRIGHT[whichSegment]);
- }
- }
- void disableSegments(){
- for(int i = 0; i < 4; i++){
- digitalWrite(i+9, HIGH);
- }
- }
- void clearLEDs(){
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, HIGH);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, HIGH);
- }
- void displayOneSegment(int whichOne){
- char sign = toDisplay[whichOne];
- switch (sign){
- case '0':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, HIGH);
- break;
- case '1':
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, HIGH);
- break;
- case '2':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, HIGH);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, LOW);
- break;
- case '3':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, LOW);
- break;
- case '4':
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case '5':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case '6':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case '7':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, HIGH);
- break;
- case '8':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case '9':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case 'A': case 'a':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case 'B': case 'b':
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case 'C': case 'c':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, HIGH);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, HIGH);
- break;
- case 'D': case 'd':
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, LOW);
- break;
- case 'E': case 'e':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, HIGH);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- case 'F': case'f':
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, HIGH);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- break;
- default:
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, HIGH);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, HIGH);
- break;
- }
- }
- void loop() {
- moveNumbers();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement