I have a paint ball gun on top of a quad bike and the gun is operated by a joy stick, laser button and a fire button on the steering wheel. A servo moves the gun up and down and a stepper motor rotates the platform with a worm gear. The stepper driver is a DF Robot and I have had to use MS Timer 2 to run the stepper motor as the slow servo and serial coms interfered with the stepper speed.
All works very well.
I now want to control the gun (and spotlight) with a 9 DOF in the crash helmet via Xbee (series 1). As the quad bike/helmet turn around I don't want the gun to turn, so I've put another 9 DOF on the gun turret and used the compass of both 9 DOFs to cancel out each other. If one 9 DOF is higher than the other then turn in one direction or the other till they are within 10 degrees of each other ( I've taken that bit out of the code)
Now the problem:
The Helmet Xbee (S1) transmits the Y axis and X axis data via Easy Transfer to the gun turret Uno
(The Xbees work fine with other projects). When switching to the ET recieve.data part, the script switches between Steering wheel and Helmet data. I have tried different "if" and "else" and "while" statements and different delay times and baud rates on both Uno's but the problem is still there.
I think it has something to do with the MS Timer 2 ??
Here is the helmet code
#include <Wire.h>
#include <EasyTransfer.h> // https://github.com/madsci1016/Arduino-EasyTransfer
#include <Adafruit_Sensor.h> // https://github.com/adafruit/Adafruit_Sensor
#include <Adafruit_LSM303_U.h> // https://github.com/adafruit/Adafruit_LSM303DLHC
#include <Adafruit_L3GD20_U.h> // https://github.com/adafruit/Adafruit_L3GD20_U
#include <Adafruit_9DOF.h> // https://github.com/adafruit/Adafruit_9DOF
Adafruit_9DOF dof = Adafruit_9DOF();
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
EasyTransfer ET;
struct SEND_DATA_STRUCTURE
{
int HelmetYaxis;
int HelmetXaxis;
};
SEND_DATA_STRUCTURE Helmet;
void initSensors()
{
if(!accel.begin())
{
Serial.println(F("Ooops, no Accelerator detected ... Check your wiring!"));
while(1);
}
if(!mag.begin())
{
Serial.println("Ooops, no Compass detected ... Check your wiring!");
while(1);
}
}
void setup()
{
Serial.begin(9600);
ET.begin(details(Helmet), &Serial);
Serial.println(F("Adafruit 9 DOF Pitch/Roll/Heading Example"));
Serial.println("");
initSensors();
}
void loop()
{
sensors_event_t accel_event;
sensors_event_t mag_event;
sensors_vec_t orientation;
Helmet.HelmetYaxis = orientation.roll + 150; // +150 adjusts for 9 DOF to level position
accel.getEvent(&accel_event);
if (dof.accelGetOrientation(&accel_event, &orientation))
{
Serial.print(F(" Y axis: "));
Serial.print(Helmet.HelmetYaxis);
Serial.print(F("; "));
mag.getEvent(&mag_event);
if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
{
Helmet.HelmetXaxis = orientation.heading;
Serial.print(F("X axis: "));
Serial.print(Helmet.HelmetXaxis);
Serial.println(F("; "));
}
}
//delay(500);
ET.sendData();
}
and the Gun turret is:
#include <MsTimer2.h> // https://github.com/PaulStoffregen/MsTimer2
#include <VarSpeedServo.h> // https://github.com/netlabtoolkit/VarSpeedServo
#include <Wire.h>
#include <EasyTransfer.h> // https://github.com/madsci1016/Arduino-EasyTransfer
#include <Adafruit_Sensor.h> // https://github.com/adafruit/Adafruit_Sensor
#include <Adafruit_LSM303_U.h> // https://github.com/adafruit/Adafruit_LSM303DLHC
#include <Adafruit_L3GD20_U.h> // https://github.com/adafruit/Adafruit_L3GD20_U
#include <Adafruit_9DOF.h> // https://github.com/adafruit/Adafruit_9DOF
Adafruit_9DOF dof = Adafruit_9DOF();
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
EasyTransfer ET;
VarSpeedServo YaxisServo;
VarSpeedServo FireServo;
struct RECEIVE_DATA_STRUCTURE
{
int HelmetYaxis;
int HelmetXaxis;
};
RECEIVE_DATA_STRUCTURE Helmet;
int YaxisVal;
int XaxisVal;
int Step_X_State = LOW;
int YaxisJoy = A0;
int XaxisJoy = A1;
int YaxisServoPin = A2;
int FireServoPin = A3;
// A4 and A5 are for the SDA & SCL for the 9 DOF
// (the other twisted pair is 5V and Grn for Joystick)
int Step_X = 6;
int Dir_X = 7;
int LaserOut = 9;
int LaserIn = 10;
int FireIn = 11;
void initSensors()
{
if(!accel.begin())
{
Serial.println(F("Ooops, no Accelerator detected ... Check your wiring!"));
while(1);
}
if(!mag.begin())
{
Serial.println("Ooops, no Compass detected ... Check your wiring!");
while(1);
}
}
void stepperInt()
{
if(Step_X_State)
digitalWrite(Step_X, digitalRead(Step_X)^1);
}
void setup()
{
Serial.begin (9600);
ET.begin(details(Helmet), &Serial);
accel.begin();
mag.begin();
MsTimer2::set(1, stepperInt); // "1" is the stepper motor full speed
MsTimer2::start();
pinMode (Step_X, OUTPUT);
pinMode (Dir_X, OUTPUT);
pinMode (LaserIn, INPUT);
pinMode (LaserOut, OUTPUT);
pinMode (FireIn, INPUT);
FireServo.attach (FireServoPin);
YaxisServo.attach (YaxisServoPin);
initSensors();
}
void loop()
{
sensors_event_t accel_event;
sensors_event_t mag_event;
sensors_vec_t orientation;
// Stuff for the Laser and Trigger
int LaserState = digitalRead (LaserIn);
int FireState = digitalRead (FireIn);
if (LaserState == LOW)
{
digitalWrite (LaserOut, HIGH);
}
else
{
digitalWrite (LaserOut, LOW);
}
if (FireState == LOW && LaserState == LOW)
{
FireServo.write (50); // Trigger on
}
else
{
FireServo.write (85); // Trigger off
}
while (ET.receiveData()>0)
{ //Stuff from helmet
//YaxisVal = (Helmet.HelmetYaxis);
//YaxisVal = constrain (YaxisVal, 1, 180);
//YaxisServo. slowmove (YaxisVal, 20); // 1 to 127 is the speed of the servo
Serial.print (" Helmet X axis ");
Serial.println (Helmet.HelmetXaxis);
// delay (500);
}
if (Serial.read() == '\n' )
//else //(!ET.receiveData ())
// Stuff from steering wheel
{
YaxisVal = analogRead(YaxisJoy);
YaxisVal = map (YaxisVal, 0, 1023, 1, 180);
YaxisVal = constrain (YaxisVal, 1, 180);
YaxisServo. slowmove (YaxisVal, 20); // 1 to 127 is the speed of the servo
XaxisVal = analogRead(XaxisJoy);
if (XaxisVal >= 550)
{
digitalWrite(Dir_X, HIGH); // Set direction
Step_X_State = HIGH;
}
if (XaxisVal <= 450)
{
digitalWrite(Dir_X, LOW); // Other direction
Step_X_State = HIGH;
}
if (XaxisVal >= 451 && XaxisVal <=549)
{
Step_X_State = LOW; //Do nothing
}
}
mag.getEvent(&mag_event);
dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation);
Serial.print (" Gun Y axis: ");
Serial.print (YaxisVal);
Serial.print(" Gun X axis: ");
Serial.print(XaxisVal);
Serial.print (" Compass ");
Serial.println (orientation.heading);
//Serial.flush ();
}
Thanks in advance