Skip to content

Instantly share code, notes, and snippets.

@arduinoboard
Last active June 26, 2018 19:37
Show Gist options
  • Save arduinoboard/4f808579da9d5dfb6d5c55eadd6285c2 to your computer and use it in GitHub Desktop.
Save arduinoboard/4f808579da9d5dfb6d5c55eadd6285c2 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino/Genuino Uno with a serial number of 55736303831351F022A0
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM9DS0.h>
#include <Adafruit_Simple_AHRS.h>
#include <SoftwareSerial.h>
#define PAUSEBTN 5
#define CALIBRATEBTN 6
#define PLAYBTN 7
#define MOUTH 8
//channel 0
#define FOOTBIT 0x01
#define BODYBIT 0x02
#define MICBIT 0x04
#define MOUTHBIT 0x08
#define HEADRIGHTBIT 0x10
//channel 1
#define HEADLEFTBIT 0x80
// Create LSM9DS0 board instance.
Adafruit_LSM9DS0 lsm(1000); // Use I2C, ID #1000
// Create simple AHRS algorithm using the LSM9DS0 instance's accelerometer and magnetometer.
Adafruit_Simple_AHRS ahrs(&lsm.getAccel(), &lsm.getMag());
// Function to configure the sensors on the LSM9DS0 board.
// You don't need to change anything here, but have the option to select different
// range and gain values.
long lookTimer=0;
long playTimer=0;
bool isLeaningLeft=false;
bool mouthOpen=false;
bool enable=false;
byte msg[] = {0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
byte pauseMsg[] = {0x3c, 0x30, 0x30};
byte playMsg[] = {0x75};
byte selectShow[] = {0x2a, 0x30, 0x30};
SoftwareSerial mySerial(2, 3); // RX, TX
void configureLSM9DS0(void)
{
// 1.) Set the accelerometer range
lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);
// 2.) Set the magnetometer sensitivity
lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);
// 3.) Setup the gyroscope
lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS);
}
void setup(void)
{
Serial.begin(9600);
// Initialise the LSM9DS0 board.
if(!lsm.begin())
{
// There was a problem detecting the LSM9DS0 ... check your connections
Serial.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!"));
while(1);
}
// Setup the sensor gain and integration time.
configureLSM9DS0();
pinMode(PLAYBTN,INPUT_PULLUP);
pinMode(PAUSEBTN,INPUT_PULLUP);
pinMode(CALIBRATEBTN,INPUT_PULLUP);
pinMode(MOUTH,INPUT_PULLUP);
pinMode(13,OUTPUT);
mySerial.begin(9600);
}
void calcParity(){
msg[17]=msg[1]+msg[2]+msg[3]+msg[4]+msg[5]+msg[6]+msg[7]+msg[8]+msg[9]+msg[10]+msg[11]+msg[12]+msg[13]+msg[14]+msg[15]+msg[16];
}
void loop(void)
{
sensors_vec_t orientation;
int songCount=0;
bool buttonDown=true;
lsm.readGyro();
if(lsm.gyroData.y>20000 && ((millis()-lookTimer)>500) && enable){
Serial.println("look left");
bitClear(msg[1],4);//channel 0, data bit 4
bitSet(msg[2],7);//channel 1, data bit 7
calcParity();
mySerial.write(msg, 18);
lookTimer=millis();
}else if(lsm.gyroData.y<-20000 && ((millis()-lookTimer)>500) && enable){
Serial.println("look right");
bitSet(msg[1],4);//channel 0, data bit 4
bitClear(msg[2],7);//channel 1, data bit 7
calcParity();
mySerial.write(msg, 18);
lookTimer=millis();
}
if(digitalRead(PLAYBTN)==LOW && ((millis()-playTimer)>1000)){
Serial.write("play pressed");
enable=false;
playTimer=millis();
while((millis()-playTimer)<10000){
if(digitalRead(PLAYBTN)==HIGH && buttonDown){
buttonDown=false;
delay(20);
}else if(digitalRead(PLAYBTN)==LOW && !buttonDown){
buttonDown=true;
songCount++;
Serial.println(songCount);
delay(20);
}
}
if(songCount>9){
selectShow[1]=String(songCount).charAt(0);
selectShow[2]=String(songCount).charAt(1);
}else{
selectShow[1]='0';
selectShow[2]=String(songCount).charAt(0);
}
Serial.write(selectShow, 3);
mySerial.write(selectShow, 3);
mySerial.write(playMsg, 1);
}
if(digitalRead(PAUSEBTN)==LOW && ((millis()-playTimer)>1000) && !enable){
mySerial.write(pauseMsg, 3);
playTimer=millis();
enable=true;
}
if(!mouthOpen && digitalRead(MOUTH)==HIGH && enable){
Serial.println("mouth open");
bitSet(msg[1],3);//channel 0, data bit 3
calcParity();
mySerial.write(msg, 18);
mouthOpen=true;
}else if(mouthOpen && digitalRead(MOUTH)==LOW && enable){
Serial.println("mouth closed");
bitClear(msg[1],3);//channel 0, data bit 3
calcParity();
mySerial.write(msg, 18);
mouthOpen=false;
}
// Use the simple AHRS function to get the current orientation.
if (ahrs.getOrientation(&orientation))
{
if(orientation.pitch<-30 && !isLeaningLeft && enable){
Serial.println("lean left");
bitSet(msg[1],1);//channel 0, data bit 1
calcParity();
mySerial.write(msg, 18);
isLeaningLeft=true;
}else if(orientation.pitch>30 && isLeaningLeft && enable){
Serial.println("lean right");
bitClear(msg[1],1);//channel 0, data bit 3
calcParity();
mySerial.write(msg, 18);
isLeaningLeft=false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment