Personos - Armband
/* This project will develop an attachement to a bone conduction headset that will play sounds in connected sets when students ask a question.*/
/*
How to use accelerometer https://learn.adafruit.com/adafruit-mma8451-accelerometer-breakout/wiring-and-test
How to run gesture program https://create.arduino.cc/projecthub/mellis/gesture-recognition-using-accelerometer-and-esp-71faa1
*/
// Initialize Inputs
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00
int mic_pin = A4;
// store the noise level
// from the microphone
int noise_level = 0;
bool canTalk = FALSE;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
void setup (){
Serial.begin(9600);
Serial1.begin(9600);
pinMode(mic_pin, INPUT);
execute_CMD(0x3F, 0, 0); // Send request for initialization parameters
while (Serial1.available()<10) // Wait until initialization parameters are received (10 bytes)
delay(30); // Pretty long delays between succesive commands needed (not always the same)
// Initialize sound to very low volume. Adapt according used speaker and wanted volume
execute_CMD(0x06, 0, 25); // Set the volume (0x00~0x30)
Particle.subscribe("diot2017/unmute", stateUnmuted);//send accelerometer data
Particle.subscribe("diot2017/mute", stateMuted);//send accelerometer data
Particle.subscribe("diot2017/unmute",talkCheck);
Particle.subscribe("diot2017/mute",talkCheck);
}
void loop(){
enterCommand();
// readCommand();
if (canTalk == TRUE){
Serial.println(canTalk);
stillTalking();
}
else{
}
}
void readCommand(){
if (Serial1.available()>=10) // There is at least 1 returned message (10 bytes each)
{
// Read the returned code
byte Returned[10];
for (byte k=0; k<10; k++)
Returned[k] = Serial1.read();
// Wtite the returned code to the screen
Serial.print("Returned: 0x"); if (Returned[3] < 16) Serial.print("0"); Serial.print(Returned[3],HEX);
Serial.print("("); Serial.print(Returned[3], DEC);
Serial.print("); Parameter: 0x"); if (Returned[5] < 16) Serial.print("0"); Serial.print(Returned[5],HEX);
Serial.print("("); Serial1.print(Returned[5], DEC);
Serial.print("), 0x"); if (Returned[6] < 16) Serial.print("0"); Serial.print(Returned[6],HEX);
Serial.print("("); Serial.print(Returned[6], DEC); Serial.println(")");
}
}
void enterCommand(){
if (Serial.available())
{
// Input in the Serial monitor: Command and the two parameters in decimal numbers (NOT HEX)
// E.g. 3,0,1 (or 3 0 1 or 3;0;1) will play first track on the TF-card
byte Command = Serial.parseInt();
byte Parameter1 = Serial.parseInt();
byte Parameter2 = Serial.parseInt();
// Write the input at the screen
Serial.print("Command : 0x");if (Command < 16) Serial.print("0"); Serial.print(Command, HEX);
Serial.print("("); Serial.print(Command, DEC);
Serial.print("); Parameter: 0x");if (Parameter1 < 16) Serial.print("0");Serial.print(Parameter1, HEX);
Serial.print("("); Serial.print(Parameter1, DEC);
Serial.print("), 0x");if (Parameter2 < 16) Serial.print("0");Serial.print(Parameter2, HEX);
Serial.print("("); Serial.print(Parameter2, DEC);Serial.println(")");
// Excecute the entered command and parameters
execute_CMD(Command, Parameter1, Parameter2);
}
}
void execute_CMD(byte CMD, byte Par1, byte Par2){ // Excecute the command and parameters
// Calculate the checksum (2 bytes)
int16_t checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, checksum >> 8, checksum & 0xFF, End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
Serial1.write( Command_line[k]);
}
}
void stateUnmuted(const char *event,const char *data){
//Handler for receiving the subscribed event
String newPatternEvent = String(event);
String newPatternData = String(data);
Serial.println("received");
//Play unmute sound
execute_CMD(0x03,0,2);
}
void stateMuted(const char *event,const char *data){
//Handler for receiving the subscribed event
String newPatternEvent = String(event);
String newPatternData = String(data);
Serial.println("received again");
//Play unmute sound
execute_CMD(0x03,0,3);
}
int sampleNoise( ){
unsigned long startMillis = millis(); // Start of sample window
int highest_sample = 0;
int lowest_sample = 1000;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
int sample = analogRead( mic_pin );
// invert the range, and convert it to a percent
sample = map( sample, 0, 4095, 1000, 0 );
// now see if the sample is the lowest;
if ( sample > highest_sample ){
highest_sample = sample ;
}
if ( sample < lowest_sample ){
lowest_sample = sample;
}
}
int peakToPeak = highest_sample - lowest_sample;
return peakToPeak;
}
bool talkCheck(const char *event,const char *data){
//Handler for receiving the subscribed event
String newPatternEvent = String(event);
String newPatternData = String(data);
/*Serial.println("received");//DEBUG*/
canTalk = !canTalk;
return canTalk;
}
//When there is sound coming through the microphone it will publish mut
void stillTalking(){
noise_level = sampleNoise();
Serial.println(noise_level);
if (noise_level > 600){
Particle.publish("diot2017/stillTalking");
}
delay(50);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .