// include the library code: #include #include #include #define isdigit(n) (n >= '0' && n <= '9') #define OCTAVE_OFFSET 0 #define TONE_PIN 8 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 int notes[] = { 0, NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7 }; //CUSTOM LCD CHARACTERS uint8_t custom_0[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}; //blank uint8_t custom_1[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF}; //1line uint8_t custom_2[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF}; //2line uint8_t custom_3[8] = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF}; //3line uint8_t custom_4[8] = { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //4line uint8_t custom_5[8] = { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //5line uint8_t custom_6[8] = { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //6line redundant. // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); const int analogPin = 0; float val = 0; unsigned int displayval = 0; unsigned int actualval = 0; unsigned int flash = 0; unsigned int last = 0; unsigned int top = 0; unsigned int check = 0; unsigned char PIN = 0; Tone tone1; //char *song = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6"; char *song = "Drunken Sailor:d=8,o=6,b=240:4d,d,d,4d,d,d,4d,4g5,4b5,4d,4c,c,c,4c,c,c,4c,4f5,4a5,4c,4d,d,d,d,4d,d,d,4e,4f,4g,4f,4d,4c,4a5,2g5,2g5"; char *chime = "Hockey:d=8;o=5,b=240:f,g,a,4c6,a,2c6"; HughesyShiftBrite sb; #define NumLEDs 25 #define NumChannels 3 /* 6 = Data 7 = Latch 9 = Enable 10 = Clock */ int maxVal = 1023; int count = 0; int currentVal = 0; int currentChannel = 0; int LEDChannels[NumLEDs][NumChannels] = {0}; int displayValue = 20; void setup() { // setup ladder colours LEDChannels[0][0] = 1023; LEDChannels[0][1] = 0; LEDChannels[0][2] = 1023; LEDChannels[1][0] = 512; LEDChannels[1][1] = 0; LEDChannels[1][2] = 1023; LEDChannels[2][0] = 0; LEDChannels[2][0] = 0; LEDChannels[2][2] = 1023; LEDChannels[3][0] = 0; LEDChannels[3][1] = 1023; LEDChannels[3][2] = 512; LEDChannels[4][0] = 0; LEDChannels[4][1] = 1023; LEDChannels[4][2] = 0; LEDChannels[5][0] = 512; LEDChannels[5][1] = 1023; LEDChannels[5][2] = 0; LEDChannels[6][0] = 1023; LEDChannels[6][1] = 1023; LEDChannels[6][2] = 0; LEDChannels[7][0] = 1023; LEDChannels[7][1] = 512; LEDChannels[7][2] = 0; LEDChannels[8][0] = 1023; LEDChannels[8][1] = 0; LEDChannels[8][2] = 0; sb = HughesyShiftBrite(6,7,9,10); // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.setCursor(2,0); lcd.print("Wine-O-Meter"); lcd.createChar(0, custom_0); //send the custom characters to the LCD lcd.createChar(1, custom_1); lcd.createChar(2, custom_2); lcd.createChar(3, custom_3); lcd.createChar(4, custom_4); lcd.createChar(5, custom_5); lcd.createChar(6, custom_6); pinMode(PIN,INPUT); tone1.begin(8); play_rtttl(song); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): // print the number of seconds since reset: // lcd.print(millis()/1000); actualval = analogRead(PIN); // baseline if(actualval > 260) { val = actualval - 260; } else { val = 0; } displayval = val; lcd.clear(); lcd.setCursor(0, 1); lcd.print(displayval); if(val > 0) { lcd.setCursor(4,1); lcd.write(0); } if(val > 150) { lcd.setCursor(5,1); lcd.write(1); } if(val > 300) { lcd.setCursor(6,1); lcd.write(2); } if(val > 450) { lcd.setCursor(7,1); lcd.write(3); } if(val > 600) { lcd.setCursor(8,1); lcd.write(4); } if(val > 750) { lcd.setCursor(9,1); lcd.write(5); } if(val > 900) { lcd.setCursor(10,1); lcd.write(6); } //tone1.stop(); setLights(((val-75)/875)*25); if(val > top){ top = val; //play_rtttl(chime); lcd.setCursor(0,0); lcd.print("*New High Score*"); } else { flash = flash + 1; if(flash > 3) { lcd.setCursor(0,0); lcd.print("High Score - "); lcd.print(top); } else { lcd.setCursor(0,0); lcd.print(" Wine-O-Meter "); } if(flash > 6) { flash = 0; } } delay(500); } void setLights(float value) { for (int h = 0;h<=NumLEDs;h++) { int i = 8; if(h>2) i = 7; if(h>5) i = 6; if(h>8) i = 5; if(h>10) i = 4; if(h>14) i = 3; if(h>17) i = 2; if(h>20) i = 1; if(h>23) i = 0; if(NumLEDs-h < value) { sb.sendColour(LEDChannels[i][0],LEDChannels[i][1],LEDChannels[i][2]); } else { sb.sendColour(0,0,0); } } sb.latch(); } void play_rtttl(char *p) { // Absolutely no error checking in here byte default_dur = 4; byte default_oct = 6; int bpm = 63; int num; long wholenote; long duration; byte note; byte scale; // format: d=N,o=N,b=NNN: // find the start (skip name, etc) while(*p != ':') p++; // ignore name p++; // skip ':' // get default duration if(*p == 'd') { p++; p++; // skip "d=" num = 0; while(isdigit(*p)) { num = (num * 10) + (*p++ - '0'); } if(num > 0) default_dur = num; p++; // skip comma } // get default octave if(*p == 'o') { p++; p++; // skip "o=" num = *p++ - '0'; if(num >= 3 && num <=7) default_oct = num; p++; // skip comma } // get BPM if(*p == 'b') { p++; p++; // skip "b=" num = 0; while(isdigit(*p)) { num = (num * 10) + (*p++ - '0'); } bpm = num; p++; // skip colon } // BPM usually expresses the number of quarter notes per minute wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in milliseconds) // now begin note loop while(*p) { // first, get note duration, if available num = 0; while(isdigit(*p)) { num = (num * 10) + (*p++ - '0'); } if(num) duration = wholenote / num; else duration = wholenote / default_dur; // we will need to check if we are a dotted note after // now get the note note = 0; switch(*p) { case 'c': note = 1; break; case 'd': note = 3; break; case 'e': note = 5; break; case 'f': note = 6; break; case 'g': note = 8; break; case 'a': note = 10; break; case 'b': note = 12; break; case 'p': default: note = 0; } p++; // now, get optional '#' sharp if(*p == '#') { note++; p++; } // now, get optional '.' dotted note if(*p == '.') { duration += duration/2; p++; } // now, get scale if(isdigit(*p)) { scale = *p - '0'; p++; } else { scale = default_oct; } scale += OCTAVE_OFFSET; if(*p == ',') p++; // skip comma for next note (or we may be at the end) // now play the note if(note) { tone1.play(notes[(scale - 4) * 12 + note]); delay(duration); tone1.stop(); } else { delay(duration); } } }