Poubelle.ino 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <Grove_LED_Bar.h>
  2. #include "Ultrasonic.h"
  3. #include <SoftwareSerial.h>
  4. #include <MP3Player_KT403A.h>
  5. Ultrasonic ultrasonic_front(2);
  6. Ultrasonic ultrasonic_down(8);
  7. Grove_LED_Bar bar(5, 4, 0); // Clock pin, Data pin, Orientation
  8. SoftwareSerial mp3(6, 7);
  9. int soundWaitTime = 20000;
  10. long int front_lastplayed , down_lastplayed;
  11. void setup()
  12. {
  13. Serial.begin(9600);
  14. // nothing to initialize
  15. bar.begin();
  16. bar.setGreenToRed(1);
  17. mp3.begin(9600);
  18. delay(100);
  19. SelectPlayerDevice(0x02); // Select SD card as the player device.
  20. SetVolume(0x1E); // Set the volume, the range is 0x00 to 0x1E.
  21. front_lastplayed = -1*soundWaitTime;
  22. down_lastplayed = -1*soundWaitTime;
  23. }
  24. void loop()
  25. {
  26. //update ultrasonic measure
  27. long front_distance = ultrasonic_front.MeasureInCentimeters();
  28. long down_distance = ultrasonic_down.MeasureInCentimeters();
  29. int bar_level = map(down_distance, 0, 42, 10, 0);
  30. bar_level = constrain(bar_level, 0, 10);
  31. bar.setLevel(bar_level);
  32. Serial.println(bar_level);
  33. long int currentTime = millis();
  34. if ( (bar_level > 8) && ((currentTime - down_lastplayed)>soundWaitTime) ) {
  35. SpecifyMusicPlay(1);
  36. Serial.println("Playing high level track");
  37. down_lastplayed = currentTime;
  38. }
  39. if ( (bar_level < 2) && ((currentTime - down_lastplayed)>soundWaitTime) ) {
  40. SpecifyMusicPlay(2);
  41. Serial.println("Playing low level track");
  42. down_lastplayed = currentTime;
  43. }
  44. if ( (front_distance < 100) && ((currentTime - front_lastplayed)>soundWaitTime) ) {
  45. SpecifyMusicPlay(3);
  46. Serial.println("Playing proximity track");
  47. front_lastplayed = currentTime;
  48. }
  49. long RangeInCentimeters;
  50. RangeInCentimeters = ultrasonic_front.MeasureInCentimeters(); // two measurements should keep an interval
  51. Serial.print(RangeInCentimeters);//0~400cm
  52. Serial.print(" - ");//0~400cm
  53. RangeInCentimeters = ultrasonic_down.MeasureInCentimeters();
  54. Serial.print(RangeInCentimeters);//0~400cm
  55. Serial.println(" cm");
  56. // if(RangeInCentimeters < 40 ) {
  57. // SpecifyMusicPlay(1);
  58. // delay(5000);
  59. // }
  60. }