Advertisement
lukicdarkoo

Boilerplate for Arduino LCD Keypad Shield

Dec 1st, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. // Boilerplate for Arduino LCD Keypad Shield
  2. // Create custom character: https://omerk.github.io/lcdchargen/
  3. // String reference: https://www.arduino.cc/en/Reference/StringObject
  4. // LCD reference: https://www.arduino.cc/en/Reference/LiquidCrystal
  5. // Stream reference: http://playground.arduino.cc/Main/StreamingOutput
  6.  
  7. #define _BYTE(a)    _BYTE_CODE(a)
  8. #define _HEX(a)     _BASED(a, HEX)
  9. #define _DEC(a)     _BASED(a, DEC)
  10. #define _OCT(a)     _BASED(a, OCT)
  11. #define _BIN(a)     _BASED(a, BIN)
  12. template<class T> inline Print &operator <<(Print &stream, T arg) { stream.print(arg); return stream; } struct _BASED { long val; int base; _BASED(long v, int b): val(v), base(b) {}}; struct _BYTE_CODE { byte val; _BYTE_CODE(byte v) : val(v) {} }; inline Print &operator <<(Print &obj, const _BYTE_CODE &arg) { obj.write(arg.val); return obj; }  inline Print &operator <<(Print &obj, const _BASED &arg) { obj.print(arg.val, arg.base); return obj; } struct _FLOAT { float val; int digits; _FLOAT(double v, int d): val(v), digits(d) {}}; inline Print &operator <<(Print &obj, const _FLOAT &arg) { obj.print(arg.val, arg.digits); return obj; } enum _EndLineCode { endl }; inline Print &operator <<(Print &obj, _EndLineCode arg) { obj.println(); return obj; }
  13.  
  14. #include <LiquidCrystal.h>
  15. #define TIMEOUT 10
  16.  
  17. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  18.  
  19. byte smiley[8] = {
  20.   B00000,
  21.   B10001,
  22.   B00000,
  23.   B00000,
  24.   B10001,
  25.   B01110,
  26.   B00000,
  27. };
  28.  
  29. typedef struct {
  30.   int number;
  31. } Object;
  32.  
  33. void setup() {
  34.   // Initialize Serial
  35.   Serial.begin(9600);
  36.   Serial.setTimeout(TIMEOUT);
  37.  
  38.   // Initialize LCD and special characters
  39.   lcd.createChar(0, smiley);
  40.   lcd.begin(16, 2);
  41.   lcd.write((byte)0);
  42.  
  43.   // Generate completely random
  44.   randomSeed(analogRead(1));
  45.  
  46.   // Split string
  47.   String t = "a b c d e";
  48.   String splited[5];
  49.   int len;
  50.   splitString(splited, &len, t, " ");
  51.   for (int i = 0; i < len; i++) {
  52.     lcd.write(String(i+1).c_str());
  53.     lcd.write(splited[i].c_str());
  54.   }
  55. }
  56.  
  57. void loop() {
  58.   if (Serial.available() > 0) {
  59.      
  60.   }
  61. }
  62.  
  63. char readKey() {
  64.   short tolerance = 5;
  65.   short value = analogRead(0);
  66.   short values[] = {639, 408, 255, 99, 0};
  67.   char out[]  = {'s', 'l', 'd', 'u', 'r'};
  68.   for (int i = 0; i < 5; i++) {
  69.     if (values[i] - tolerance < value && values[i] + tolerance > value) {
  70.       return out[i];
  71.     }
  72.   }
  73.  
  74.   return '-';
  75. }
  76.  
  77. String getRandomString(int n) {
  78.     // Awful optimisation, but easy to scale
  79.     char charset[] = "0123456789"
  80.                      "abcdefghijklmnopqrstuvwxyz"
  81.                      "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  82.     String randomString = "";
  83.     for (int i = 0; i < n; i++) {
  84.       randomString += String(charset[random(0, strlen(charset))]);
  85.     }
  86.     return randomString;
  87. }
  88.  
  89. void splitString(String *result, int *len, String source, String delimiter) {
  90.   int i = 0;
  91.   int endOfString;
  92.   while ((endOfString = source.indexOf(delimiter)) > 0) {
  93.     result[i++] = source.substring(0, endOfString);
  94.     source.remove(0, endOfString + 1);
  95.   }
  96.   result[i++] = source;
  97.   *len = i;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement