Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #define backlightPin 10
- #define buttonPin 11
- #define ledPin 12
- LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
- String lastMsg = "";
- long lastPressed = 0;
- long lastMsgTime = 0;
- int page = 0; //current screen page
- void setup() {
- Serial.begin(9600);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- pinMode(backlightPin, OUTPUT);
- pinMode(buttonPin, INPUT);
- pinMode(ledPin, OUTPUT);
- digitalWrite(backlightPin, HIGH);
- }
- void loop() {
- listen();
- }
- void doStuff() {
- if (digitalRead(buttonPin) == LOW && (millis() - lastPressed) > 1000) {
- page ^= 1;
- show((page == 0) ? lastMsg : String(lastMsgTime)); //toggle msg/time page
- lastPressed = millis();
- }
- }
- void listen() {
- String string = "";
- static byte c;
- while (1) {
- doStuff();
- if (!Serial.available()) continue;
- c = Serial.read();
- if (c != '\n') string += char(c);
- else {
- lastMsg = string;
- lastMsgTime = millis() / 1000;
- Serial.println(string);
- show((page == 0) ? string : String(lastMsgTime)); //update the display
- parse(string);
- flash();
- string = "";
- }
- }
- }
- void show(String text) {
- lcd.clear();
- if (text.length() > 16) {
- String first = text.substring(0, 16);
- String second = text.substring(16);
- lcd.home();
- lcd.print(first);
- lcd.setCursor(0, 1);
- lcd.print(second);
- } else {
- lcd.home();
- lcd.print(text);
- }
- }
- void parse(String input) {
- if (input == "light on") digitalWrite(ledPin, HIGH);
- if (input == "light off") digitalWrite(ledPin, LOW);
- }
- void flash() {
- for (int x = 0; x < 3; x++) {
- for (int i = 255; i >= 0; i--) {
- analogWrite(backlightPin, i);
- delay(1);
- }
- for (int i = 0; i < 255; i++) {
- analogWrite(backlightPin, i);
- delay(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment