Advertisement
Coriic

Arduino 27.05

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