I want to use a Arduino nano 33 ble as a custom Headtracker to track the tilt and pan from my Head where it can control two servors for a Puppethead with tilt and pan movement.I tried finding Tutorials about this topic but there are all about connecting a Headtracker with a Radio, what i dont need.
Any help is much appreciated even if its just a link to a Tutorial and i hope you could unterstand my Problem with my level of Englisch.
#include <Arduino_LSM9DS1.h>
float x, y, z;
int X = 0;
int Y = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Started");
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
}
void loop() {
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
}
if (x > 0.1) {
x = 100 * x;
X = map(x, 0, 97, 0, 90);
Serial.print("Tilting up ");
Serial.print(X);
Serial.println(" degrees");
}
if (x < -0.1) {
x = 100 * x;
X = map(x, 0, -100, 0, 90);
Serial.print("Tilting down ");
Serial.print(X);
Serial.println(" degrees");
}
if (y > 0.1) {
y = 100 * y;
Y = map(y, 0, 97, 0, 90);
Serial.print("Tilting left ");
Serial.print(Y);
Serial.println(" degrees");
}
if (y < -0.1) {
y = 100 * y;
Y = map(y, 0, -100, 0, 90);
Serial.print("Tilting right ");
Serial.print(Y);
Serial.println(" degrees");
}
delay(1000);
}
could you then use the variables for the Servos like that? Also i dont really understand how the map works.Is the map just the range where it can read?
The map() function takes a value in a range and converts it to the equivalent number in another range
So, if the x value was in the range 0 to 1000 and it was currently 500 you would want to move the servo to an angle of 90 to drive the servo to its centre position so you would map() the x value like this
int servoAngle = map(x, 0, 1000, 0, 180);
In other words, given an x value between 0 and 1000 return a proportional value between 0 and 180 That is all there is to it. I only used 0 to 100 as an example. What range of values for x do you get in practice ?
The Code for the Orientation is working i get yaw,pitch,roll angel so the board is okay i assume.I also checked the servo with a arduino uno and it is working.
I run it and the servo doesnt move.
The servo is connected to pin D8.
The servo is powered 5v extern.
Yes i have the liabry for the nano 33 ble and using it.