Skip to content

Instantly share code, notes, and snippets.

@lukicdarkoo
Created December 17, 2015 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukicdarkoo/cff0efab8e04a5e2edd3 to your computer and use it in GitHub Desktop.
Save lukicdarkoo/cff0efab8e04a5e2edd3 to your computer and use it in GitHub Desktop.
Notes for Arduino LCD Keypad Shield
// Notes for Arduino LCD Keypad Shield
// Create custom character: https://omerk.github.io/lcdchargen/
// String reference: https://www.arduino.cc/en/Reference/StringObject
// LCD reference: https://www.arduino.cc/en/Reference/LiquidCrystal
// Stream reference: http://playground.arduino.cc/Main/StreamingOutput
#define _BYTE(a) _BYTE_CODE(a)
#define _HEX(a) _BASED(a, HEX)
#define _DEC(a) _BASED(a, DEC)
#define _OCT(a) _BASED(a, OCT)
#define _BIN(a) _BASED(a, BIN)
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; }
#include <LiquidCrystal.h>
#define TIMEOUT 10
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
byte smiley[8] = {
B00000,
B10001,
B00000,
B00000,
B10001,
B01110,
B00000,
};
typedef struct {
int number;
} Object;
void setup() {
// Initialize Serial
Serial.begin(9600);
Serial.setTimeout(TIMEOUT);
// Initialize LCD and special characters
lcd.createChar(0, smiley);
lcd.begin(16, 2);
lcd.write((byte)0);
// Generate completely random
randomSeed(analogRead(1));
// Split string
String t = "a b c d e";
String splited[5];
int len;
splitString(splited, &len, t, " ");
for (int i = 0; i < len; i++) {
lcd.write(String(i+1).c_str());
lcd.write(splited[i].c_str());
}
}
void loop() {
if (Serial.available() > 0) {
}
}
char readKey() {
short tolerance = 5;
short value = analogRead(0);
short values[] = {639, 408, 255, 99, 0};
char out[] = {'s', 'l', 'd', 'u', 'r'};
for (int i = 0; i < 5; i++) {
if (values[i] - tolerance < value && values[i] + tolerance > value) {
return out[i];
}
}
return '-';
}
String getRandomString(int n) {
// Awful optimisation, but easy to scale
char charset[] = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String randomString = "";
for (int i = 0; i < n; i++) {
randomString += String(charset[random(0, strlen(charset))]);
}
return randomString;
}
void splitString(String *result, int *len, String source, String delimiter) {
int i = 0;
int endOfString;
while ((endOfString = source.indexOf(delimiter)) > 0) {
result[i++] = source.substring(0, endOfString);
source.remove(0, endOfString + 1);
}
result[i++] = source;
*len = i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment