Virtual Pet

Here are some videos of how my pet came out.

As a previous post may have mentioned, the idea was to display the pet on the lcd screen, and make it react to knocks.

In this^ video you can see that the screen is displaying basically whatever it wants to, whether I knock or not.

In this^ video, I am printing the analog value that the Arduino is reading from the piezo sensor. It is suppose to mark each “Knock!” and then keep count. You can see that even without touching the knock sensor, the analog values continue to fluctuate.

Working on it for close to 3 weeks, this is as far as I have gotten with the project. After some research and some input from a TA, it seems to be because the sensor was so cheap! (It may have something to do with the Canal st location i bought it at:http://www.nycresistor.com/2009/09/16/269-electronics/)

Here is the code:


/* 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);
}

view raw

v_pet.ino

hosted with ❤ by GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *