Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define pinA 2
- #define pinB 3
- #define pinC 4
- #define pinD 5
- #define pinE 6
- #define pinF 7
- #define pinG 8
- #define pinD1 9
- #define pinD2 10
- #define pinD3 11
- #define pinD4 12
- char TEXT[6];
- char toDisplay[4];
- int BRIGHT[4] = {0,0,0,0};
- unsigned long DISP = 1000;
- int currentWorkingIndex = 0;
- 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){
- if(command[0] == 'T'){
- numbersToDisplay(command);
- }
- else if(command[0] == 'D'){
- timeOfDisplaying(command);
- }
- else{
- brightnessOfSegments(command);
- }
- }
- void numbersToDisplay(char *numbersSequence){
- if(checkNumbersToDisplay(numbersSequence)){
- setNumbersToDisplay(numbersSequence);
- }
- }
- boolean checkNumbersToDisplay(char *numbersSequence){
- for(int i = 1; i<7; i++){
- if((numbersSequence[i]>='a' && numbersSequence[i]<='f') ||
- (numbersSequence[i]>='A' && numbersSequence[i]<='F') ||
- (numbersSequence[i]>='0' && numbersSequence[i]<='9')){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- return true;
- }
- void setNumbersToDisplay(char *numbersSequence){
- 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){
- for(int i=1; i<5; i++){
- if(time[i]>='0' && time[i]<='9'){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- return true;
- }
- void brightnessOfSegments(char *brightness){
- if(checkBrightnessOfSegments(brightness)){
- setBrightness(brightness);
- }
- }
- boolean checkBrightnessOfSegments(char *brightness){
- for(int i = 1; i<5; i++){
- if(brightness[i]>='0' && brightness[i]<='2'){
- }
- else{
- Serial.println("Wrong sequence of characters");
- return false;
- }
- }
- return true;
- }
- void setBrightness(char *brightnessString){
- 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{
- flushSerial();
- Serial.println("Unknown command");
- return;
- }
- command = (char*) malloc(howMany*sizeof(char));
- }
- else if(i==(howMany-1)){
- flushSerial();
- }
- command[i] = letter;
- i++;
- read = true;
- }
- if(read){
- checkIfCorrectCommand(command);
- free(command);
- }
- }
- void flushSerial(){
- while(Serial.available()){
- Serial.read();
- }
- }
- 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