Here is the code with the TM1637 stuff removed and the LCD stuff inserted. Note that I had to change one pin as there was a conflict between one of the buzzers and the LCD. Changed the buzzer to pin 8. See comment in code. Don't forget to copy the pitches.h file into a tab. The #include is already fixed. Code tested to the extent that I can without building the entire circuit. You now have a lot more flexibility in terms of what you can display.
#include <hd44780.h>
#include <hd44780ioClass/hd44780_pinIO.h> // Arduino pin i/o class header
#include "pitches.h" // include pitches
const int rs = 11, en = 12, db4 = 4, db5 = 5, db6 = 6, db7 = 7; // for all other devices
hd44780_pinIO lcd(rs, en, db4, db5, db6, db7);
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
// music
int songState = 0;
int melody[] =
{
NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4,
NOTE_C4, NOTE_B3, NOTE_AS3, NOTE_A3,
NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_A3,
NOTE_G3, NOTE_C4, 0,
NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
NOTE_GS3, NOTE_A3, NOTE_F4, NOTE_C4,
NOTE_C4, NOTE_A3, NOTE_AS3, NOTE_AS3,
NOTE_AS3, NOTE_C4, NOTE_D4, 0,
NOTE_AS3, NOTE_G3, NOTE_G3, NOTE_G3,
NOTE_FS3, NOTE_G3, NOTE_E4, NOTE_D4,
NOTE_D4, NOTE_AS3, NOTE_A3, NOTE_A3,
NOTE_A3, NOTE_AS3, NOTE_C4, 0,
NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
NOTE_GS3, NOTE_A3, NOTE_A4, NOTE_F4,
NOTE_F4, NOTE_C4, NOTE_B3, NOTE_G4,
NOTE_G4, NOTE_G4, NOTE_G4, 0,
NOTE_G4, NOTE_E4, NOTE_G4, NOTE_G4,
NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_G4,
NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_C4,
NOTE_B3, NOTE_C4, NOTE_B3, NOTE_C4, 0
};
int tempo[] =
{
8, 16, 8, 16,
8, 16, 8, 16,
16, 16, 16, 8,
16, 8, 3,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 12,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 12,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 16,
12, 17, 17, 17,
8, 12, 17, 17,
17, 8, 16, 8,
16, 8, 16, 8, 1
};
// non blocking setup
// free play
unsigned long previousMillis1 = 0; // time words last changed
const long interval1 = 1500; // interval between changing
// music
unsigned long previousMillis2 = 0; // time last changed
const long interval2 = 100; // interval between notes
int displayStatus = 0; // keep track of what's displayed
int mode = 0; // keep track of game mode -- change to 0 or 1 for different modes
bool countdown = false;
unsigned long previousMillis3 = 0; // time last changed
const long interval3 = 1000; // interval between countdown
int count = 20; // challenge mode timer
void setup()
{
// put your setup code here, to run once:
pinMode(9, INPUT); // setup circuit
pinMode(10, OUTPUT); // setup buzzer 1
pinMode(8, OUTPUT); // setup buzzer 2 // had to change this pin. conflict with LCD
pinMode(2, INPUT); // setup button
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.print("BUZZ REAADY");
delay(1000);
lcd.clear();
}
void loop()
{
// put your main code here, to run repeatedly:
if (mode == 0)
{
// challenge mode
if (digitalRead(2) == HIGH)
{
delay(25);
if (digitalRead(2) == HIGH)
{
countdown = true; // stop the countdown
}
else
{
countdown = false; // stop the countdown
}
}
if (countdown)
{
showCountdown(); // advance countdown
}
}
else
{
// free play
toggleFreePlay();
}
if (digitalRead(10) == HIGH)
{
delay(25);
if (digitalRead(10) == HIGH)
{
while (digitalRead(10) == HIGH)
{
buzz(11, NOTE_B0, 1000 / 24);
}
}
}
else
{
sing();
}
}
void showCountdown()
{
// countdown the time remaining
unsigned long currentMillis = millis(); // current time
if (currentMillis - previousMillis3 >= interval3)
{
previousMillis3 = currentMillis;
--count;
showNumber(count);
if (count == 0)
{
// game over
countdown = false;
count = 20;
// reset countdown
// buzz 3 times
buzz(11, NOTE_B0, 1000 / 24);
delay(100);
buzz(11, NOTE_B0, 1000 / 24);
delay(100);
buzz(11, NOTE_B0, 1000 / 24);
}
}
}
void showNumber(int number)
{
lcd.clear();
lcd.setCursor(4,0);
lcd.print("-- ");
lcd.setCursor(7,0);
lcd.print(number);
lcd.print(" --");
}
void toggleFreePlay()
{
// scroll between words without blocking
unsigned long currentMillis = millis(); // current time
if (currentMillis - previousMillis1 >= interval1)
{
previousMillis1 = currentMillis;
if (displayStatus == 1)
showPlay();
else
showFree();
}
}
void showPlay()
{
// write "PLAY" to the display
lcd.clear();
lcd.setCursor(6,0);
lcd.print("PLAY");
displayStatus = 2;
}
void showFree()
{
// write "Free" to the display
lcd.clear();
lcd.setCursor(6,1);
lcd.print("FREE");
displayStatus = 1;
}
void buzz(int targetPin, long frequency, long length)
{
/* Buzzer example function by Rob Faludi
http://www.faludi.com
https://gist.github.com/AnthonyDiGirolamo/1405180
*/
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i = 0; i < numCycles; i++) // for the calculated length of time...
{
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphragm
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphragm
delayMicroseconds(delayValue); // wait again for the calculated delay value
}
}
void sing()
{
// play the song in a non blocking way
unsigned long currentMillis = millis();
if (currentMillis - previousMillis2 >= interval2)
{
previousMillis2 = currentMillis;
int noteDuration = 1000 / tempo[songState];
buzz(10, melody[songState], noteDuration);
int pauseBetweenNotes = noteDuration;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(10, 0, noteDuration);
++songState;
// start song again if finished
if (songState > 79)
{
songState = 14; // skip intro
}
}
}
Have fun, Charlie.