Advertisement
computermuseo

arduino display 7 segment 4 font direct

Apr 18th, 2015
3,917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.40 KB | None | 0 0
  1. boolean DigitOn = LOW;
  2. boolean DigitOff = HIGH;
  3. boolean SegOn=HIGH;
  4. boolean SegOff=LOW;
  5. int DigitPins[] = {2, 3, 4, 5};
  6. int SegmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
  7.  
  8. //looks terrible, but I didn't find a way to copy Arrays or merge them from parts
  9. //N is for numbers and NxP is a number with a decimal point behind
  10. int BLANK[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
  11. int N0[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
  12. int N0P[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH};
  13. int N1[] = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
  14. int N1P[] = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH};
  15. int N2[] = {HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH, LOW};
  16. int N2P[] = {HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH};
  17. int N3[] = {HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, LOW};
  18. int N3P[] = {HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH, HIGH};
  19. int N4[] = {LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, LOW};
  20. int N4P[] = {LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH, HIGH};
  21. int N5[] = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
  22. int N5P[] = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, HIGH};
  23. int N6[] = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
  24. int N6P[] = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
  25. int N7[] = {HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
  26. int N7P[] = {HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW, HIGH};
  27. int N8[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
  28. int N8P[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
  29. int N9[] = {HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
  30. int N9P[] = {HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH};
  31. int MIN[] = {LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW};
  32. //The letters K, M, N, T, V, W, Z are off limits with a 7 segment display
  33. //Some letters like D, G, Q are hard to recognize, as D is like O and G like 6
  34. int A[] = {HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, LOW};
  35. int B[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
  36. int C[] = {HIGH, LOW, LOW, HIGH, HIGH, HIGH, LOW, LOW};
  37. int D[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
  38. int E[] = {HIGH, LOW, LOW, HIGH, HIGH, HIGH, HIGH, LOW};
  39. int F[] = {HIGH, LOW, LOW, LOW, HIGH, HIGH, HIGH, LOW};
  40. int G[] = {HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW};
  41. int H[] = {LOW, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, LOW};
  42. int I[] = {LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW};
  43. int J[] = {LOW, HIGH, HIGH, HIGH, HIGH, LOW, LOW, LOW};
  44. int L[] = {LOW, LOW, LOW, HIGH, HIGH, HIGH, LOW, LOW};
  45. int O[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
  46. int P[] = {HIGH, HIGH, LOW, LOW, HIGH, HIGH, HIGH, LOW};
  47. int Q[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH};
  48. int R[] = {HIGH, HIGH, HIGH, LOW, HIGH, HIGH, HIGH, LOW};
  49. int S[] = {HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
  50. int U[] = {LOW, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, LOW};
  51. int Y[] = {LOW, HIGH, HIGH, HIGH, LOW, HIGH, HIGH, LOW};
  52.  
  53. //Array of pointers for the 4 digits
  54. int* lights[4];
  55.  
  56. //char array coming from the serial interface
  57. //4 numbers or chars, 4 optional decimal points, 1 end-of-line char
  58. char incoming[9] = {};
  59.  
  60. void setup() {
  61. Serial.begin(9600);
  62.  
  63. for (byte digit=0;digit<4;digit++) {
  64. pinMode(DigitPins[digit], OUTPUT);
  65. }
  66. for (byte seg=0;seg<8;seg++) {
  67. pinMode(SegmentPins[seg], OUTPUT);
  68. }
  69. //initialize display with 1.234
  70. lights[0] = N1P;
  71. lights[1] = N2;
  72. lights[2] = N3;
  73. lights[3] = N4;
  74. }
  75.  
  76. void loop() {
  77. //read the numbers and / or chars from the serial interface
  78. if (Serial.available() > 0) {
  79. int i = 0;
  80. //clear the array of char
  81. memset(incoming, 0, sizeof(incoming));
  82. while (Serial.available() > 0 && i < sizeof(incoming) - 1) {
  83. incoming[i] = Serial.read();
  84. i++;
  85. delay(3);
  86. }
  87. Serial.println(incoming);
  88.  
  89. //tmp is for the incoming string and counter for the 4 digits
  90. int counter = -1;
  91. for (int tmp = 0; tmp < 9; tmp++) {
  92. counter++;
  93. switch(incoming[tmp]){
  94. case '0':
  95. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  96. lights[counter] = N0P;
  97. tmp++;
  98. } else {
  99. lights[counter] = N0;
  100. }
  101. break;
  102. case '1':
  103. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  104. lights[counter] = N1P;
  105. tmp++;
  106. } else {
  107. lights[counter] = N1;
  108. }
  109. break;
  110. case '2':
  111. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  112. lights[counter] = N2P;
  113. tmp++;
  114. } else {
  115. lights[counter] = N2;
  116. }
  117. break;
  118. case '3':
  119. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  120. lights[counter] = N3P;
  121. tmp++;
  122. } else {
  123. lights[counter] = N3;
  124. }
  125. break;
  126. case '4':
  127. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  128. lights[counter] = N4P;
  129. tmp++;
  130. } else {
  131. lights[counter] = N4;
  132. }
  133. break;
  134. case '5':
  135. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  136. lights[counter] = N5P;
  137. tmp++;
  138. } else {
  139. lights[counter] = N5;
  140. }
  141. break;
  142. case '6':
  143. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  144. lights[counter] = N6P;
  145. tmp++;
  146. } else {
  147. lights[counter] = N6;
  148. }
  149. break;
  150. case '7':
  151. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  152. lights[counter] = N7P;
  153. tmp++;
  154. } else {
  155. lights[counter] = N7;
  156. }
  157. break;
  158. case '8':
  159. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  160. lights[counter] = N8P;
  161. tmp++;
  162. } else {
  163. lights[counter] = N8;
  164. }
  165. break;
  166. case '9':
  167. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  168. lights[counter] = N9P;
  169. tmp++;
  170. } else {
  171. lights[counter] = N9;
  172. }
  173. break;
  174. case '-':
  175. lights[counter] = MIN;
  176. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  177. tmp++;
  178. }
  179. break;
  180. //with letters the decimal point is ignored!
  181. //if you need it, just write AP, BP etc with HIGH in the last position
  182. case 'a': //falls through to the next case
  183. case 'A':
  184. lights[counter] = A;
  185. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  186. tmp++;
  187. }
  188. break;
  189. case 'b': //falls through to the next case
  190. case 'B':
  191. lights[counter] = B;
  192. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  193. tmp++;
  194. }
  195. break;
  196. case 'c': //falls through to the next case
  197. case 'C':
  198. lights[counter] = C;
  199. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  200. tmp++;
  201. }
  202. break;
  203. case 'd': //falls through to the next case
  204. case 'D':
  205. lights[counter] = D;
  206. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  207. tmp++;
  208. }
  209. break;
  210. case 'e': //falls through to the next case
  211. case 'E':
  212. lights[counter] = E;
  213. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  214. tmp++;
  215. }
  216. break;
  217. case 'f': //falls through to the next case
  218. case 'F':
  219. lights[counter] = F;
  220. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  221. tmp++;
  222. }
  223. break;
  224. case 'g': //falls through to the next case
  225. case 'G':
  226. lights[counter] = G;
  227. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  228. tmp++;
  229. }
  230. break;
  231. case 'h': //falls through to the next case
  232. case 'H':
  233. lights[counter] = H;
  234. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  235. tmp++;
  236. }
  237. break;
  238. case 'i': //falls through to the next case
  239. case 'I':
  240. lights[counter] = I;
  241. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  242. tmp++;
  243. }
  244. break;
  245. case 'j': //falls through to the next case
  246. case 'J':
  247. lights[counter] = J;
  248. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  249. tmp++;
  250. }
  251. break;
  252. case 'l': //falls through to the next case
  253. case 'L':
  254. lights[counter] = L;
  255. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  256. tmp++;
  257. }
  258. break;
  259. case 'o': //falls through to the next case
  260. case 'O':
  261. lights[counter] = O;
  262. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  263. tmp++;
  264. }
  265. break;
  266. case 'p': //falls through to the next case
  267. case 'P':
  268. lights[counter] = P;
  269. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  270. tmp++;
  271. }
  272. break;
  273. case 'q': //falls through to the next case
  274. case 'Q':
  275. lights[counter] = Q;
  276. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  277. tmp++;
  278. }
  279. break;
  280. case 'r': //falls through to the next case
  281. case 'R':
  282. lights[counter] = R;
  283. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  284. tmp++;
  285. }
  286. break;
  287. case 's': //falls through to the next case
  288. case 'S':
  289. lights[counter] = S;
  290. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  291. tmp++;
  292. }
  293. break;
  294. case 'u': //falls through to the next case
  295. case 'U':
  296. lights[counter] = U;
  297. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  298. tmp++;
  299. }
  300. break;
  301. case 'y': //falls through to the next case
  302. case 'Y':
  303. lights[counter] = Y;
  304. if (tmp < 8 && (incoming[tmp + 1] == '.' || incoming[tmp + 1] == ',')) {
  305. tmp++;
  306. }
  307. break;
  308. case 1 ... 43: counter--; break;//special chars are ignored
  309. //44 to 46 are , - .
  310. case 47: counter--; break;//special chars are ignored
  311. //chars between Z and a
  312. case 91 ... 96: counter--; break;//special chars are ignored
  313. case 123 ... 127: counter--; Serial.println("above 122"); break;//special chars are ignored
  314.  
  315. default: lights[counter] = BLANK;
  316. }
  317. } //end for
  318.  
  319. //show the input values
  320. for (int y = 0; y < 4; y++) {
  321. Serial.print(y);
  322. Serial.print(": ");
  323. for (int z = 0; z < 8; z++) {
  324. Serial.print(lights[y][z]);
  325. }
  326. Serial.println("");
  327. }
  328. } //end if, i.e. reading from serial interface
  329.  
  330. //This part of the code is from the library SevSeg by Dean Reading
  331. for (byte seg=0;seg<8;seg++) {
  332. //Turn the relevant segment on
  333. digitalWrite(SegmentPins[seg],SegOn);
  334.  
  335. //For each digit, turn relevant digits on
  336. for (byte digit=0;digit<4;digit++){
  337. if (lights[digit][seg]==1) {
  338. digitalWrite(DigitPins[digit],DigitOn);
  339. }
  340. //delay(200); //Uncomment this to see it in slow motion
  341. }
  342. //Turn all digits off
  343. for (byte digit=0;digit<4;digit++){
  344. digitalWrite(DigitPins[digit],DigitOff);
  345. }
  346.  
  347. //Turn the relevant segment off
  348. digitalWrite(SegmentPins[seg],SegOff);
  349. } //end of for
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement