I have been trying to program an Arduino Uno to take PWM signals from a spectrum RC receiver and mix those signals to control 4 motors on a mecanum drive bot. Although I don't have the bot built/designed yet I have been trying to get some of the code working. My issue is that when ever I try to use more than one pulseIn command it adds about 1 second of lag per pulseIn command. I am controlling a servo through this setup.
The code:
Code:
#include <Servo.h>
Servo FLS; //front left servo
Servo FRS; //front right servo...
Servo RLS;
Servo RRS;
int Forward = 1; //Inputs from RC receiver
int Strafe = 2;
int Rotate = 3;
unsigned long ForwardDuration; //To store the duration of the pulses
unsigned long StrafeDuration; //coming from the receiver
unsigned long RotateDuration;
unsigned long FL; //Front Left Value
unsigned long FR; //Front Right Value...
unsigned long RL;
unsigned long RR;
void setup()
{
FLS.attach(10);
FRS.attach(11);
RLS.attach(12);
RRS.attach(13);
pinMode(Forward, INPUT);
pinMode(Strafe, INPUT);
pinMode(Rotate, INPUT);
}
void loop()
{
ForwardDuration = pulseIn(Forward, HIGH); //read receiver inputs
StrafeDuration = pulseIn(Strafe, HIGH);
RotateDuration = pulseIn(Rotate, HIGH);
FL = ForwardDuration + StrafeDuration + RotateDuration;
FR = ForwardDuration - StrafeDuration - RotateDuration;
RL = ForwardDuration - StrafeDuration + RotateDuration;
RR = ForwardDuration + StrafeDuration - RotateDuration;
FLS.writeMicroseconds(FL);
FRS.writeMicroseconds(FR);
RLS.writeMicroseconds(RL);
RRS.writeMicroseconds(RR);
}
My initial thought was to add interrupts but I'm not exactly sure how to do that.
My attempt at using Interrupts:
Code:
#include <Servo.h>
Servo FLS; //front left servo
Servo FRS; //front right servo...
Servo RLS;
Servo RRS;
int Forward = 1; //Inputs from RC receiver
int Strafe = 2;
int Rotate = 3;
volatile unsigned long ForwardDuration; //To store the duration of the pulses
volatile unsigned long StrafeDuration; //coming from the receiver
volatile unsigned long RotateDuration;
volatile unsigned long FL; //Front Left Value
volatile unsigned long FR; //Front Right Value...
volatile unsigned long RL;
volatile unsigned long RR;
void setup()
{
attachInterrupt(0, reciever, CHANGE);
FLS.attach(10);
FRS.attach(11);
RLS.attach(12);
RRS.attach(13);
pinMode(Forward, INPUT);
pinMode(Strafe, INPUT);
pinMode(Rotate, INPUT);
}
void loop()
{
FLS.write(FL);
FRS.writeMicroseconds(FR);
RLS.writeMicroseconds(RL);
RRS.writeMicroseconds(RR);
}
void reciever()
{
ForwardDuration = pulseIn(Forward, HIGH); //read receiver inputs
StrafeDuration = pulseIn(Strafe, HIGH);
RotateDuration = pulseIn(Rotate, HIGH);
FL == ForwardDuration + StrafeDuration + RotateDuration;
FR == ForwardDuration - StrafeDuration - RotateDuration;
RL == ForwardDuration - StrafeDuration + RotateDuration;
RR == ForwardDuration + StrafeDuration - RotateDuration;
}
The reason I am posting here and not on the Arduino forums is that it wont load on my computer right now so I thought I'd try my shot with CD and see if anyone here could help.