Advertisement
Guest User

LCD-LED-Controller

a guest
Mar 30th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. // initialize the library by associating any needed LCD interface pin
  4. // with the arduino pin number it is connected to
  5. const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  6.  
  7. //LED
  8. int green, yellow, red, ticks, button_val;
  9. boolean red_on, yellow_on, green_on = false;
  10.  
  11. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  12.  
  13. void setup() {
  14. init();
  15. Serial.begin(9600);
  16. Serial.println("Starting!");
  17. delay(20);
  18.  
  19. // set up the LCD's number of columns and rows:
  20. lcd.begin(16,2);
  21. int col = 0; //collumn to write in!
  22.  
  23. //greeting:
  24. String greeting = "Hello, Julian!";
  25. //fade-in:
  26. while ((col < 16) && (col < greeting.length())) {
  27. lcd.setCursor(col, 0);
  28. lcd.print(greeting.charAt(col));
  29. delay(200);
  30. col++;
  31. }
  32.  
  33. delay(2000);
  34. //fade-out
  35. col = 15;
  36. while (col >= 0) {
  37. lcd.setCursor(col, 0);
  38. lcd.print(" ");
  39. delay(200);
  40. col--;
  41. }
  42.  
  43. //main-display:
  44. //fancy-firstline fade-in:
  45. String first_line = "Current state: ";
  46. String second_line = "G: 0| Y: 0| R: 0";
  47. col = 0;
  48. while ((col < 16) && (col < first_line.length())) {
  49. lcd.setCursor(col, 0);
  50. lcd.print(first_line.charAt(col));
  51. delay(200);
  52. col++;
  53. }
  54.  
  55. col = 0;
  56. while ((col < 16) && (col < second_line.length())) {
  57. lcd.setCursor(col, 1);
  58. lcd.print(second_line.charAt(col));
  59. delay(200);
  60. col++;
  61. }
  62.  
  63. //LED:
  64. green = 8;
  65. yellow = 9;
  66. red = 10;
  67. pinMode(green, OUTPUT);
  68. pinMode(yellow, OUTPUT);
  69. pinMode(red, OUTPUT);
  70.  
  71. //button:
  72. pinMode(A2, INPUT);
  73.  
  74. //loop:
  75. while (1) {
  76. int b = Serial.read(); //funny, you can't just use int OR byte, bc byte and int have different ranges. Use int to get the ASCII-Value correctly (byte is from 0 - 255; so -1 [nothing] reads 255 instead of -1)
  77. button_val = digitalRead(A2);
  78.  
  79. if (((b == -1) || (b == 32) || (b == 10)) && (button_val == 0)) { //nothing or Space or NEW LINE -> Ignore and don't print (reduces spam)
  80. continue;
  81. }
  82. char input = char(b);
  83. //check "nothing" again, since the button could be pressed. This would result in a print of '?', since the upper break will not kick in.
  84. if ((b != -1)) {
  85. Serial.println(input);
  86. }
  87.  
  88. //check if the bytes are something we want
  89. if ((input == 'r') || (input == 'R')) {
  90. switch_red();
  91. }
  92. else if ((input == 'y') || (input == 'Y')) {
  93. switch_yellow();
  94. }
  95. else if ((input == 'g') || (input == 'G')) {
  96. switch_green();
  97. }
  98. //button pressed?
  99. else if (button_val == 1) {
  100. int to_switch = random(3);
  101. Serial.println(to_switch);
  102. if (to_switch == 0) {
  103. switch_green();
  104. } else if (to_switch == 1) {
  105. switch_yellow();
  106. } else if (to_switch == 2) {
  107. switch_red();
  108. }
  109. }
  110. delay(500);
  111. }
  112. }
  113. void switch_red() {
  114. lcd.setCursor(15, 1);
  115. if (red_on) {
  116. digitalWrite(red, LOW);
  117. lcd.print("0");
  118. red_on = false;
  119. } else {
  120. digitalWrite(red, HIGH);
  121. lcd.print("1");
  122. red_on = true;
  123. }
  124. }
  125.  
  126. void switch_yellow() {
  127. lcd.setCursor(9,1);
  128. if (yellow_on) {
  129. digitalWrite(yellow, LOW);
  130. lcd.print("0");
  131. yellow_on = false;
  132. } else {
  133. digitalWrite(yellow, HIGH);
  134. lcd.print("1");
  135. yellow_on = true;
  136. }
  137. }
  138.  
  139. void switch_green() {
  140. lcd.setCursor(3,1);
  141. if (green_on) {
  142. digitalWrite(green, LOW);
  143. lcd.print("0");
  144. green_on = false;
  145. } else {
  146. digitalWrite(green, HIGH);
  147. lcd.print("1");
  148. green_on = true;
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement