Advertisement
Coriic

Arduino final Sobol

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