Guldastronaut
New member
I need this "hen" to be able to play sounds
Im trying to convert pitch and roll in to angle and distance from center and to light up some neopixels and play sounds from a sd card when the user hits 8 fictive "bells" in a circle.
first challenge is to get the angle from the pitch and roll
next up is to get the circular position from these
then i need to calculate the distance from that posistion and to center
if that distance is bigger than a certain radius i want my teensy to play a sound
Code:
#include <NXPMotionSense.h>
#include <MahonyAHRS.h>
#include <Wire.h>
#include <EEPROM.h>
NXPMotionSense imu;
Mahony filter;
int joyheading;
int joypitch;
int joyroll;
const float RAD2DEG = 180.0f / PI;
void setup() {
Serial.begin(9600);
imu.begin();
filter.begin(100); // 100 measurements per second
}
void loop() {
float ax, ay, az;
float gx, gy, gz;
float mx, my, mz;
float roll, pitch, heading;
if (imu.available()) {
// Read the motion sensors
imu.readMotionSensor(ax, ay, az, gx, gy, gz, mx, my, mz);
// Update the Mahony filter
filter.update(gx, gy, gz, ax, ay, az, mx, my, mz);
// print pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
if(pitch==0)
{
joypitch = 512;
}
else
{
joypitch = (pitch-(-180))/(180-(-180))*(1024-0)+0;
}
if(roll==0)
{
joyroll = 512;
}
else
{
joyroll = (roll-(-180))/(180-(-180))*(1024-0)+0;
}
//float x = map(joypitch,0,1024,0,100)/100.0;
//float y = map(joyroll,0,1024,0,100)/100.0;
float x =joypitch; // 0-1024
float y =joyroll; // 0-1024
// here im trying to find the distance betwen the current position of my joystick and center
// float posX = x * sqrt(1 - y * y / 2);
// float posY = y * sqrt(1 - x * x / 2);
float angle = atan2(y, x) * RAD2DEG;
for(int i =0;i<8;i++){
int minimum = 45*i;
int maximum = minimum+45;
if(inRange(angle,minimum,maximum){
// this defines wich tone to play
// playfromsd("tone"+i.ToString()+".waw");
// but i need it ONLY to play if the distance is far enough away from center
}
}
}
}
bool inRange(int val, int minimum, int maximum)
{
return ((minimum <= val) && (val <= maximum));
}