Advertisement
georgeB96

Untitled

Apr 7th, 2022
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Showing numbers, chars and phrases
  3.                             A (seg[0] in this project)
  4.                            ---
  5.   F (seg[5] in this project) |   | B (seg[1] in this project)
  6.                           |   |
  7.                            --- G (seg[6] in this project)
  8.   E (seg[4] in this project) |   |
  9.                           |   | C (seg[2] in this project)
  10.                            ---  . dot or dicimal (seg[7] in this project)
  11.                             D (seg[3] in this project)
  12. */
  13. #define A 11
  14. #define B 10
  15. #define C 9
  16. #define D 8
  17. #define E 7
  18. #define F 6
  19. #define G 5
  20. #define DP 12 // decimal
  21. #define common_cathode 0
  22. #define common_anode 1
  23. bool segMode = common_cathode; // set this to your segment type, my segment is common_cathode
  24. int seg[] {A, B, C, D, E, F, G, DP}; // segment pins
  25. byte chars = 35; // max value in the array "Chars"
  26. byte Chars[35][9] {  // LUT ARRAY
  27.   {48, 1, 1, 1, 1, 1, 1, 0, 0}, //0
  28.   {49, 0, 1, 1, 0, 0, 0, 0, 0}, //1
  29.   {50, 1, 1, 0, 1, 1, 0, 1, 0}, //2
  30.   {51, 1, 1, 1, 1, 0, 0, 1, 0}, //3
  31.   {52, 0, 1, 1, 0, 0, 1, 1, 0}, //4
  32.   {53, 1, 0, 1, 1, 0, 1, 1, 0}, //5
  33.   {54, 1, 0, 1, 1, 1, 1, 1, 0}, //6
  34.   {55, 1, 1, 1, 0, 0, 0, 0, 0}, //7
  35.   {56, 1, 1, 1, 1, 1, 1, 1, 0}, //8
  36.   {57, 1, 1, 1, 1, 0, 1, 1, 0}, //9
  37.   {65, 1, 1, 1, 0, 1, 1, 1, 0}, //A/10
  38.   {98, 0, 0, 1, 1, 1, 1, 1, 0}, //b/11
  39.   {67, 1, 0, 0, 1, 1, 1, 0, 0}, //C/12
  40.   {100, 0, 1, 1, 1, 1, 0, 1, 0}, //d/13
  41.   {69, 1, 0, 0, 1, 1, 1, 1, 0}, //E/14
  42.   {70, 1, 0, 0, 0, 1, 1, 1, 0}, //F/15
  43.   {71, 1, 0, 1, 1, 1, 1, 1, 0}, //G/16
  44.   {72, 0, 1, 1, 0, 1, 1, 1, 0}, //H/17
  45.   {73, 0, 0, 0, 0, 1, 1, 0, 0}, //I/18
  46.   {74, 0, 1, 1, 1, 1, 0, 0, 0}, //J/19
  47.   {76, 0, 0, 0, 1, 1, 1, 0, 0}, //L/20
  48.   {110, 0, 0, 1, 0, 1, 0, 1, 0}, //n/21
  49.   {111, 0, 0, 1, 1, 1, 0, 1, 0}, //o/22
  50.   {80, 1, 1, 0, 0, 1, 1, 1, 0}, //P/23
  51.   {113, 1, 1, 1, 0, 0, 1, 1, 0}, //q/24
  52.   {114, 0, 0, 0, 0, 1, 0, 1, 0}, //r/25
  53.   {83, 1, 0, 1, 1, 0, 1, 1, 0}, //S/26   looks like number 5
  54.   {116, 0, 0, 0, 1, 1, 1, 1, 0}, //t/27
  55.   {85, 0, 1, 1, 1, 1, 1, 0, 0}, //U/28
  56.   {121, 0, 1, 1, 1, 0, 1, 1, 0}, //y/29
  57.   {45, 0, 0, 0, 0, 0, 0, 1, 0}, //-/30
  58.   {46, 0, 0, 0, 0, 0, 0, 0, 1}, //./31
  59.   {93, 1, 1, 1, 1, 0, 0, 0, 0}, //]/32
  60.   {91, 1, 0, 0, 1, 1, 1, 0, 0}, //[/33
  61.   {95, 0, 0, 0, 1, 0, 0, 0, 0}, //_/34
  62. };
  63. void setup() {
  64.   Serial.begin(9600);
  65.   Serial.print("\nPress 1 to display all possible numbers and letters.");
  66.   Serial.print("\nPress 2 followed by space and the ASCII code of the number/letter to display");
  67.   Serial.print("\nPress 3 to print 10 random numbers");
  68.   Serial.print("\nPress 4 followed by space and the number you want to display followed by another space");
  69.  
  70.   // set segment pins as OUTPUT
  71.   pinMode(seg[0], OUTPUT);
  72.   pinMode(seg[1], OUTPUT);
  73.   pinMode(seg[2], OUTPUT);
  74.   pinMode(seg[3], OUTPUT);
  75.   pinMode(seg[4], OUTPUT);
  76.   pinMode(seg[5], OUTPUT);
  77.   pinMode(seg[6], OUTPUT);
  78.   pinMode(seg[7], OUTPUT);
  79. }
  80. void setState(bool mode) //sets the hole segment state to "mode"
  81. { for (int i = 0; i <= 6; i++)
  82.   {
  83.     digitalWrite(seg[i], mode);
  84.   }
  85. }
  86. void PrintAscii(int asciiCode) // print any character on the segment ( Note : you can't use capital characters )
  87. {
  88.  
  89.   int charNum = -1;// set search resault to -1
  90.   setState(segMode);//turn off the segment
  91.   for (int i = 0; i < chars ; i++) { //search for the enterd character
  92.     if (asciiCode == Chars[i][0]) { //if the character found
  93.       charNum = i;//set the resault number into charNum ( because this function prints the character using it's number in the array )
  94.     }
  95.   }
  96.   if (charNum == -1 ) // if the character not found
  97.   {
  98.     for (int i = 0; i <= 6; i++)
  99.     {
  100.       digitalWrite(seg[i], HIGH);
  101.       delay(100);
  102.       digitalWrite(seg[i], LOW);
  103.     }
  104.     for (int i = 0; i <= 2; i++)
  105.     {
  106.       delay(100);
  107.       setState(HIGH);
  108.       delay(100);
  109.       setState(LOW);
  110.     }
  111.   } else // else if the character found print it
  112.   {
  113.     for (int i = 0; i < 8; i++)
  114.     { digitalWrite(seg[i], Chars[charNum][i + 1]);
  115.     }
  116.   }
  117. }
  118. void Print(int num) // print any number on the segment
  119. {
  120.   setState(segMode);//turn off the segment
  121.   if (num > chars || num < 0 ) // if the number is not declared
  122.   {
  123.     for (int i = 0; i <= 6; i++)
  124.     {
  125.       digitalWrite(seg[i], HIGH);
  126.       delay(100);
  127.       digitalWrite(seg[i], LOW);
  128.     }
  129.     for (int i = 0; i <= 2; i++)
  130.     {
  131.       delay(100);
  132.       setState(HIGH);
  133.       delay(100);
  134.       setState(LOW);
  135.     }
  136.   } else // else if the number declared, print it
  137.   {
  138.     if (segMode == 0) { //for segment mode
  139.       for (int i = 0; i < 8; i++) {
  140.         digitalWrite(seg[i], Chars[num][i + 1]);
  141.       }
  142.     }
  143.     else {
  144.       for (int i = 0; i < 8; i++) {
  145.         digitalWrite(seg[i], !Chars[num][i + 1]);
  146.       }
  147.     }
  148.   }
  149.  
  150. }
  151.  
  152. void loop() {
  153.  
  154.   //read from keyboard
  155.   char string[32];
  156.   char byteRead;
  157.   //  char asciiNum[4];
  158.   int availableBytes = Serial.available();
  159.   for (int i = 0; i < availableBytes; i++)
  160.   {
  161.     string[i] = Serial.read();
  162.   }
  163.   //  Serial.println(string);
  164.   delay(1000);
  165.  
  166.   // assign switchCase
  167.   int randNumber = random(48, 57);
  168.   int switchCase = string[0] - '0';
  169.   int *numArr;
  170.   char asciiNum[4];
  171.   asciiNum[0] = string[2];
  172.   asciiNum[1] = string[3];
  173.   asciiNum[2] = string[4];
  174.   switch (switchCase) {
  175.     case 1:
  176.       //print everything
  177.       for (int i = 0; i < chars; i++) //print
  178.       {
  179.         Print(i);
  180.         Serial.print("\nDisplaying ASCII character with number:");
  181.         Serial.print(Chars[i][0]);
  182.         delay(10);
  183.       }
  184.       break;
  185.     case 2:
  186.       // print number/letter/symbol of each element using the ascii number
  187.       Serial.println(charArrayToInt(asciiNum));
  188.       PrintAscii(charArrayToInt(asciiNum));
  189.       break;
  190.     case 3:
  191.       //print 10 random number;
  192.       numArr = generateNumbers();
  193.       Serial.println("Displaying 10 random numbers");
  194.       for (int i = 0; i < 11; i++) {
  195.         Serial.print("\nDisplaying ASCII character with number:");
  196.         Serial.print(numArr[i]);
  197.         PrintAscii(numArr[i]);
  198.         delay(1000);
  199.       }
  200.       Serial.println("Done");
  201.       break;
  202.     case 4:
  203.       //print number
  204.       Serial.println("\nDisplaying number digit by digit with 2000ms delay");
  205.       for (int i = 2; i < sizeof(string) - 1; i++) {
  206.         if (isDigit(string[i])) {
  207.           Serial.println(0+ string[i]);
  208.           PrintAscii(0+ string[i]);
  209.           delay(2000);
  210.         } else {
  211.           break;
  212.         }
  213.       }
  214.       setState(segMode);
  215.       Serial.println("Done");
  216.       break;
  217.     case 0:
  218.       //default case
  219.       //    Serial.println("Invalid switch case");
  220.       break;
  221.   }
  222.  
  223. }
  224.  
  225. int * generateNumbers() {
  226.   static int nuArr[11];
  227.   for (int i = 0; i < 11; i++) {
  228.     nuArr[i] = random(48, 57);
  229.   }
  230.   return nuArr;
  231. }
  232.  
  233. int charArrayToInt(char *arr) {
  234.   int i, value, r;
  235.   i = value = 0;
  236.   for (i = 0; i < strlen(arr); ++i) {
  237.     r = arr[i] - '0';
  238.     value = value * 10 + r;
  239.   }
  240.   if (value > 121) {
  241.     value = value / 10;
  242.   }
  243.   return value;
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement