Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NOW FIXED IF THIS IS INDEED THE INSTALLED VERSION.
- // LONGPRESSGREEN to start time edit. then:
- // * press blue to move hour back one (rarely needed)
- // * press red to move minute back one (usual case)
- // * when done, press yellow to end edit mode.
- // DO THIS RIGHT AFTER THE BEGINNING OF THE MINUTE, SINCE IT WILL SEND IT TO :00 SECONDS.
- // IN PROGRESS NOT FULLY WORKING PROBABLY TRYING TO FIX SHORT MINUTES DIGIT IN YELLOW. will test over time.
- // 10.0 now need to fix bugs that (1) z0 and z1 screw up the light pattern, (2) song gets nuked now somehow? // FIXED IT A BIT FOR NOW
- // 9.9 currently under development. ***SEEMS TO WORK NOW! searching for a way to modify clock time with buttons.
- // 9.85 works fine, made motor spin buzz longer and disabled floor cleanup chime at 5:45
- // 9.8 making yellw button show time on 8x8
- // trying to make the ball bounce nicer
- // still seems to be newest now that it's 22 march 2017, we'll see.
- // also installed on 29 june 2018!! current as of then
- // RTC RESET RERUN THEN PLUG IN NEW TIME ~ ~ 57600baud ~ ~ HOW TO DO IT?
- // take out battery from clock.
- // make only power be from computer.
- // make a change to the app and save it.
- // unplug usb so arduino turns off.
- // FAST compile app with the check mark icon.
- // FAST plug in USB immediately (hopefully not need to reset port.) it will bleeeep.
- // [And set its time to... last compiled time? god knows]
- // FAST and do an upload with the arrow button.
- // then while running, place battery back into clock.
- // then maybe do one more usb upload?
- // RTC from RTC examples, see built in to IDC
- #include <Wire.h>
- #include "RTClib.h"
- #include <Time.h> // NOT PUT IN
- //#include <DS1307RTC.h> // NOT PUT IN
- #include "pitches.h"
- #include "Adafruit_LEDBackpack.h"
- #include "Adafruit_GFX.h"
- // FORTHECOLORONE Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
- Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
- // RTC head stuff =========================
- // removed this array for memory --- char daysOfTheWeek[7][1] = {"S", "M", "T", "W", "h", "F", "a"};
- #if defined(ARDUINO_ARCH_SAMD)
- // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
- #define Serial SerialUSB
- #endif
- RTC_DS1307 rtc;
- // end RTC head stuff ======================
- // eric song book
- int maxsongs = 11; //make this be one greater than the index number of the highest song. if songs number from 0-5, maxsongs should be 6. popsong will skip the first one(s) as reserved as not "songs"
- // first the note tones
- int songsnotes[11][25] = {
- {g3,ds3,f3,as2,0,as2,f3,g3,ds3},
- {b3,gs3,e3,cs3},
- {as3,a3,as3,a3,as3,c4,d4,e4,f4},
- {a3,as3,c4,as3,a3,g3,f3,e3,ds3,f3,g3,a3,as3,fs3,f3,ds3},
- {g3, g3, g2, g2, g3, g3, f3, f3, f2, f2, f3, f3, ds3, ds3, ds2, ds2, ds3, ds3, a2, as2,c3,as2,a2,as2,g2 },
- {e3,e3,0,a3,a3,0,d3,d3,g3,g3,fs3,e3,e3,a3,gs3,b3,b3,g3,g3,fs3,fs3},
- {f3,f3,ds3,f3,0,c3,0,c3,f3,as3,a3,f3 },
- {c2,d2,e2,f2,0,g2,a2,as2,c3,0,d3,e3,f3,e3,0,d3,c3,as2,c3},
- {c3,c4,a3,c3,c4,a3,f3,d4,c4,c4,as3,a3,g3,as3,g3,f3,e3,f3},
- {e3,d3,e3,0,d3,c3,b2,a2,gs2,a2},
- {g3,b3,cs4,e4,d4,b3,g3,e3,cs3,cs3,cs3,d3}
- };
- // then the durations
- int songslengths[11][26] = {
- {2,2,2,1,4,2,2,2,1,999}, // reserved big ben
- {2,2,2,1,999}, //reserved
- {16,16,16,16,16,16,16,16,4,999}, // quickie
- {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,999}, // psycho
- {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,2,999}, // chungking express
- {4,4,8,8,8,8,4,3,8,8,8, 3,8,4,4,3,8,8,8,8,8,999}, // futurama
- {8,8,8,8,8,8,8,8,8,8,8,8 ,999}, // funky town
- {8,8,8,2,8,8,8,8,2,8,8,8,8,2,8,8,8,8,2,999}, // cinema paradiso
- {8,8,4,8,8,4,4,8,8,8,8,4,8,16,16,8,8,4}, // tune odd
- {16,16,2,4,6,6,6,6,2,1}, // fugue
- {4,8,8,16,4,8,8,16,16,16,16,8} // simpsons
- };
- const int reservedsongs = 2;
- const int bulb[] = {9,6,11,10}; // originally 13,12,11,10. want this to be 9,6,11,10
- const int bttn[] = {13,8,7,12}; // originally 9,8,7,6. want this to be 13,8,7,12
- const int keysound[] = {65,82,98,131};
- const int speaker = 5;
- const int analogpot = 1;
- // int pattern;
- int lengthdelay =250;
- int cyclenumber=10;
- int counter=1;
- int counterout=1;
- int counterin=1;
- const int motor=3;
- float ballx=5.0;
- float bally=2.0;
- float ballyvel=0.1;
- float ballxvel=0.6;
- const int ballxmin = 0;
- const int ballxmax = 7;
- const int ballymin = 0;
- const int ballymax = 8;
- int utilinteger = 0;
- int z0=0;
- int z1=0;
- int z2=0;
- int z3=0;
- // END HEAD ==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==
- void note(int pitch, int len) {
- int chop=1;
- if (chop==1) {
- tone(speaker, pitch, len);
- delay(len*1.3);
- }
- else {
- for(int ccc = 0; ccc<chop; ccc++) {
- tone(speaker, pitch, len/chop);
- delay((len/chop));
- }
- delay(len/chop);
- }
- }
- void sayhi() {
- note(gs3,250);
- note(e3,100);
- note(e3,100);
- }
- void pip() {
- note(e3,100);
- delay(100);
- }
- void sayhi2() {
- note(e3,100);
- note(gs3,150);
- delay(70);
- note(gs3,100);
- note(b2,150);
- delay(70);
- note(gs3,100);
- note(e3,250);
- }
- void playsong(int whichsong) {
- // iterate over the notes of the melody:
- for (int thisnote = 0; thisnote < 30; thisnote++) {
- // to calculate the note duration, take one second
- // divided by the note type.
- //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
- int mult = map(analogRead(analogpot),0,1023,0.2,3) ; // ***
- int noteDuration = (1500 / songslengths[whichsong][thisnote]) * mult;
- if (noteDuration < 10) {
- break;
- }
- // disabling 3 steps so that this can call eric's note method
- // tone(speaker, songsnotes[whichsong][thisnote], noteDuration);
- // to distinguish the notes, set a minimum time between them.
- // the note's duration + 30% seems to work well:
- // int pauseBetweenNotes = noteDuration * 1.30;
- // delay(pauseBetweenNotes);
- note(songsnotes[whichsong][thisnote], noteDuration);
- noTone(speaker);
- }
- }
- void setup() { // OOOOOOOOOOOOOOOO0000000000000000000000000000088888888000000OOOOOOOOOOOOOOOOO
- // RTC setup stuff
- int nonsense=2;
- #ifndef ESP8266
- while (!Serial); // for Leonardo/Micro/Zero
- #endif
- Serial.begin(57600);
- randomSeed(analogRead(0));
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
- // **++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**++**
- if (! rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- tone(speaker,131,600);
- delay(602);
- tone(speaker,131,100);
- delay(20);
- tone(speaker,101,500);
- delay(20);
- tone(speaker,131,100);
- delay(20);
- tone(speaker,200,900);
- delay(20);
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for exxxample to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- // end RTC setup stuff
- matrix.begin(0x70); // pass in the address, SAME IN COLOR OR NOT COLOR MODELS
- pinMode(motor, OUTPUT);
- pinMode(bulb[0],OUTPUT);
- pinMode(bulb[1],OUTPUT);
- pinMode(bulb[2],OUTPUT);
- pinMode(bulb[3],OUTPUT);
- pinMode(bttn[0],INPUT);
- pinMode(bttn[1],INPUT);
- pinMode(bttn[2],INPUT);
- pinMode(bttn[3],INPUT);
- pinMode(speaker, OUTPUT);
- }
- // END OF SETUP OOOOOOOOOOOOOOOO00000000000OOOOOO0000000000000000000000000000088
- void climbditty(int level, int gap) {
- note(level,100);
- delay(80);
- note(level,80);
- delay(20);
- note(level,80);
- delay(10);
- note(level+gap,125);
- delay(10);
- note(level,80);
- delay(150);
- }
- void showtext(String name) {
- matrix.setTextSize(1);
- matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely
- matrix.setTextColor(LED_ON);
- for (int8_t x=0; x>=-36; x--) {
- matrix.clear();
- matrix.setCursor(x,0);
- matrix.print(" " + name);
- matrix.writeDisplay();
- delay(50);
- }
- }
- void climbsong() {
- const int lowtone=100;
- const int hightone=900;
- String scorecountstring;
- int scorecount=0;
- int level=lowtone;
- while (level<hightone) {
- int gap=random(2,700);
- climbditty(level,gap);
- scorecount++;
- level += gap;
- }
- buzz();
- scorecountstring = String(scorecount);
- showtext(scorecountstring);
- }
- void yay() {
- note(g3,100);
- delay(80);
- note(g3,80);
- delay(20);
- note(g3,80);
- delay(10);
- note(c4,700);
- }
- void boo() {
- note(330,50);
- delay(300);
- note(e2,200);
- delay(50);
- note(c2,600);
- }
- void compubabble(int howmany,int pace) {
- for(int c=1; c<howmany+1;c++) {
- int num=random(33,126);
- char ch = num;
- matrix.drawChar(2, 0, ch, 255, 0, 1);
- matrix.writeDisplay();
- note(random(50,1500),pace);
- //matrix clear it
- matrix.clear();
- matrix.writeDisplay();
- //matrix end clear it
- }
- }
- // GAME GAME GAME STUFF FROM HERE *******
- void show(int key) { // PART OF GAME
- digitalWrite(bulb[key],HIGH);
- note(keysound[key],600);
- digitalWrite(bulb[key],LOW);
- delay(50);
- }
- int getinput() { // PART OF GAME
- int decision=9;
- // wait til press
- while (decision==9) {
- if ( (digitalRead(bttn[0]) == HIGH) or (digitalRead(bttn[1]) == HIGH) or (digitalRead(bttn[2]) == HIGH) or (digitalRead(bttn[3]) == HIGH ) ) {
- if (digitalRead(bttn[0]) == HIGH) {
- decision=0;
- }
- else if (digitalRead(bttn[1]) == HIGH) {
- decision=1;
- }
- else if (digitalRead(bttn[2]) == HIGH) {
- decision=2;
- }
- else if (digitalRead(bttn[3]) == HIGH) {
- decision=3;
- }
- }
- }
- show(decision);
- return decision;
- }
- boolean trial(int sequence[], int glength) { // PART OF GAME
- boolean dotheywin=true;
- int guess;
- for (int countx = 0; countx < glength; countx++) {
- guess=getinput();
- if (guess != sequence[countx]) { // kick out if wrong
- dotheywin=false;
- break;
- }
- }
- // we have gone through the whole loop, or got kicked out
- return dotheywin;
- }
- void playgame() { // PART OF GAME
- int glength = 7;
- int sequence[glength-1];
- boolean theywon;
- flashblank(); // clear the lights to all off.
- // tones to say let's start
- delay(400);
- note(e4,50);
- delay(50);
- note(e4,50);
- delay(50);
- note(e4,50);
- delay(50);
- delay(600);
- // define puzzle
- for(int aa = 0; aa < glength; aa++) {
- sequence[aa] = random(0,4);
- }
- // play puzzle
- for(int bb = 0; bb<glength; bb++) {
- show(sequence[bb]);
- }
- // loop for user to try to match it
- theywon = trial(sequence,glength);
- // evaluate
- if (theywon) {
- yay();
- }
- else if (not theywon) {
- boo();
- }
- }
- // END GAME *****************************
- int popsong(int whichsong) {
- playsong(whichsong);
- whichsong++;
- if (whichsong == maxsongs) {
- whichsong = reservedsongs;
- }
- return whichsong;
- }
- void buzz() {
- analogWrite(motor, 254);
- delay(1000);
- analogWrite(motor, 0);
- delay(1);
- }
- void flashmult(int r0, int r1, int r2, int r3) {
- if (r0==1) {digitalWrite(bulb[0],HIGH); };
- if (r1==1) {digitalWrite(bulb[1],HIGH); };
- if (r2==1) {digitalWrite(bulb[2],HIGH); };
- if (r3==1) {digitalWrite(bulb[3],HIGH); };
- delay(lengthdelay/6);
- }
- void flashblank() {
- digitalWrite(bulb[0],LOW);
- digitalWrite(bulb[1],LOW);
- digitalWrite(bulb[2],LOW);
- digitalWrite(bulb[3],LOW);
- }
- void loop() { // ****** ***** *********** ****** **** **** ***** *****
- DateTime now = rtc.now();
- int prevvalue = 99;
- int prevminute = 99;
- int songrotator = reservedsongs; //XXXXXXX ROTATOR
- boolean tempmuted = true;
- boolean showpic = false;
- String thestring;
- // OXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXO REAL LOOP
- // OXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXO REAL LOOP
- // OXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXO REAL LOOP
- while (true) {
- // POTENTIOMETER INPUT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- lengthdelay = analogRead(analogpot);
- // DATE TIME STUFF ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // dt cycletop
- DateTime now = rtc.now();
- int thishour=now.hour();
- int thisminute=now.minute();
- cyclenumber=thishour;
- // dt every hour detect and chime
- if ((thishour != prevvalue) and ((thishour > 6) and (thishour < 21) and (tempmuted != true) )) {
- buzz();
- // playsong(4); // play the big ben song if that's the method you prefer
- songrotator = popsong(songrotator);
- counter=1;
- buzz();
- // Serial.println('BEEPBEEPBEEP HOUR CHIME');
- }
- tempmuted = false; // avoids the first-time chime each time you upload the app
- // dt every 30min detect and chime
- if((thisminute != prevminute) and (thisminute == 30) and ((thishour > 5) and (thishour < 21))) {
- // here
- note(e3,50);
- delay(50);
- note(e3,50);
- buzz();
- buzz();
- buzz();
- buzz();
- }
- // dt CLEAN UP FLOOR TIME detect and chime **DISABLED WITH 1=2
- if( (1==2) and (thisminute != prevminute) and (thisminute == 45) and (thishour == 17)) {
- // here
- buzz();
- buzz();
- buzz();
- yay();
- delay(500);
- boo();
- boo();
- delay(500);
- yay();
- buzz();
- buzz();
- buzz();
- }
- // dt cyclebottom
- prevvalue=thishour;
- prevminute=thisminute;
- // LIGHTS SHOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- if ((counter % cyclenumber)==0) {
- flashblank();
- if (counterin%1==0) { z3 = !z3;};
- if (counterin%2==0) { z2 = !z2;};
- if (counterin%4==0) { z1 = !z1;};
- if (counterin%8==0) { z0 = !z0;};
- flashmult(z0,z1,z2,z3);
- counterin++;
- }
- // BUTTON INPUT BY USER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // BUTTON-BLUE
- while (digitalRead(bttn[0]) == HIGH ) {
- playgame();
- }
- // BUTTON-RED
- while (digitalRead(bttn[1]) == HIGH ) {
- climbsong();
- }
- // BUTTON-YELLOW
- while (digitalRead(bttn[2]) == HIGH ) {
- compubabble(5,70);
- delay(600);
- compubabble(9,70);
- DateTime now = rtc.now();
- // UN-COMMENT BELOW IF YOU CARE ABOUT PRINTLN TO SERIAL
- //Serial.print(now.year(), DEC);
- //Serial.print('/');
- //Serial.print(now.month(), DEC);
- //Serial.print('/');
- //Serial.print(now.day(), DEC);
- //Serial.print(" (");
- // Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
- //Serial.print(now.dayOfTheWeek()); // made this instead of above one
- //Serial.print(") ");
- //Serial.print(now.hour(), DEC);
- //Serial.print(':');
- //Serial.print(now.minute(), DEC);
- //Serial.print(':');
- //Serial.print(now.second(), DEC);
- //Serial.println();
- //Serial.println('counter is');
- //Serial.println(counter);
- //Serial.println('cyclenumber is');
- //Serial.println(cyclenumber);
- //Serial.println('thishour is');
- //Serial.println(thishour);
- // end rtc stuff
- // showtext(F(__DATE__));
- // thestring='----now--';
- // showtext(thestring);
- // ***** soon will use this: String.format("%02d", myNumber)
- // ***** backing up old string: thestring = String(now.hour()) + ":" + String(now.minute()) ;
- thestring = String(now.hour()) + ":" ;
- if (strlen(now.minute()) < 2) {
- thestring = thestring + "0";
- }
- thestring = thestring + String(now.minute()) ;
- showtext(thestring);
- }
- // BUTTON-GREEN
- while (digitalRead(bttn[3]) == HIGH ) {
- // sing a damn song and we will see if user is still holding green afterwards
- songrotator = popsong(songrotator);
- // sayhi2();
- while (digitalRead(bttn[3]) == HIGH ) { // GREEN STILL ON we are doing the NEW TIME FIX INTERFACE
- sayhi();
- // 1 STORE THE hour and minute
- z0 = now.hour();
- z1 = now.minute();
- utilinteger = 1; //set to zero when time to exit interface
- while (utilinteger == 1) {
- while (digitalRead(bttn[0]) == HIGH ) { // BLUE means hour backwards
- z0 = z0 - 1;
- if (z0 < 0) {
- z0 = 23;
- }
- pip();
- }
- while (digitalRead(bttn[1]) == HIGH ) { // RED means min backwards
- z1 = z1 - 1;
- if (z1 < 0) {
- z1 = 59;
- }
- pip();
- }
- while (digitalRead(bttn[2]) == HIGH ) { // YELLOW means exit interface
- pip();
- pip();
- utilinteger = 0;
- } // end yellow
- } // leaving = they pressed yellow so end interface
- rtc.adjust(DateTime(now.year(), now.month(), now.day(), z0, z1, 0));
- z0 = 0;
- z1 = 0;
- z2 = 0;
- z3 = 0;
- } // leaving = they did not hold green or timeset is dome
- } // OK finished the initial green
- // MATRIX GRID ball bounce MAXXSHOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ballyvel = ballyvel + 0.5;
- bally = bally + ballyvel;
- ballx = ballx + ballxvel;
- if (bally > ballymax) {
- bally = bally - ballyvel;
- ballyvel = 0-ballyvel;
- }
- if (ballx > ballxmax or ballx < ballxmin) {
- ballx = ballx - ballxvel;
- ballxvel = 0- ballxvel;
- //matrix clear it
- // matrix.clear(); CLEAR ONLY PER PAGE METHOD
- // matrix.writeDisplay();
- //matrix end clear it
- }
- //matrix draw it
- // matrix.drawRect(int(ballx),int(bally),2,2, LED_ON);
- matrix.clear();
- matrix.drawPixel(int(ballx),int(bally), LED_ON);
- matrix.writeDisplay();
- // matrix end
- delay(lengthdelay/8);
- counter++;
- } // END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end the inner infinite repeat loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement