Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Showing numbers, chars and phrases
- A (seg[0] in this project)
- ---
- F (seg[5] in this project) | | B (seg[1] in this project)
- | |
- --- G (seg[6] in this project)
- E (seg[4] in this project) | |
- | | C (seg[2] in this project)
- --- . dot or dicimal (seg[7] in this project)
- D (seg[3] in this project)
- */
- #define A 11
- #define B 10
- #define C 9
- #define D 8
- #define E 7
- #define F 6
- #define G 5
- #define DP 12 // decimal
- #define common_cathode 0
- #define common_anode 1
- bool segMode = common_cathode; // set this to your segment type, my segment is common_cathode
- int seg[] {A, B, C, D, E, F, G, DP}; // segment pins
- byte chars = 35; // max value in the array "Chars"
- byte Chars[35][9] { // LUT ARRAY
- {48, 1, 1, 1, 1, 1, 1, 0, 0}, //0
- {49, 0, 1, 1, 0, 0, 0, 0, 0}, //1
- {50, 1, 1, 0, 1, 1, 0, 1, 0}, //2
- {51, 1, 1, 1, 1, 0, 0, 1, 0}, //3
- {52, 0, 1, 1, 0, 0, 1, 1, 0}, //4
- {53, 1, 0, 1, 1, 0, 1, 1, 0}, //5
- {54, 1, 0, 1, 1, 1, 1, 1, 0}, //6
- {55, 1, 1, 1, 0, 0, 0, 0, 0}, //7
- {56, 1, 1, 1, 1, 1, 1, 1, 0}, //8
- {57, 1, 1, 1, 1, 0, 1, 1, 0}, //9
- {65, 1, 1, 1, 0, 1, 1, 1, 0}, //A/10
- {98, 0, 0, 1, 1, 1, 1, 1, 0}, //b/11
- {67, 1, 0, 0, 1, 1, 1, 0, 0}, //C/12
- {100, 0, 1, 1, 1, 1, 0, 1, 0}, //d/13
- {69, 1, 0, 0, 1, 1, 1, 1, 0}, //E/14
- {70, 1, 0, 0, 0, 1, 1, 1, 0}, //F/15
- {71, 1, 0, 1, 1, 1, 1, 1, 0}, //G/16
- {72, 0, 1, 1, 0, 1, 1, 1, 0}, //H/17
- {73, 0, 0, 0, 0, 1, 1, 0, 0}, //I/18
- {74, 0, 1, 1, 1, 1, 0, 0, 0}, //J/19
- {76, 0, 0, 0, 1, 1, 1, 0, 0}, //L/20
- {110, 0, 0, 1, 0, 1, 0, 1, 0}, //n/21
- {111, 0, 0, 1, 1, 1, 0, 1, 0}, //o/22
- {80, 1, 1, 0, 0, 1, 1, 1, 0}, //P/23
- {113, 1, 1, 1, 0, 0, 1, 1, 0}, //q/24
- {114, 0, 0, 0, 0, 1, 0, 1, 0}, //r/25
- {83, 1, 0, 1, 1, 0, 1, 1, 0}, //S/26 looks like number 5
- {116, 0, 0, 0, 1, 1, 1, 1, 0}, //t/27
- {85, 0, 1, 1, 1, 1, 1, 0, 0}, //U/28
- {121, 0, 1, 1, 1, 0, 1, 1, 0}, //y/29
- {45, 0, 0, 0, 0, 0, 0, 1, 0}, //-/30
- {46, 0, 0, 0, 0, 0, 0, 0, 1}, //./31
- {93, 1, 1, 1, 1, 0, 0, 0, 0}, //]/32
- {91, 1, 0, 0, 1, 1, 1, 0, 0}, //[/33
- {95, 0, 0, 0, 1, 0, 0, 0, 0}, //_/34
- };
- void setup() {
- Serial.begin(9600);
- Serial.print("\nPress 1 to display all possible numbers and letters.");
- Serial.print("\nPress 2 followed by space and the ASCII code of the number/letter to display");
- Serial.print("\nPress 3 to print 10 random numbers");
- Serial.print("\nPress 4 followed by space and the number you want to display followed by another space");
- // set segment pins as OUTPUT
- pinMode(seg[0], OUTPUT);
- pinMode(seg[1], OUTPUT);
- pinMode(seg[2], OUTPUT);
- pinMode(seg[3], OUTPUT);
- pinMode(seg[4], OUTPUT);
- pinMode(seg[5], OUTPUT);
- pinMode(seg[6], OUTPUT);
- pinMode(seg[7], OUTPUT);
- }
- void setState(bool mode) //sets the hole segment state to "mode"
- { for (int i = 0; i <= 6; i++)
- {
- digitalWrite(seg[i], mode);
- }
- }
- void PrintAscii(int asciiCode) // print any character on the segment ( Note : you can't use capital characters )
- {
- int charNum = -1;// set search resault to -1
- setState(segMode);//turn off the segment
- for (int i = 0; i < chars ; i++) { //search for the enterd character
- if (asciiCode == Chars[i][0]) { //if the character found
- charNum = i;//set the resault number into charNum ( because this function prints the character using it's number in the array )
- }
- }
- if (charNum == -1 ) // if the character not found
- {
- for (int i = 0; i <= 6; i++)
- {
- digitalWrite(seg[i], HIGH);
- delay(100);
- digitalWrite(seg[i], LOW);
- }
- for (int i = 0; i <= 2; i++)
- {
- delay(100);
- setState(HIGH);
- delay(100);
- setState(LOW);
- }
- } else // else if the character found print it
- {
- for (int i = 0; i < 8; i++)
- { digitalWrite(seg[i], Chars[charNum][i + 1]);
- }
- }
- }
- void Print(int num) // print any number on the segment
- {
- setState(segMode);//turn off the segment
- if (num > chars || num < 0 ) // if the number is not declared
- {
- for (int i = 0; i <= 6; i++)
- {
- digitalWrite(seg[i], HIGH);
- delay(100);
- digitalWrite(seg[i], LOW);
- }
- for (int i = 0; i <= 2; i++)
- {
- delay(100);
- setState(HIGH);
- delay(100);
- setState(LOW);
- }
- } else // else if the number declared, print it
- {
- if (segMode == 0) { //for segment mode
- for (int i = 0; i < 8; i++) {
- digitalWrite(seg[i], Chars[num][i + 1]);
- }
- }
- else {
- for (int i = 0; i < 8; i++) {
- digitalWrite(seg[i], !Chars[num][i + 1]);
- }
- }
- }
- }
- void loop() {
- //read from keyboard
- char string[32];
- char byteRead;
- // char asciiNum[4];
- int availableBytes = Serial.available();
- for (int i = 0; i < availableBytes; i++)
- {
- string[i] = Serial.read();
- }
- // Serial.println(string);
- delay(1000);
- // assign switchCase
- int randNumber = random(48, 57);
- int switchCase = string[0] - '0';
- int *numArr;
- char asciiNum[4];
- asciiNum[0] = string[2];
- asciiNum[1] = string[3];
- asciiNum[2] = string[4];
- switch (switchCase) {
- case 1:
- //print everything
- for (int i = 0; i < chars; i++) //print
- {
- Print(i);
- Serial.print("\nDisplaying ASCII character with number:");
- Serial.print(Chars[i][0]);
- delay(10);
- }
- break;
- case 2:
- // print number/letter/symbol of each element using the ascii number
- Serial.println(charArrayToInt(asciiNum));
- PrintAscii(charArrayToInt(asciiNum));
- break;
- case 3:
- //print 10 random number;
- numArr = generateNumbers();
- Serial.println("Displaying 10 random numbers");
- for (int i = 0; i < 11; i++) {
- Serial.print("\nDisplaying ASCII character with number:");
- Serial.print(numArr[i]);
- PrintAscii(numArr[i]);
- delay(1000);
- }
- Serial.println("Done");
- break;
- case 4:
- //print number
- Serial.println("\nDisplaying number digit by digit with 2000ms delay");
- for (int i = 2; i < sizeof(string) - 1; i++) {
- if (isDigit(string[i])) {
- Serial.println(0+ string[i]);
- PrintAscii(0+ string[i]);
- delay(2000);
- } else {
- break;
- }
- }
- setState(segMode);
- Serial.println("Done");
- break;
- case 0:
- //default case
- // Serial.println("Invalid switch case");
- break;
- }
- }
- int * generateNumbers() {
- static int nuArr[11];
- for (int i = 0; i < 11; i++) {
- nuArr[i] = random(48, 57);
- }
- return nuArr;
- }
- int charArrayToInt(char *arr) {
- int i, value, r;
- i = value = 0;
- for (i = 0; i < strlen(arr); ++i) {
- r = arr[i] - '0';
- value = value * 10 + r;
- }
- if (value > 121) {
- value = value / 10;
- }
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement