Advertisement
Coriic

ARDUINO FINAL

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