Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- // initialize the library by associating any needed LCD interface pin
- // with the arduino pin number it is connected to
- const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
- //LED
- int green, yellow, red, ticks, button_val;
- boolean red_on, yellow_on, green_on = false;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- void setup() {
- init();
- Serial.begin(9600);
- Serial.println("Starting!");
- delay(20);
- // set up the LCD's number of columns and rows:
- lcd.begin(16,2);
- int col = 0; //collumn to write in!
- //greeting:
- String greeting = "Hello, Julian!";
- //fade-in:
- while ((col < 16) && (col < greeting.length())) {
- lcd.setCursor(col, 0);
- lcd.print(greeting.charAt(col));
- delay(200);
- col++;
- }
- delay(2000);
- //fade-out
- col = 15;
- while (col >= 0) {
- lcd.setCursor(col, 0);
- lcd.print(" ");
- delay(200);
- col--;
- }
- //main-display:
- //fancy-firstline fade-in:
- String first_line = "Current state: ";
- String second_line = "G: 0| Y: 0| R: 0";
- col = 0;
- while ((col < 16) && (col < first_line.length())) {
- lcd.setCursor(col, 0);
- lcd.print(first_line.charAt(col));
- delay(200);
- col++;
- }
- col = 0;
- while ((col < 16) && (col < second_line.length())) {
- lcd.setCursor(col, 1);
- lcd.print(second_line.charAt(col));
- delay(200);
- col++;
- }
- //LED:
- green = 8;
- yellow = 9;
- red = 10;
- pinMode(green, OUTPUT);
- pinMode(yellow, OUTPUT);
- pinMode(red, OUTPUT);
- //button:
- pinMode(A2, INPUT);
- //loop:
- while (1) {
- 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)
- button_val = digitalRead(A2);
- if (((b == -1) || (b == 32) || (b == 10)) && (button_val == 0)) { //nothing or Space or NEW LINE -> Ignore and don't print (reduces spam)
- continue;
- }
- char input = char(b);
- //check "nothing" again, since the button could be pressed. This would result in a print of '?', since the upper break will not kick in.
- if ((b != -1)) {
- Serial.println(input);
- }
- //check if the bytes are something we want
- if ((input == 'r') || (input == 'R')) {
- switch_red();
- }
- else if ((input == 'y') || (input == 'Y')) {
- switch_yellow();
- }
- else if ((input == 'g') || (input == 'G')) {
- switch_green();
- }
- //button pressed?
- else if (button_val == 1) {
- int to_switch = random(3);
- Serial.println(to_switch);
- if (to_switch == 0) {
- switch_green();
- } else if (to_switch == 1) {
- switch_yellow();
- } else if (to_switch == 2) {
- switch_red();
- }
- }
- delay(500);
- }
- }
- void switch_red() {
- lcd.setCursor(15, 1);
- if (red_on) {
- digitalWrite(red, LOW);
- lcd.print("0");
- red_on = false;
- } else {
- digitalWrite(red, HIGH);
- lcd.print("1");
- red_on = true;
- }
- }
- void switch_yellow() {
- lcd.setCursor(9,1);
- if (yellow_on) {
- digitalWrite(yellow, LOW);
- lcd.print("0");
- yellow_on = false;
- } else {
- digitalWrite(yellow, HIGH);
- lcd.print("1");
- yellow_on = true;
- }
- }
- void switch_green() {
- lcd.setCursor(3,1);
- if (green_on) {
- digitalWrite(green, LOW);
- lcd.print("0");
- green_on = false;
- } else {
- digitalWrite(green, HIGH);
- lcd.print("1");
- green_on = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement