Guest User

Untitled

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. #define backlightPin 10
  4. #define buttonPin 11
  5. #define ledPin 12
  6.  
  7. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  8. String lastMsg = "";
  9.  
  10. long lastPressed = 0;
  11. long lastMsgTime = 0;
  12.  
  13. int page = 0; //current screen page
  14.  
  15. void setup() {
  16.     Serial.begin(9600);
  17.     // set up the LCD's number of columns and rows:
  18.     lcd.begin(16, 2);
  19.  
  20.     pinMode(backlightPin, OUTPUT);
  21.     pinMode(buttonPin, INPUT);
  22.     pinMode(ledPin, OUTPUT);
  23.     digitalWrite(backlightPin, HIGH);
  24. }
  25.  
  26. void loop() {
  27.     listen();
  28. }
  29.  
  30. void doStuff() {
  31.     if (digitalRead(buttonPin) == LOW && (millis() - lastPressed) > 1000) {
  32.         page ^= 1;
  33.         show((page == 0) ? lastMsg : String(lastMsgTime)); //toggle msg/time page
  34.         lastPressed = millis();
  35.     }
  36. }
  37.  
  38. void listen() {
  39.     String string = "";
  40.     static byte c;
  41.  
  42.     while (1) {
  43.         doStuff();
  44.        
  45.         if (!Serial.available()) continue;
  46.  
  47.         c = Serial.read();
  48.         if (c != '\n') string += char(c);
  49.         else {
  50.             lastMsg = string;
  51.             lastMsgTime = millis() / 1000;
  52.             Serial.println(string);
  53.             show((page == 0) ? string : String(lastMsgTime)); //update the display
  54.             parse(string);
  55.             flash();
  56.             string = "";
  57.         }
  58.     }
  59. }
  60.  
  61. void show(String text) {
  62.     lcd.clear();
  63.     if (text.length() > 16) {
  64.         String first = text.substring(0, 16);
  65.         String second = text.substring(16);
  66.         lcd.home();
  67.         lcd.print(first);
  68.         lcd.setCursor(0, 1);
  69.         lcd.print(second);
  70.     } else {
  71.         lcd.home();
  72.         lcd.print(text);
  73.     }
  74. }
  75.  
  76. void parse(String input) {
  77.     if (input == "light on") digitalWrite(ledPin, HIGH);
  78.     if (input == "light off") digitalWrite(ledPin, LOW);
  79. }
  80.  
  81. void flash() {
  82.     for (int x = 0; x < 3; x++) {
  83.         for (int i = 255; i >= 0; i--) {
  84.             analogWrite(backlightPin, i);
  85.             delay(1);
  86.         }
  87.         for (int i = 0; i < 255; i++) {
  88.             analogWrite(backlightPin, i);
  89.             delay(1);
  90.         }
  91.     }
  92. }
Add Comment
Please, Sign In to add comment