#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
#define LCD_ADDRESS 0x27
#define LCD_COLS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);
long distance;
int echoNote = 220;
int buttonOne = 1;
int buttonTwo = 2;
int buttonThree = 3;
int buttonFour = 4;
int buttonFive = 5;
int buttonSix = 6;
int buttonSeven = 7;
int buttonEight = 8;
int buttonNine = 9;
int buttonTen = 10;
int tomOne = 196;
int tomOnePlay = 0;
int tomTwo = 784;
int tomTwoPlay = 0;
int soundOut = 13;
void updateLCD() {
lcd.clear(); // Clear the LCD screen
// Display distance
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
// Display note played
lcd.setCursor(0, 1);
lcd.print("Note: ");
lcd.print(echoNote);
}
void updateLCDnote(int note) {
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("Note: ");
lcd.print(note);
}
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
int melody[] = {262, 294, 330, 349, 392, 440, 494, 523, 587, 659, 698, 784};
void setup() {
Wire.begin();
mpu.initialize();
// Configure the MPU-6050 sensor for accelerometer range +/- 2g
mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
// put your setup code here, to run once:
// lcd.begin(LCD_COLS, LCD_ROWS);
lcd.init();
lcd.backlight();
pinMode(buttonOne, INPUT_PULLUP);
pinMode(buttonTwo, INPUT_PULLUP);
pinMode(buttonThree, INPUT_PULLUP);
pinMode(buttonFour, INPUT_PULLUP);
pinMode(buttonFive, INPUT_PULLUP);
pinMode(buttonSix, INPUT_PULLUP);
pinMode(buttonSeven, INPUT_PULLUP);
pinMode(buttonEight, INPUT_PULLUP);
pinMode(buttonNine, INPUT_PULLUP);
pinMode(buttonTen, INPUT_PULLUP);
pinMode(soundOut, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(buttonOne) == LOW)
{
tone(soundOut, 110, 10);
}
if (digitalRead(buttonTwo) == LOW)
{
tomOnePlay = 1;
}
if (tomOnePlay == 1)
{
tone(soundOut, tomOne, 15);
delay(10);
tomOne -= 6;
if (tomOne < 41)
{
tomOne = 196;
tomOnePlay = 0;
}
}
if (digitalRead(buttonThree) == LOW)
{
tomTwoPlay = 1;
}
if (tomTwoPlay == 1)
{
tone(soundOut, tomTwo, 14);
delay(12);
tomTwo -= 28;
if (tomTwo < 262)
{
tomTwoPlay = 0;
tomTwo = 784;
}
}
if (digitalRead(buttonFour) == LOW)
{
tone(soundOut, 147, 30);
delay(40);
tone(soundOut, 220, 30);
delay(40);
tone(soundOut, 294, 30);
delay(40);
tone(soundOut, 349, 30);
delay(40);
tone(soundOut, 440, 30);
delay(40);
}
if (digitalRead(buttonFive) == LOW)
{
tone(soundOut, 175, 30);
delay(40);
tone(soundOut, 262, 30);
delay(40);
tone(soundOut, 340, 30);
delay(40);
tone(soundOut, 440, 30);
delay(40);
tone(soundOut, 523, 30);
delay(40);
}
if (digitalRead(buttonSix) == LOW)
{
tone(soundOut, 196, 30);
delay(40);
tone(soundOut, 294, 30);
delay(40);
tone(soundOut, 392, 30);
delay(40);
tone(soundOut, 494, 30);
delay(40);
tone(soundOut, 587, 30);
delay(40);
}
if (digitalRead(buttonSeven) == LOW)
{
tone(soundOut, 220, 30);
delay(40);
tone(soundOut, 330, 30);
delay(40);
tone(soundOut, 440, 30);
delay(40);
tone(soundOut, 523, 30);
delay(40);
tone(soundOut, 659, 30);
delay(40);
}
if (digitalRead(buttonEight) == LOW)
{
tone(soundOut, 262, 30);
delay(40);
tone(soundOut, 392, 30);
delay(40);
tone(soundOut, 523, 30);
delay(40);
tone(soundOut, 659, 30);
delay(40);
tone(soundOut, 784, 30);
delay(40);
}
if (digitalRead(buttonNine) == LOW)
{
distance = 0.01723 * readUltrasonicDistance(11, 12);
updateLCD();
if (distance < 7)
{
if (distance > 1)
{
echoNote = 220;
}
}
if (distance < 9)
{
if (distance > 6)
{
echoNote = 262;
}
}
if (distance < 11)
{
if (distance > 8)
{
echoNote = 294;
}
}
if (distance < 13)
{
if (distance > 10)
{
echoNote = 330;
}
}
if (distance < 15)
{
if (distance > 12)
{
echoNote = 392;
}
}
if (distance < 17)
{
if (distance > 14)
{
echoNote = 440;
}
}
if (distance < 19)
{
if (distance > 16)
{
echoNote = 523;
}
}
if (distance < 21)
{
if (distance > 18)
{
echoNote = 587;
}
}
if (distance < 23)
{
if (distance > 20)
{
echoNote = 659;
}
}
if (distance < 25)
{
if (distance > 22)
{
echoNote = 784;
}
}
if (distance < 40)
{
if (distance > 24)
{
echoNote = 880;
}
}
if (distance < 40)
{
tone(soundOut, echoNote, 50);
}
if (distance > 39)
{
tone(soundOut, 220, 50);
}
}
// Read accelerometer data
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Read gyroscope data
int16_t gx, gy, gz;
mpu.getRotation(&gx, &gy, &gz);
// Detect gestures based on accelerometer and gyroscope data
// Example: If ax > threshold, interpret as tilting gesture
if (digitalRead(buttonTen) == LOW){
if (abs(gx) > 10000 || abs(gy) > 10000 || abs(gz) > 10000) {
// Play a sequence of random tones
for (int i = 0; i < 16; i++) {
int note = random(12); // Generate a random index
updateLCDnote(melody[note]);
tone(soundOut, melody[note]);
delay(100);
noTone(soundOut);
delay(50);
}
} else {
// No sound
noTone(soundOut);
}
delay(100); // Adjust delay as needed
}
}