MAKEY.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Description: Use MAKEY Unit to control M5Core to emit tones.
  3. */
  4. #include <M5Stack.h>
  5. #include <Wire.h>
  6. #define NOTE_D1 294
  7. #define NOTE_DL1 261
  8. #define NOTE_DL2 293
  9. #define NOTE_DL3 329
  10. #define NOTE_DL4 349
  11. #define NOTE_DL5 392
  12. #define NOTE_DL6 440
  13. #define NOTE_DL7 494
  14. void setup() {
  15. // Power ON Stabilizing...
  16. delay(500);
  17. M5.begin();
  18. M5.Power.begin();
  19. pinMode(39, INPUT);
  20. pinMode(38, INPUT);
  21. // Init I2C
  22. Wire.begin();
  23. M5.Lcd.setTextSize(2);
  24. M5.Lcd.setTextColor(WHITE, BLACK);
  25. M5.Lcd.println("M5Stack Makey Piano.");
  26. }
  27. int Key1 = 0, Key2 = 0, Index = 0;
  28. void showKey() {
  29. M5.Lcd.setCursor(20, 200);
  30. M5.Lcd.setTextColor(BLUE, BLACK);
  31. M5.Lcd.print(Index);
  32. M5.Lcd.print(" ");
  33. M5.Lcd.setCursor(5 + 0 * 16, 110);
  34. if ((Key1 & (0x01 << 0)) == 0x00)
  35. M5.Lcd.setTextColor(WHITE, BLACK);
  36. else {
  37. M5.Lcd.setTextColor(RED, BLACK);
  38. M5.Speaker.tone(NOTE_DL1, 20);
  39. }
  40. M5.Lcd.print("1");
  41. M5.Lcd.setCursor(5 + 1 * 16, 110);
  42. if ((Key1 & (0x01 << 1)) == 0x00)
  43. M5.Lcd.setTextColor(WHITE, BLACK);
  44. else {
  45. M5.Lcd.setTextColor(RED, BLACK);
  46. M5.Speaker.tone(NOTE_DL2, 20);
  47. }
  48. M5.Lcd.print("2");
  49. M5.Lcd.setCursor(5 + 2 * 16, 110);
  50. if ((Key1 & (0x01 << 2)) == 0x00)
  51. M5.Lcd.setTextColor(WHITE, BLACK);
  52. else {
  53. M5.Lcd.setTextColor(RED, BLACK);
  54. M5.Speaker.tone(NOTE_DL3, 20);
  55. }
  56. M5.Lcd.print("3");
  57. M5.Lcd.setCursor(5 + 3 * 16, 110);
  58. if ((Key1 & (0x01 << 3)) == 0x00)
  59. M5.Lcd.setTextColor(WHITE, BLACK);
  60. else {
  61. M5.Lcd.setTextColor(RED, BLACK);
  62. M5.Speaker.tone(NOTE_DL4, 20);
  63. }
  64. M5.Lcd.print("4");
  65. M5.Lcd.setCursor(5 + 4 * 16, 110);
  66. if ((Key1 & (0x01 << 4)) == 0x00)
  67. M5.Lcd.setTextColor(WHITE, BLACK);
  68. else {
  69. M5.Lcd.setTextColor(RED, BLACK);
  70. M5.Speaker.tone(NOTE_DL5, 20);
  71. }
  72. M5.Lcd.print("5");
  73. M5.Lcd.setCursor(5 + 5 * 16, 110);
  74. if ((Key1 & (0x01 << 5)) == 0x00)
  75. M5.Lcd.setTextColor(WHITE, BLACK);
  76. else {
  77. M5.Lcd.setTextColor(RED, BLACK);
  78. M5.Speaker.tone(NOTE_DL6, 20);
  79. }
  80. M5.Lcd.print("6");
  81. M5.Lcd.setCursor(5 + 6 * 16, 110);
  82. if ((Key1 & (0x01 << 6)) == 0x00)
  83. M5.Lcd.setTextColor(WHITE, BLACK);
  84. else {
  85. M5.Lcd.setTextColor(RED, BLACK);
  86. M5.Speaker.tone(NOTE_DL7, 20);
  87. }
  88. M5.Lcd.print("7");
  89. M5.Lcd.setCursor(10 + 7 * 16, 110);
  90. if ((Key1 & (0x01 << 7)) == 0x00)
  91. M5.Lcd.setTextColor(WHITE, BLACK);
  92. else {
  93. M5.Lcd.setTextColor(RED, BLACK);
  94. M5.Speaker.tone(NOTE_D1, 20);
  95. }
  96. }
  97. int CommandStatus = 0;
  98. void loop() {
  99. Wire.requestFrom(0x51, 2);
  100. while (Wire.available()) {
  101. Key1 = Wire.read();
  102. Key2 = Wire.read();
  103. Index++;
  104. }
  105. showKey();
  106. // delay(10);
  107. M5.update();
  108. }