Last active
September 20, 2016 06:15
-
-
Save takawo/fa47f4f5d64ad5c9537eb9ae54c73b47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <avr/wdt.h> //library for watchdog | |
| #include <Wire.h> //library for i2c | |
| #include <rgb_lcd.h> //library for lcd display | |
| #include <SoftwareSerial.h> | |
| #include <MP3Player_KT403A.h> | |
| #include <stdio.h> | |
| #include <SoftwareSerial.h> | |
| #include <Ultrasonic.h> | |
| #include <TM1637.h> | |
| #include <CircularLED.h> | |
| /* | |
| D2-USRF | |
| D3,D4-PlayMP3 | |
| D5,D6-CircularLED1 | |
| D7,D8-CircularLED2 | |
| A0-Volume | |
| A1(D15)-Button(DisplayLCD) | |
| A2(D16)-Button(Reset) | |
| A3(D18)-LED(on MP3_play) | |
| I2C-Display Status | |
| */ | |
| #define USRF 2 //the Grove - LED is connected to D2 of Arduino | |
| #define MP3PLAYER_A 3 //the Grove - MP3PLAYER is connected to D3 of Arduino | |
| #define MP3PLAYER_B 4 //the Grove - MP3PLAYER is connected to D4 of Arduino | |
| #define CIRCULAR_LED_1A 6 //the Grove - RING LED is connected to D6 of Arduino | |
| #define CIRCULAR_LED_1B 5 //the Grove - RING LED is connected to D5 of Arduino | |
| #define CIRCULAR_LED_2A 8 //the Grove - RING LED is connected to D8 of Arduino | |
| #define CIRCULAR_LED_2B 7 //the Grove - RING LED is connected to D7 of Arduino | |
| #define VOLUME A0 //the Grove ROTARY_ANGLE_SENSOR is connected to A0 of Arduino | |
| #define DISPLAY_BUTTON 15 | |
| #define DEBUG_BUTTON 16 | |
| #define LED_PIN 17 | |
| CircularLED circularLED1(CIRCULAR_LED_1A, CIRCULAR_LED_1B); | |
| CircularLED circularLED2(CIRCULAR_LED_2A, CIRCULAR_LED_2B); | |
| SoftwareSerial mp3(MP3PLAYER_A, MP3PLAYER_B); | |
| const int mp3Num = 10; | |
| int mp3Index[mp3Num]; | |
| unsigned int LED[24]; | |
| unsigned int LED_Nums[] = {1, 2, 3, 4, 6, 8, 12, 24}; | |
| int LED_Status = 0; | |
| int LED_Num = LED_Nums[LED_Status]; | |
| int LED_rotator = 24 / LED_Num; | |
| int delayTime = 50; | |
| boolean isFlash = false; | |
| boolean clockwise = true; | |
| int frameCount = 0; | |
| int distance; | |
| int threshold; | |
| int count; | |
| int debugButtonState; | |
| int prevDebugButtonState = 0; | |
| boolean debugState = false; | |
| int displayButtonState; | |
| int displayState; | |
| int displayCount; | |
| int mp3State; | |
| Ultrasonic ultrasonic(USRF); | |
| rgb_lcd lcd; | |
| void setup() { | |
| Serial.begin(9600); | |
| pinsInit(); | |
| lcdInit(); | |
| mp3Init(); | |
| MCUSR = 0; // clear out any flags of prior resets. | |
| } | |
| void loop() { | |
| readSensor(); | |
| displayLCD(distance, threshold, count, debugState); | |
| displayCircularLEDs(); | |
| if (debugState != true) { | |
| checkTicket(); | |
| } | |
| } | |
| void pinsInit() { | |
| pinMode(CIRCULAR_LED_1A, OUTPUT); | |
| pinMode(CIRCULAR_LED_1B, OUTPUT); | |
| pinMode(CIRCULAR_LED_2A, OUTPUT); | |
| pinMode(CIRCULAR_LED_2B, OUTPUT); | |
| pinMode(VOLUME, INPUT); | |
| pinMode(DISPLAY_BUTTON, INPUT); | |
| pinMode(DEBUG_BUTTON, INPUT); | |
| pinMode(LED_PIN, OUTPUT); | |
| } | |
| void lcdInit() { | |
| lcd.begin(16, 2); | |
| lcd.setRGB(0, 0, 0); | |
| } | |
| void mp3Init() { | |
| mp3.begin(9600); | |
| SelectPlayerDevice(0x02); // Select SD card as the player device. | |
| SetVolume(0x99); // Set the volume, the range is 0x00 to 0x1E. | |
| for (int i = 0; i < mp3Num; i++) { | |
| mp3Index[i] = 2 * i + 1; | |
| } | |
| } | |
| void(* resetFunc) (void) = 0; //declare reset function @ address 0 | |
| void readSensor() { | |
| distance = ultrasonic.MeasureInCentimeters(); | |
| threshold = map(analogRead(VOLUME), 0, 1023, 20, 0); | |
| debugButtonState = digitalRead(DEBUG_BUTTON); | |
| if (debugButtonState != prevDebugButtonState && debugButtonState == 1) { | |
| debugState = !debugState; | |
| } | |
| prevDebugButtonState = debugButtonState; | |
| displayButtonState = digitalRead(DISPLAY_BUTTON); | |
| if (debugButtonState == 1) { | |
| // softwareReset(WDTO_1S); | |
| } | |
| if (displayButtonState == 1) { | |
| displayState = 1; | |
| //playMp3(); | |
| } | |
| if (displayState == 1) { | |
| displayCount++; | |
| } | |
| if (displayState == 1 && displayCount == 1) { | |
| lcd.setRGB(255 / 3, 255 / 3, 255 / 3); | |
| lcd.noBlinkLED(); | |
| } | |
| if (displayCount > 200) { | |
| displayCount = 0; | |
| displayState = 0; | |
| lcd.setRGB(0, 0, 0); | |
| } | |
| } | |
| // displayLCD(distance, threshold, count, debugState); | |
| void displayLCD(int dist, int threshold, int cnt, int debug) { | |
| lcd.clear(); | |
| // (note: line 1 is the second row, since counting begins with 0): | |
| lcd.setCursor(0, 0); | |
| // print the number of seconds since reset: | |
| lcd.print("DST:" + String(dist) + "/" + String(threshold) + " CNT:" + String(cnt)); | |
| // (note: line 1 is the second row, since counting begins with 0): | |
| lcd.setCursor(0, 1); | |
| // print the number of seconds since reset: | |
| String debugStatus; | |
| if (debug == 0) { | |
| debugStatus = ":OFF"; | |
| } else { | |
| debugStatus = "!!!"; | |
| } | |
| lcd.print("MP3:" + String(mp3Num) + " " + "DEBUG" + debugStatus); | |
| } | |
| void softwareReset( uint8_t prescaller) { | |
| // start watchdog with the provided prescaller | |
| wdt_enable( prescaller); | |
| // wait for the prescaller time to expire | |
| // without sending the reset signal by using | |
| // the wdt_reset() method | |
| while (1) {} | |
| } | |
| void displayCircularLEDs() { | |
| LED_Num = LED_Nums[LED_Status]; | |
| LED_rotator = 24 / LED_Num; | |
| for (int i = 0; i < 24; i++) { | |
| if (i % LED_rotator == frameCount % (24 / LED_Num)) { | |
| if (clockwise) { | |
| LED[i] = 0xff; | |
| } else { | |
| LED[23 - i] = 0xff; | |
| } | |
| } else { | |
| if (clockwise) { | |
| LED[i] = 0; | |
| } else { | |
| LED[23 - i] = 0; | |
| } | |
| } | |
| } | |
| if (isFlash) { | |
| if (frameCount % 2 == 0) { | |
| for (int i = 0; i < 24; i++) { | |
| if (LED[i] == 0xff) { | |
| LED[i] = 0; | |
| } | |
| } | |
| } | |
| } | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| if (isFlash) { | |
| delay(delayTime * 3); | |
| } else { | |
| delay(delayTime); | |
| } | |
| frameCount++; | |
| if (frameCount % 48 == 0) { | |
| changeValue(); | |
| } | |
| if (frameCount % 72 == 0) { | |
| isFlash = !isFlash; | |
| } | |
| if (frameCount % 60 == 0) { | |
| clockwise = !clockwise; | |
| } | |
| } | |
| void changeValue() { | |
| int new_LED_Status; | |
| do { | |
| new_LED_Status = random(0, 6); | |
| } while (LED_Status == new_LED_Status); | |
| LED_Status = new_LED_Status; | |
| LED_Num = LED_Nums[LED_Status]; | |
| LED_rotator = 24 / LED_Num; | |
| } | |
| void playMp3() { | |
| SpecifyMusicPlay(mp3Index[int(random(mp3Num))]); | |
| } | |
| void randomMp3() { | |
| count++; | |
| digitalWrite(LED_PIN, HIGH); | |
| int t = 8; | |
| if (random(1) > 0.8) { | |
| t = 16; | |
| } | |
| for (int i = 0; i < t; i++) { | |
| checkDebug(); | |
| if (debugState == true) { | |
| break; | |
| } | |
| for (int i = 0; i < 24; i++) { | |
| LED[i] = 0xff; | |
| } | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| delay(30); | |
| for (int i = 0; i < 24; i++) { | |
| LED[i] = 0x00; | |
| } | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| playMp3(); | |
| delay(630); | |
| } | |
| digitalWrite(LED_PIN, LOW); | |
| } | |
| void checkTicket() { | |
| if (distance < threshold && QueryPlayStatus() == 1) { | |
| int counter = 1; | |
| while (counter < 24) { | |
| for (int i = 0; i <= counter; i++) { | |
| LED[i] = 0xff; | |
| } | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| counter++; | |
| delay(map(counter, 0, 24, 10, 0)); | |
| } | |
| delay(100); | |
| blinkCircularLED(50, 2); | |
| randomMp3(); | |
| while (counter > 0) { | |
| counter--; | |
| LED[counter] = 0x00; | |
| Serial.println(counter); | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| delay(map(counter, 0, 24, 10, 0)); | |
| } | |
| delay(100); | |
| } | |
| } | |
| void blinkCircularLED(float duration, int times) { | |
| int t = 0; | |
| while (t < times) { | |
| for (int i = 0; i < 24; i++) { | |
| LED[i] = 0xff; | |
| } | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| delay(duration); | |
| for (int i = 0; i < 24; i++) { | |
| LED[i] = 0x00; | |
| } | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| delay(duration); | |
| t++; | |
| } | |
| for (int i = 0; i < 24; i++) { | |
| LED[i] = 0xff; | |
| } | |
| circularLED1.CircularLEDWrite(LED); | |
| circularLED2.CircularLEDWrite(LED); | |
| } | |
| void checkDebug() { | |
| debugButtonState = digitalRead(DEBUG_BUTTON); | |
| if (debugButtonState != prevDebugButtonState && debugButtonState == 1) { | |
| debugState = !debugState; | |
| } | |
| prevDebugButtonState = debugButtonState; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment