Advertisement
Coriic

Untitled

May 27th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. const byte hexadecimalNumbers[16][7] = { { 0,0,0,0,0,0,1}, // = 0
  2. { 1,0,0,1,1,1,1 }, // = 1
  3. { 0,0,1,0,0,1,0 }, // = 2
  4. { 0,0,0,0,1,1,0 }, // = 3
  5. { 1,0,0,1,1,0,0 }, // = 4
  6. { 0,1,0,0,1,0,0 }, // = 5
  7. { 0,1,0,0,0,0,0 }, // = 6
  8. { 0,0,0,1,1,1,1 }, // = 7
  9. { 0,0,0,0,0,0,0 }, // = 8
  10. { 0,0,0,1,1,0,0 }, // = 9
  11. { 0,0,0,1,0,0,0 }, // = A
  12. { 0,0,0,0,0,0,0 }, // = B
  13. { 0,1,1,0,0,0,1 }, // = C
  14. { 0,0,0,0,0,0,1 }, // = D
  15. { 0,1,1,0,0,0,0 }, // = E
  16. { 0,1,1,1,0,0,0 } // = F
  17. };
  18.  
  19. byte currentNumbersToDisplay[6][7];
  20. byte toDisplay[4][7] = {{ 1,1,1,1,1,1,1}, { 1,1,1,1,1,1,1}, { 1,1,1,1,1,1,1}, { 1,1,1,1,1,1,1}};
  21. byte brightness[4] = {LOW, LOW, LOW, LOW};
  22. unsigned long timeDelay = 0;
  23. int currentWorkingIndex = 0;
  24. boolean notRead = true;
  25.  
  26. void setup() {
  27. pinMode(2, OUTPUT);
  28. pinMode(3, OUTPUT);
  29. pinMode(4, OUTPUT);
  30. pinMode(5, OUTPUT);
  31. pinMode(6, OUTPUT);
  32. pinMode(7, OUTPUT);
  33. pinMode(8, OUTPUT);
  34.  
  35. pinMode(9, OUTPUT);
  36. pinMode(10, OUTPUT);
  37. pinMode(11, OUTPUT);
  38. pinMode(12, OUTPUT);
  39.  
  40. Serial.begin(9600);
  41. }
  42.  
  43. void checkIfCorrectCommand(char firstCharacter){
  44. if(firstCharacter == 'T'){
  45. numbersToDisplay();
  46. }
  47. else if(firstCharacter == 'D'){
  48. timeOfDisplaying();
  49. }
  50. else if(firstCharacter == 'B'){
  51. brightnessOfSegments();
  52. }
  53. else{
  54. Serial.println("Unknown command");
  55. }
  56. notRead = true;
  57. }
  58.  
  59. void numbersToDisplay(){
  60. String numbersSequence = Serial.readString();
  61. if(checkNumbersToDisplay(numbersSequence)){
  62. setNumbersToDisplay(numbersSequence);
  63. }
  64. }
  65.  
  66. boolean checkNumbersToDisplay(String numbersSequence){
  67. if(numbersSequence.length() == 6){
  68. char characterToTest;
  69. for(int i = 0; i<5; i++){
  70. characterToTest = numbersSequence.charAt(i);
  71. if((characterToTest>='a' && characterToTest<='f') ||
  72. (characterToTest>='A' && characterToTest<='F') ||
  73. (characterToTest>='0' && characterToTest<='9')){
  74. }
  75. else{
  76. Serial.println("Wrong sequence of characters");
  77. return false;
  78. }
  79. }
  80. }
  81. else{
  82. Serial.println("Too few characters");
  83. return false;
  84. }
  85. return true;
  86. }
  87.  
  88. void setNumbersToDisplay(String numbersSequence){
  89. char characterToDisplay;
  90. for(int i=0; i<5; i++){
  91. characterToDisplay = numbersSequence.charAt(i);
  92. copyRowOfMatrix(i, parseCharToInt(characterToDisplay), currentNumbersToDisplay, hexadecimalNumbers);
  93. }
  94. }
  95.  
  96. void copyRowOfMatrix(int dest, int from, byte (*firstArray)[7], byte (*secondArray)[7]){
  97. for(int i = 0; i<6; i++){
  98. (*firstArray)[dest] = (*secondArray)[from];
  99. firstArray++;
  100. secondArray++;
  101. }
  102. }
  103.  
  104. int parseCharToInt(char hexadecimalCharacter){
  105. currentWorkingIndex = 0;
  106. if(hexadecimalCharacter>='0' && hexadecimalCharacter<='9'){
  107. return hexadecimalCharacter - '0';
  108. }
  109. else if(hexadecimalCharacter>='a' && hexadecimalCharacter<='f'){
  110. return hexadecimalCharacter - 'a';
  111. }
  112. else{
  113. return hexadecimalCharacter - 'A';
  114. }
  115. }
  116.  
  117. void timeOfDisplaying(){
  118. String time = Serial.readString();
  119. if(checkTimeOfDisplaying(time)){
  120. timeDelay = time.toInt();
  121. }
  122. }
  123.  
  124. boolean checkTimeOfDisplaying(String time){
  125. if(time.length() == 4){
  126. char characterToTest;
  127. for(int i = 0; i<3; i++){
  128. characterToTest = time.charAt(i);
  129. if((characterToTest>='0' && characterToTest<='9' && i!=0)
  130. || (characterToTest>='1' && characterToTest<='9' && i==0)){
  131. }
  132. else{
  133. Serial.println("Wrong sequence of characters");
  134. return false;
  135. }
  136. }
  137. }
  138. else{
  139. Serial.println("Too few characters");
  140. return false;
  141. }
  142. return true;
  143. }
  144.  
  145. void brightnessOfSegments(){
  146. String brightness = Serial.readString();
  147. if(checkBrightnessOfSegments(brightness)){
  148. setBrightness(brightness);
  149. }
  150. }
  151.  
  152. boolean checkBrightnessOfSegments(String brightness){
  153. if(brightness.length() == 4){
  154. char characterToTest;
  155. for(int i = 0; i<3; i++){
  156. characterToTest = brightness.charAt(i);
  157. if(characterToTest>='0' && characterToTest<='2'){
  158. }
  159. else{
  160. Serial.println("Wrong sequence of characters");
  161. return false;
  162. }
  163. }
  164. }
  165. else{
  166. Serial.println("Too few characters");
  167. return false;
  168. }
  169. return true;
  170. }
  171.  
  172. void setBrightness(String brightnessString){
  173. char brightnessLevel;
  174. for(int i=0; i<3; i++){
  175. brightnessLevel = brightnessString.charAt(i);
  176. if(brightnessLevel == '0'){
  177. brightness[i] = HIGH;
  178. }
  179. else if(brightnessLevel == '1'){
  180. brightness[i] = LOW;
  181. }
  182. else{
  183. brightness[i] = LOW;
  184. }
  185. }
  186. }
  187.  
  188. void moveNumbers(){
  189. if((Serial.available() > 0) && (notRead)){
  190. checkIfCorrectCommand(Serial.read());
  191. notRead = false;
  192. }
  193. for(int i=0; i<2; i++){
  194. copyRowOfMatrix(i, i+1, toDisplay, toDisplay);
  195. }
  196. copyRowOfMatrix(3, currentWorkingIndex++, toDisplay, currentNumbersToDisplay);
  197. if(currentWorkingIndex == 6){
  198. currentWorkingIndex = 0;
  199. }
  200. display();
  201. delay(timeDelay);
  202. }
  203.  
  204. void display(){
  205. for(int i = 0; i < 3; i++){
  206. prepareOneSegment(i);
  207. displayOneSegment(i);
  208. setAppropriateBrightness();
  209. }
  210. }
  211.  
  212. void prepareOneSegment(int whichOne){
  213. for(int i = 0; i < 3; i++){
  214. if(i == whichOne){
  215. digitalWrite(i+9, LOW);
  216. }
  217. else{
  218. digitalWrite(i+9, HIGH);
  219. }
  220. }
  221. }
  222.  
  223. void displayOneSegment(int whichOne){
  224. byte pin = 2;
  225. for (byte segCount = 0; segCount < 7; ++segCount) {
  226. digitalWrite(pin, toDisplay[whichOne][segCount]);
  227. ++pin;
  228. }
  229. }
  230.  
  231. void setAppropriateBrightness(){
  232. for(int i = 0; i < 3; i++){
  233. digitalWrite(i+9, brightness[i]);
  234. }
  235. }
  236.  
  237. void loop() {
  238. moveNumbers();
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement