Advertisement
Coriic

Arduino 28.05 16.35

May 28th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.23 KB | None | 0 0
  1. #define pinA 2
  2. #define pinB 3
  3. #define pinC 4
  4. #define pinD 5
  5. #define pinE 6
  6. #define pinF 7
  7. #define pinG 8
  8. #define pinD1 9
  9. #define pinD2 10
  10. #define pinD3 11
  11. #define pinD4 12
  12.  
  13. char TEXT[6];
  14. char toDisplay[4];
  15. int BRIGHT[4] = {0,0,0,0};
  16. unsigned long DISP = 1000;
  17. int currentWorkingIndex = 0;
  18.  
  19. void setup() {                
  20.   pinMode(pinA, OUTPUT);  
  21.   pinMode(pinB, OUTPUT);
  22.   pinMode(pinC, OUTPUT);
  23.   pinMode(pinD, OUTPUT);
  24.   pinMode(pinE, OUTPUT);
  25.   pinMode(pinF, OUTPUT);
  26.   pinMode(pinG, OUTPUT);
  27.  
  28.   pinMode(pinD1, OUTPUT);
  29.   pinMode(pinD2, OUTPUT);
  30.   pinMode(pinD3, OUTPUT);
  31.   pinMode(pinD4 , OUTPUT);
  32.  
  33.   Serial.begin(9600);
  34. }
  35.  
  36. void checkIfCorrectCommand(char *command){
  37.   if(command[0] == 'T'){
  38.     numbersToDisplay(command);
  39.   }
  40.   else if(command[0] == 'D'){
  41.     timeOfDisplaying(command);
  42.   }
  43.   else{
  44.     brightnessOfSegments(command);
  45.   }
  46. }
  47.  
  48. void numbersToDisplay(char *numbersSequence){
  49.   if(checkNumbersToDisplay(numbersSequence)){
  50.     setNumbersToDisplay(numbersSequence);
  51.   }
  52. }
  53.  
  54. boolean checkNumbersToDisplay(char *numbersSequence){
  55.   for(int i = 1; i<7; i++){
  56.     if((numbersSequence[i]>='a' && numbersSequence[i]<='f') ||
  57.       (numbersSequence[i]>='A' && numbersSequence[i]<='F') ||
  58.       (numbersSequence[i]>='0' && numbersSequence[i]<='9')){
  59.       }
  60.     else{
  61.       Serial.println("Wrong sequence of characters");
  62.       return false;
  63.     }
  64.   }
  65.   return true;
  66. }
  67.  
  68. void setNumbersToDisplay(char *numbersSequence){
  69.   currentWorkingIndex = 0;
  70.   for(int i=1; i<7; i++){
  71.     TEXT[i-1] = numbersSequence[i];
  72.   }
  73. }
  74.  
  75. void timeOfDisplaying(char *time){
  76.   if(checkTimeOfDisplaying(time)){
  77.     DISP = 1000*(time[1]-'0')+100*(time[2]-'0')+10*(time[3]-'0')+(time[4]-'0');
  78.   }
  79. }
  80.  
  81. boolean checkTimeOfDisplaying(char *time){
  82.   for(int i=1; i<5; i++){
  83.     if(time[i]>='0' && time[i]<='9'){
  84.     }
  85.     else{
  86.       Serial.println("Wrong sequence of characters");
  87.       return false;
  88.     }
  89.   }
  90.   return true;
  91. }
  92.  
  93. void brightnessOfSegments(char *brightness){
  94.   if(checkBrightnessOfSegments(brightness)){
  95.     setBrightness(brightness);
  96.   }
  97. }
  98.  
  99. boolean checkBrightnessOfSegments(char *brightness){
  100.   for(int i = 1; i<5; i++){
  101.     if(brightness[i]>='0' && brightness[i]<='2'){
  102.     }
  103.     else{
  104.       Serial.println("Wrong sequence of characters");
  105.       return false;
  106.     }
  107.   }
  108.   return true;
  109. }
  110.  
  111. void setBrightness(char *brightnessString){
  112.   for(int i=1; i<5; i++){
  113.     BRIGHT[i-1] = brightnessString[i] - '0';
  114.   }
  115. }
  116.  
  117. void moveNumbers(){
  118.   receiveCommand();
  119.   for(int i=0; i<3; i++){
  120.     toDisplay[i] = toDisplay[i+1];
  121.   }
  122.   toDisplay[3] = TEXT[currentWorkingIndex++];
  123.   if(currentWorkingIndex == 6){
  124.     currentWorkingIndex = 0;
  125.   }
  126.   displayArray();
  127. }
  128.  
  129. void receiveCommand(){
  130.   char letter, *command;
  131.   int howMany=1, i=0;
  132.   boolean read = false;
  133.   while((Serial.available()>0) && (i<howMany)){
  134.     letter = Serial.read();
  135.     delay(1);
  136.     if(i==0){
  137.       if(letter=='T'){
  138.         howMany = 7;
  139.       }
  140.       else if((letter == 'B') || (letter=='D')){
  141.         howMany = 5;
  142.       }
  143.       else{
  144.         flushSerial();
  145.         Serial.println("Unknown command");
  146.         return;
  147.       }
  148.       command = (char*) malloc(howMany*sizeof(char));
  149.     }
  150.     else if(i==(howMany-1)){
  151.       flushSerial();
  152.     }
  153.     command[i] = letter;
  154.     i++;
  155.     read = true;
  156.   }
  157.   if(read){
  158.     checkIfCorrectCommand(command);
  159.     free(command);
  160.   }
  161. }
  162.  
  163. void flushSerial(){
  164.   while(Serial.available()){
  165.     Serial.read();
  166.   }
  167. }
  168.  
  169. void displayArray(){
  170.   int startTime = millis();
  171.   int endTime = startTime;
  172.   while((endTime - startTime) <= DISP){
  173.     for(int i = 0; i <4; i++){
  174.         disableSegments();
  175.         clearLEDs();
  176.         digitalWrite(i+9, LOW);
  177.         displayOneSegment(i);
  178.         adjustBrightness(i);
  179.         digitalWrite(i+9, HIGH);
  180.     }
  181.     endTime = millis();
  182.   }
  183. }
  184.  
  185. void adjustBrightness(int whichSegment){
  186.   if(BRIGHT[whichSegment] == 1){
  187.     delayMicroseconds(BRIGHT[whichSegment]*400);
  188.   }
  189.   else{
  190.     delay(BRIGHT[whichSegment]);
  191.   }
  192. }
  193.  
  194. void disableSegments(){
  195.   for(int i = 0; i < 4; i++){
  196.     digitalWrite(i+9, HIGH);
  197.   }
  198. }
  199.  
  200. void clearLEDs(){
  201.   digitalWrite(pinA, HIGH);  
  202.   digitalWrite(pinB, HIGH);  
  203.   digitalWrite(pinC, HIGH);  
  204.   digitalWrite(pinD, HIGH);  
  205.   digitalWrite(pinE, HIGH);  
  206.   digitalWrite(pinF, HIGH);  
  207.   digitalWrite(pinG, HIGH);
  208. }
  209.  
  210. void displayOneSegment(int whichOne){
  211.   char sign = toDisplay[whichOne];
  212.   switch (sign){
  213.     case '0':
  214.       digitalWrite(pinA, LOW);  
  215.       digitalWrite(pinB, LOW);  
  216.       digitalWrite(pinC, LOW);  
  217.       digitalWrite(pinD, LOW);  
  218.       digitalWrite(pinE, LOW);  
  219.       digitalWrite(pinF, LOW);  
  220.       digitalWrite(pinG, HIGH);  
  221.       break;
  222.  
  223.     case '1':
  224.       digitalWrite(pinA, HIGH);  
  225.       digitalWrite(pinB, LOW);  
  226.       digitalWrite(pinC, LOW);  
  227.       digitalWrite(pinD, HIGH);  
  228.       digitalWrite(pinE, HIGH);  
  229.       digitalWrite(pinF, HIGH);  
  230.       digitalWrite(pinG, HIGH);    
  231.       break;
  232.  
  233.     case '2':
  234.       digitalWrite(pinA, LOW);  
  235.       digitalWrite(pinB, LOW);  
  236.       digitalWrite(pinC, HIGH);  
  237.       digitalWrite(pinD, LOW);  
  238.       digitalWrite(pinE, LOW);  
  239.       digitalWrite(pinF, HIGH);  
  240.       digitalWrite(pinG, LOW);    
  241.       break;
  242.  
  243.     case '3':
  244.       digitalWrite(pinA, LOW);  
  245.       digitalWrite(pinB, LOW);  
  246.       digitalWrite(pinC, LOW);  
  247.       digitalWrite(pinD, LOW);  
  248.       digitalWrite(pinE, HIGH);  
  249.       digitalWrite(pinF, HIGH);  
  250.       digitalWrite(pinG, LOW);    
  251.       break;
  252.  
  253.     case '4':
  254.       digitalWrite(pinA, HIGH);  
  255.       digitalWrite(pinB, LOW);  
  256.       digitalWrite(pinC, LOW);  
  257.       digitalWrite(pinD, HIGH);  
  258.       digitalWrite(pinE, HIGH);  
  259.       digitalWrite(pinF, LOW);  
  260.       digitalWrite(pinG, LOW);    
  261.       break;
  262.  
  263.     case '5':    
  264.       digitalWrite(pinA, LOW);  
  265.       digitalWrite(pinB, HIGH);  
  266.       digitalWrite(pinC, LOW);  
  267.       digitalWrite(pinD, LOW);  
  268.       digitalWrite(pinE, HIGH);  
  269.       digitalWrite(pinF, LOW);  
  270.       digitalWrite(pinG, LOW);    
  271.       break;
  272.  
  273.     case '6':    
  274.       digitalWrite(pinA, LOW);  
  275.       digitalWrite(pinB, HIGH);  
  276.       digitalWrite(pinC, LOW);  
  277.       digitalWrite(pinD, LOW);  
  278.       digitalWrite(pinE, LOW);  
  279.       digitalWrite(pinF, LOW);  
  280.       digitalWrite(pinG, LOW);    
  281.       break;
  282.  
  283.     case '7':    
  284.       digitalWrite(pinA, LOW);  
  285.       digitalWrite(pinB, LOW);  
  286.       digitalWrite(pinC, LOW);  
  287.       digitalWrite(pinD, HIGH);  
  288.       digitalWrite(pinE, HIGH);  
  289.       digitalWrite(pinF, HIGH);  
  290.       digitalWrite(pinG, HIGH);    
  291.       break;
  292.  
  293.     case '8':
  294.       digitalWrite(pinA, LOW);  
  295.       digitalWrite(pinB, LOW);  
  296.       digitalWrite(pinC, LOW);  
  297.       digitalWrite(pinD, LOW);  
  298.       digitalWrite(pinE, LOW);  
  299.       digitalWrite(pinF, LOW);  
  300.       digitalWrite(pinG, LOW);    
  301.       break;
  302.  
  303.     case '9':  
  304.       digitalWrite(pinA, LOW);  
  305.       digitalWrite(pinB, LOW);  
  306.       digitalWrite(pinC, LOW);  
  307.       digitalWrite(pinD, LOW);  
  308.       digitalWrite(pinE, HIGH);  
  309.       digitalWrite(pinF, LOW);  
  310.       digitalWrite(pinG, LOW);    
  311.       break;
  312.  
  313.     case 'A': case 'a':
  314.       digitalWrite(pinA, LOW);  
  315.       digitalWrite(pinB, LOW);  
  316.       digitalWrite(pinC, LOW);  
  317.       digitalWrite(pinD, HIGH);  
  318.       digitalWrite(pinE, LOW);  
  319.       digitalWrite(pinF, LOW);  
  320.       digitalWrite(pinG, LOW);  
  321.       break;
  322.  
  323.     case 'B': case 'b':
  324.       digitalWrite(pinA, HIGH);  
  325.       digitalWrite(pinB, HIGH);  
  326.       digitalWrite(pinC, LOW);  
  327.       digitalWrite(pinD, LOW);  
  328.       digitalWrite(pinE, LOW);  
  329.       digitalWrite(pinF, LOW);  
  330.       digitalWrite(pinG, LOW);  
  331.       break;
  332.  
  333.     case 'C': case 'c':
  334.       digitalWrite(pinA, LOW);  
  335.       digitalWrite(pinB, HIGH);  
  336.       digitalWrite(pinC, HIGH);  
  337.       digitalWrite(pinD, LOW);  
  338.       digitalWrite(pinE, LOW);  
  339.       digitalWrite(pinF, LOW);  
  340.       digitalWrite(pinG, HIGH);  
  341.       break;
  342.  
  343.     case 'D': case 'd':  
  344.       digitalWrite(pinA, HIGH);  
  345.       digitalWrite(pinB, LOW);  
  346.       digitalWrite(pinC, LOW);  
  347.       digitalWrite(pinD, LOW);  
  348.       digitalWrite(pinE, LOW);  
  349.       digitalWrite(pinF, HIGH);  
  350.       digitalWrite(pinG, LOW);  
  351.       break;
  352.  
  353.     case 'E': case 'e':
  354.       digitalWrite(pinA, LOW);  
  355.       digitalWrite(pinB, HIGH);  
  356.       digitalWrite(pinC, HIGH);  
  357.       digitalWrite(pinD, LOW);  
  358.       digitalWrite(pinE, LOW);  
  359.       digitalWrite(pinF, LOW);  
  360.       digitalWrite(pinG, LOW);  
  361.       break;
  362.  
  363.     case 'F': case'f':    
  364.       digitalWrite(pinA, LOW);  
  365.       digitalWrite(pinB, HIGH);  
  366.       digitalWrite(pinC, HIGH);  
  367.       digitalWrite(pinD, HIGH);  
  368.       digitalWrite(pinE, LOW);  
  369.       digitalWrite(pinF, LOW);  
  370.       digitalWrite(pinG, LOW);  
  371.       break;
  372.    
  373.     default:
  374.       digitalWrite(pinA, HIGH);  
  375.       digitalWrite(pinB, HIGH);  
  376.       digitalWrite(pinC, HIGH);  
  377.       digitalWrite(pinD, HIGH);  
  378.       digitalWrite(pinE, HIGH);  
  379.       digitalWrite(pinF, HIGH);  
  380.       digitalWrite(pinG, HIGH);  
  381.       break;
  382.   }
  383. }
  384.  
  385. void loop() {
  386.   moveNumbers();
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement