Created
November 24, 2015 06:55
-
-
Save jessherzog/2707ff270156429b0ba4 to your computer and use it in GitHub Desktop.
Virtual Pet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The circuit: | |
* LCD RS pin to digital pin 12 | |
* LCD Enable pin to digital pin 11 | |
* LCD D4 pin to digital pin 5 | |
* LCD D5 pin to digital pin 4 | |
* LCD D6 pin to digital pin 3 | |
* LCD D7 pin to digital pin 2 | |
* LCD R/W pin to ground | |
* LCD VSS pin to ground | |
* LCD VCC pin to 5V | |
* 10K resistor: | |
* ends to +5V and ground | |
* wiper to LCD VO pin (pin 3) | |
*/ | |
// include the library code: | |
#include <LiquidCrystal.h> | |
// light | |
const int ledPin = 13; | |
// piezo sensor on Analog pin 0 | |
const int knockSensor = A1; | |
// is the knock strong enough ? | |
const int threshold = 400; | |
// changing variables: | |
int knockReads = 0; | |
int ledState = LOW; | |
int counter = 0; | |
// initialize the library with the numbers of the interface pins | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
byte customChar1[8] = { | |
0b01110, | |
0b10011, | |
0b10011, | |
0b11111, | |
0b01110, | |
0b00000, | |
0b00000, | |
0b00000 | |
}; | |
byte customChar2[8] = { | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b10001, | |
0b01110 | |
}; | |
byte customChar3[8] = { | |
0b00000, | |
0b00000, | |
0b01110, | |
0b01010, | |
0b01110, | |
0b00000, | |
0b00000, | |
0b00000 | |
}; | |
byte customChar4[8] = { | |
0b00011, | |
0b00011, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000, | |
0b00000 | |
}; | |
byte customChar5[8] = { | |
0b00100, | |
0b01010, | |
0b01010, | |
0b10001, | |
0b10001, | |
0b01010, | |
0b01010, | |
0b00100 | |
}; | |
byte customChar6[8] = { | |
0b00001, | |
0b00001, | |
0b00010, | |
0b00010, | |
0b00100, | |
0b01100, | |
0b11000, | |
0b00000 | |
}; | |
void setup() { | |
lcd.createChar(0, customChar1); | |
lcd.createChar(1, customChar2); | |
lcd.createChar(3, customChar3); | |
lcd.createChar(4, customChar4); | |
lcd.createChar(5, customChar5); | |
lcd.createChar(6, customChar6); | |
lcd.begin(16, 2); | |
lcd.setCursor(16, 0); | |
lcd.print("("); | |
lcd.print("L"); | |
lcd.print((char)0); | |
lcd.print((char)1); | |
lcd.print((char)0); | |
lcd.print(" )"); | |
lcd.print("L"); | |
lcd.print((char)3); | |
lcd.print("*:"); | |
lcd.print((char)3); | |
lcd.print((char)4); | |
lcd.print((char)5); | |
lcd.setCursor(0, 1); | |
lcd.print (counter); | |
pinMode(ledPin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
knockReads = analogRead(knockSensor); | |
Serial.println(knockReads); | |
if (knockReads >= threshold) { | |
// pet dances! | |
lcd.print("("); | |
lcd.print((char)6); | |
lcd.print((char)0); | |
lcd.print((char)1); | |
lcd.print((char)0); | |
lcd.print(" )"); | |
lcd.print((char)6); | |
lcd.print((char)3); | |
lcd.print("*:"); | |
lcd.print((char)3); | |
lcd.print((char)4); | |
lcd.print((char)5); | |
// update the LED pin itself: | |
digitalWrite(ledPin, ledState); | |
// count the knocks | |
counter++; | |
// if knocked, turn light on | |
ledState = HIGH; | |
Serial.println("Knock!"); | |
// reset threshold | |
knockReads = 0; | |
ledState = LOW; | |
Serial.println(counter); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment