I'm using some code to read the ppm signal from an RC receiver. this works good on its own. I am wanting to use these values to run a pololu motor driver board. I have had the motor driver working with an analog joystick just fine but I can't quite get the 2 pieces of code to work together.
Here is what I have:
#include <DualG2HighPowerMotorShield.h>
DualG2HighPowerMotorShield24v14 md;
#define RECEIVE_PIN 2
#define CHANNEL_AMOUNT 8
#define DETECTION_SPACE 2500
#define METHOD RISING
int ch[CHANNEL_AMOUNT + 1];
int move; // Forward/Back speed
int turn; // Turning Factor
void setup()
{
Serial.begin(115200);
pinMode(RECEIVE_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RECEIVE_PIN), ppm_interrupt, METHOD);
}
void loop()
{
ppm_write();
}
void ppm_write()
{
static unsigned long int t;
if (millis() - t < 100)
return 0;
for (byte i = 0; i < CHANNEL_AMOUNT + 1; i++)
{
Serial.print(ch[i]);
Serial.print("\t");
}
Serial.print("\n");
t = millis();
}
void ppm_interrupt()
{
static byte i;
static unsigned long int t_old;
unsigned long int t = micros(); //store time value a when pin value falling/rising
unsigned long int dt = t - t_old; //calculating time inbetween two peaks
t_old = t;
if ((dt > DETECTION_SPACE) || (i > CHANNEL_AMOUNT))
{
i = 0;
}
ch[i++] = dt;
}
{ //I am not sure whre to place this portion to not affect the isr above
md.enableDrivers();
delay(1); // The drivers require a maximum of 1ms to elapse when brought out of sleep mode.
md.setM1Speed(move);
// stopIfFault();
md.setM2Speed(move);
// stopIfFault();
move = map(ch[3], 1000, 2000, -400, 400); //center over zero
turn = map(ch[1], 1000, 2000, -400, 400);
if(turn>0){md.setM2Speed(move-turn); md.setM1Speed(move);};
if(turn<0){md.setM1Speed(move-abs(turn)); md.setM2Speed(move);};
//Serial.println(move); Where to place this for debugging?
//Serial.println(turn);
//delay(2000);
}
I'm not sure if the syntax is incorrect or if it is because one uses interrupts and the other uses timer functions.
I am getting a compiler issue currently because of syntax.