#include "MD_MAX72xx.h"
#include "RTClib.h"
#define CLK_PIN 52
#define DATA_PIN 51
#define CS_PIN 53
boolean screenImage[9][8][8] =
//0, idle guitar
{{{0,0,0,0,0,0,1,1},
{0,0,0,0,0,1,1,0},
{0,0,0,0,1,1,0,0},
{0,0,1,1,1,0,0,0},
{0,1,0,0,1,0,0,0},
{1,0,1,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0}},
//1, image for backwards guitar
{{1,1,0,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{0,0,1,1,0,0,0,0},
{0,0,0,1,1,1,0,0},
{0,0,0,1,0,0,1,0},
{0,0,0,0,1,1,0,1},
{0,0,0,0,1,0,0,1},
{0,0,0,0,0,1,1,0}},
//2, 1st image for guitar breaking
{{0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,0},
{0,0,0,0,1,1,0,1},
{0,0,1,1,1,0,0,0},
{0,1,0,0,1,0,0,0},
{1,0,1,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,1,0,0,0,0,0}},
//3, 2nd image for guitar breaking
{{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0},
{0,1,0,0,1,1,1,0},
{1,0,1,1,0,0,1,1},
{1,0,0,1,0,0,0,1},
{0,1,1,0,0,0,0,0}},
//4, 3rd image for guitar breaking
{{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,1,1,0,0,0,0},
{0,1,0,0,1,0,0,0},
{1,0,0,1,0,1,1,0},
{0,1,1,0,0,1,1,1}},
//5, image for a snowflake
{{1,0,0,1,1,0,0,1},
{0,1,0,1,1,0,1,0},
{0,0,1,1,1,1,0,0},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{0,0,1,1,1,1,0,0},
{0,1,0,1,1,0,1,0},
{1,0,0,1,1,0,0,1}},
//6, image for fire
{{0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,1,0,1,0,1,0,0},
{0,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,0},
{0,0,1,0,0,1,0,0},
{0,0,0,1,1,0,0,0}},
//7, image of musical note 1
{{0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,0,0},
{0,0,1,0,0,1,0,0},
{0,0,1,0,0,1,0,0},
{0,0,1,0,0,1,0,0},
{0,1,1,0,1,1,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}},
//8, image of musical note 2
{{0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,0,1,0,0},
{0,0,0,1,0,1,0,0},
{0,0,1,1,0,0,0,0},
{0,1,1,1,0,0,0,0},
{0,0,1,0,0,0,0,0}}};
MD_MAX72XX ledMatrix = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, CS_PIN,1);
RTC_DS1307 realClock;
bool isPushed = false;
int playMusicButton = A2;
int fixTempButton = A1;
int fixGuitarNeckButton = A0;
int sleepTime = 22; //The Time the Tamagotchi will stop acting up and "sleep".
int wakeUpTime = 7; //The Time the Tamagotchi will start acting up and "wake".
int minSinceWake;
int nextPlayTime = 0;
int nextFixTempTime = 0;
int nextFixNeckTime = 0;
const int needsToPlayInterval = 180;
const int fixTempInterval = 480;
const int fixNeckInterval = 720;
void setup() {
//Serial.begin(9600);
ledMatrix.begin();
/*if(realClock.begin()){
Serial.println("Clock starting");
}else{
Serial.println("Clock did not Start Properly");
Serial.flush();
abort();
}*/
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
randomSeed(analogRead(7));
}
void loop() {
DateTime lastTimeChecked = realClock.now();
minSinceWake = ((lastTimeChecked.hour() * 60 ) + lastTimeChecked.minute()) - (wakeUpTime * 60);
if ((lastTimeChecked.hour() >= sleepTime) || (lastTimeChecked.hour() < wakeUpTime)){
idleGuitar();
//Serial.println("Sleeping...");
minSinceWake = 0;
nextPlayTime = 0;
nextFixTempTime = 0;
nextFixNeckTime = 0;
}else{
idleGuitar();
if(nextPlayTime == 0){
// Serial.println(random(needsToPlayInterval));
nextPlayTime = random(needsToPlayInterval);
}
if(nextFixTempTime == 0){
// Serial.println(random(fixTempInterval));
nextFixTempTime = random(fixTempInterval);
}
if (nextFixNeckTime == 0){
// Serial.println(random(fixNeckInterval));
nextFixNeckTime = random(fixNeckInterval);
}
if (minSinceWake >= nextPlayTime +30){
wantsToPlay();
nextPlayTime = minSinceWake + needsToPlayInterval + random(needsToPlayInterval/4);
}
if (minSinceWake >= nextFixTempTime + 60 ){
wrongTemp();
nextFixTempTime = minSinceWake + fixTempInterval + random(fixTempInterval/4);
}
if (minSinceWake >= nextFixNeckTime + 120){
guitarBreaking();
nextFixNeckTime = minSinceWake + fixNeckInterval + random(fixNeckInterval/4);
}
}
}
void showImage(int ImageNumber){
for(int row = 0; row <= 7; row ++){
for(int col = 0; col <= 7; col ++){
ledMatrix.setPoint(row,col,screenImage[ImageNumber][row][7-col]);
}
}
}
void idleGuitar(){
showImage(0);
delay(500);
showImage(1);
delay(500);
showImage(0);
delay(500);
showImage(1);
delay(500);
}
void wantsToPlay(){
bool playMusicButtonState;
do{
showImage(7);
delay(500);
showImage(0);
delay(500);
showImage(8);
delay(500);
showImage(0);
delay(500);
playMusicButtonState = digitalRead(playMusicButton);
}while(playMusicButtonState != isPushed);
}
void wrongTemp(){
bool fixTempButtonState;
do{
for(int x = 5; x <= 6; x++){
showImage(x);
delay(1000);
}
fixTempButtonState = digitalRead(fixTempButton);
}while(fixTempButtonState != isPushed);
}
void guitarBreaking(){
bool fixGuitarNeckButtonState;
do{
showImage(0);
delay(1000);
showImage(2);
delay(1000);
showImage(3);
delay(1000);
showImage(4);
delay(1000);
fixGuitarNeckButtonState = digitalRead(fixGuitarNeckButton);
}while(fixGuitarNeckButtonState != isPushed);
}