Skip to content

Instantly share code, notes, and snippets.

@esthicodes
Created August 3, 2021 08:04
Show Gist options
  • Save esthicodes/e9184398bc610e0cf44074754b697a2a to your computer and use it in GitHub Desktop.
Save esthicodes/e9184398bc610e0cf44074754b697a2a to your computer and use it in GitHub Desktop.
Arduino that would generate sound when the two pointing data is close to each other.
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
SoftwareSerial Vicon_BTSerial(2,3);
SoftwareSerial Master_BTSerial(4,9);
SoftwareSerial MP3Module(7, 6);
DFRobotDFPlayerMini MP3Player;
int speakerButton = 12;
int buzzer = 8;
int vicon_data = 0;
int subject_data = 0;
int valid_subject = 0;
int subject_count = 0;
int temp_vicon = 0;
bool SUCCESS = false;
bool guide_flag = false;
void target_sound();
void terminate_sound();
void success_sound();
void fail_sound();
void guide_correct_sound();
void setup()
{
Serial.begin(9600);
Master_BTSerial.begin(9600);
Vicon_BTSerial.begin(9600);
MP3Module.begin(9600);
pinMode(speakerButton, INPUT_PULLUP);
if (!MP3Player.begin(MP3Module))
{ // MP3 모듈을 초기화
Serial.println(F("Unable to begin:"));
while (true);
}
delay(1);
MP3Player.volume(30); // 볼륨을 조절합니다. 0~30까지 설정
Vicon_BTSerial.read();
Master_BTSerial.read();
Vicon_BTSerial.println("end");
}
void loop()
{
// vicon_data = 0;
// subject_data = 0;
//
// if(digitalRead(speakerButton)==LOW)
// {
// target_sound();
// valid_subject = 0;
// Vicon_BTSerial.println("request");
// }
// Master_BTSerial.listen();
while(Master_BTSerial.available()!=0)
{
subject_data = Master_BTSerial.read();
Serial.println(subject_data);
if(subject_data!=0)
{
valid_subject = subject_data; // subject_clicked!!
Serial.println(valid_subject);
Master_BTSerial.end();
break;
}
}
// Vicon_BTSerial.listen();
// while(Vicon_BTSerial.available() && Vicon_BTSerial.readStringUntil('.').toInt() > 0)
// {
// if(valid_subject!=0)
// {
//
// vicon_data = Vicon_BTSerial.readStringUntil('.').toInt();
//
// if(0 < vicon_data && vicon_data < 150)
// {
// if(!guide_flag)
// {
// success_sound();
// SUCCESS = true;
//// Serial.print("vicon:: ");
//// Serial.println(vicon_data);
// }
// }
// else
// {
// fail_sound();
// SUCCESS = false;
// subject_count++;
//
// while(vicon_data > 150)
// {
// temp_vicon = Vicon_BTSerial.readStringUntil('.').toInt();
//
// if(temp_vicon > 0)
// {
// vicon_data = temp_vicon;
// tone(buzzer, vicon_data*1.5, 100);
// }
//// Serial.print("vicon:: ");
//// Serial.println(vicon_data);
// }
// guide_correct_sound();
// Vicon_BTSerial.end();
// }
//
// valid_subject = 0;
//
// Vicon_BTSerial.println("end");
// vicon_data = 0;
// break;
// }
// }
//
// if (subject_count==3 || SUCCESS)
// {
// subject_count = 0;
// SUCCESS = false;
// terminate_sound();
// }
}
void target_sound()
{
for(int i=0; i<5; ++i)
{
tone(buzzer, 500, 500);
delay(1000);
}
}
void terminate_sound()
{
tone(buzzer, 2093, 100); delay(100);
tone(buzzer, 2349, 100); delay(100);
tone(buzzer, 2637, 100); delay(100);
}
void guide_correct_sound()
{
MP3Player.play(2);
delay(1200);
}
void success_sound()
{
MP3Player.play(5);
delay(2000);
}
void fail_sound()
{
MP3Player.play(4);
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment