Developing a connected headset that increases your presence in distance classrooms
To develop a personal headset system used to send notifications to instructors who are teaching from outside the classroom.
/* 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
/**************************************************************************/
/*!
@file Adafruit_MMA8451.h
@author K. Townsend (Adafruit Industries)
@license BSD (see license.txt)
This is an example for the Adafruit MMA8451 Accel breakout board
----> https://www.adafruit.com/products/2019
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>
#include "neopixel.h"
/*# define STATE_UNMUTE 0
# define STATE_MUTE 1*/
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D4
#define PIXEL_COUNT 1
#define PIXEL_TYPE SK6812RGBW
int state = 1;
float xData = 0;
int sampleWindow = 50;//sample every 50ms
int brightness = 0;
float rmult = 0.33;
float gmult = 0.84;
float bmult = 0.75;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
/*Timer timer(500, doUnmuteState);*/
Adafruit_MMA8451 mma = Adafruit_MMA8451();
int timeStart;
bool triggeredMute = true;
bool triggered = false;
float freq = 3000;
float val = 0;
void signOn(){
//Turn on the acceleraometer and check if it's receiving data
Serial.println("Adafruit MMA8451 test!");
if (! mma.begin()) {
Serial.println("Couldnt start");
while (1);
}
Serial.println("MMA8451 found!");
mma.setRange(MMA8451_RANGE_2_G);
Serial.print("Range = "); Serial.print(2 << mma.getRange());
Serial.println("G");
}
int triggerFor = 5000;
int triggerFor2 = 10000;
int triggerFor3 = 3000;
int triggeredAt = -1;
int triggeredAt2 = -1;
int triggeredAt3 = -1;
int ftrigger = -1;
void setup() {
Serial.begin(9600);
signOn();//check that the accelerometer is getting data
//initialion the D7 light
delay(5000);
pinMode( D7 , OUTPUT);
pinMode( D4 , OUTPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
timeStart= millis();
}
void loop() {
run();
Serial.print("triggerMute = ");
Serial.print(triggeredMute);
Serial.print("\t");
Serial.print("trigger = ");
Serial.println(triggered);
Serial.print("triggeredAt = ");
Serial.print(triggeredAt);
Serial.print("\t");
Serial.print("triggeredAt2 = ");
Serial.print(triggeredAt2);
Serial.print("\t");
Serial.print("triggeredAt3 = ");
Serial.println(triggeredAt3);
/*Serial.println(xData);
Serial.println(triggered);*/
/*Serial.print(sampling());
Serial.print("\t");
Serial.print("Timer: ");
Serial.println(timer.isActive());*/
delay(5);
}
int checkX() {
// Read the 'raw' data in 14-bit counts
mma.read();
// Get a new sensor event
sensors_event_t event;
mma.getEvent(&event);
unsigned long startMillis = millis();
float highest_sample = 0;
float lowest_sample = 0;
float peakToPeak = 0;
const int sampleWindow = 50;
while (millis() - startMillis < sampleWindow)
{
xData = event.acceleration.x;
// invert the range, and convert it to a percent
if ( xData > highest_sample )
{
highest_sample = xData ;
}
if ( xData < lowest_sample ){
lowest_sample = xData;
}
}
if (highest_sample<1)
{
peakToPeak = highest_sample + lowest_sample;
}
else
{
peakToPeak = highest_sample - lowest_sample;
}
/*Serial.print("xData: ");
Serial.print(xData);
Serial.print("\t");
Serial.print("P2P: ");
Serial.println(peakToPeak);*/
return peakToPeak;
}
void run(){
checkX();
if(xData > 8.0 && xData < 11){
triggered = true;
if( triggeredAt == -1)
triggeredAt = millis();
}else{
Serial.println("NoHand");
triggered = false;
triggeredAt = -1;
}
if(triggered == false ){
if(triggeredMute == true){
triggeredAt2 = -1;
triggeredAt3 = -1;
rmult = 0.33;
gmult = 0.84;
bmult = 0.75;
constLed();
}else if(triggeredMute == false){
if( triggeredAt2 + triggerFor2 > millis() ){
triggeredAt3 = millis();
rmult = 0.5;
gmult = 0.35;
bmult = 0.65;
constLed();
}else if(triggeredAt3 + triggerFor3 < millis() ){
rmult = 0.33;
gmult = 0.84;
bmult = 0.75;
frequency();
breathe();
triggeredAt2 = -1;
if(freq < 500){
doMuteState();
ftrigger =-1;
}
}
}
}else if(triggered == true ){
if(triggeredMute == true){
rmult = 0.5;
gmult = 0.35;
bmult = 0.65;
frequency();
breathe();
if( triggeredAt + triggerFor < millis() ){
triggeredMute = false;
triggeredAt2 = millis();
doUnmuteState();
}else if(triggeredMute == false){
rmult = 0.33;
gmult = 0.84;
bmult = 0.75;
frequency();
breathe();
}
}
}
}
float frequency(){
if( ftrigger == -1){
ftrigger = millis();
}
freq = (-0.5*(millis() - ftrigger)) + 3000.0;
}
void doMuteState(){
Serial.println("Mute");
Particle.publish("diot2017/mute");
triggeredMute = true;
}
void doUnmuteState(){
Serial.println("Not Mute");
Particle.publish("diot2017/unmute");
/*delay(15000);*/
}
float breathe(){
float i = freq;
val = (exp(sin(millis()/i*M_PI)) - 0.36787944)*108.0;
Serial.print("i= ");
Serial.println(i);
uint32_t c = strip.Color(val*rmult, val*gmult, val*bmult, 0);
strip.setPixelColor(0, c);
strip.show();
}
float constLed(){
float val = 255;
uint32_t c = strip.Color(val*rmult, val*gmult, val*bmult, 0);
strip.setPixelColor(0, c);
strip.show();
ftrigger = -1;
}
Click to Expand
Developing a connected headset that increases your presence in distance classrooms
May 16th, 2017