still no luck after looking at this thread (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247084671/all). any suggestions?
#include <Servo.h>
boolean inputReadyCh1;
boolean inputReadyCh3;
const int MIN_CH1 = 970;
const int MAX_CH1 = 1800;
const int MIN_CH3 = 1080; //minimum PPM value read for throttle stick
const int MAX_CH3 = 1800; //maximum PPM value read for throttle stick
const int PIN_CH1 = 2; //input pin for steering left/right
const int PIN_CH2 = 4; //input pin for steering up/down
const int PIN_CH3 = 3; //input pin for throttle
const int PIN_LEFTMOTOR = 5; //output pin for left motor
const int PIN_RIGHTMOTOR = 6; //output pin for right motor
const int PIN_SERVO = 9; //output pin for servo steering up/down
int valueCh1;
int valueCh3;
Servo servoCh2;
unsigned long pulseLengthCh1;
unsigned long pulseLengthCh2;
unsigned long pulseLengthCh3;
unsigned long startTimeCh1;
unsigned long startTimeCh3;
void setup() {
servoCh2.attach(PIN_SERVO);
pinMode(PIN_CH1, INPUT);
pinMode(PIN_CH3, INPUT);
pinMode(PIN_LEFTMOTOR, OUTPUT);
pinMode(PIN_RIGHTMOTOR, OUTPUT);
attachInterrupt(0, ISR_Ch1, CHANGE);
attachInterrupt(1, ISR_Ch3, CHANGE);
//Serial.begin(9600);
}
void ISR_Ch1() {
if(digitalRead(PIN_CH1) == HIGH) {
startTimeCh1 = micros();
} else {
pulseLengthCh1 = micros() - startTimeCh1;
inputReadyCh1 = true;
}
}
void ISR_Ch3() {
if(digitalRead(PIN_CH3) == HIGH) {
startTimeCh3 = micros();
} else {
pulseLengthCh3 = micros() - startTimeCh3;
inputReadyCh3 = true;
}
}
void loop() {
if(inputReadyCh1) {
inputReadyCh1 = false;
valueCh1 = constrain(pulseLengthCh1, MIN_CH1, MAX_CH1);
int centralValue = (MIN_CH1 + MAX_CH1) / 2;
if(valueCh1 < centralValue + 100) { //100 is just a spacer value so the stick has
digitalWrite(PIN_LEFTMOTOR, LOW); //a bit of play when it is neutral
} else if(valueCh1 > centralValue + 100) {
digitalWrite(PIN_RIGHTMOTOR, LOW);
}
}
if(inputReadyCh3) {
inputReadyCh3 = false;
if(pulseLengthCh3 < MIN_CH3) {
pulseLengthCh3 = MIN_CH3;
} else if(pulseLengthCh3 > MAX_CH3) {
pulseLengthCh3 = MAX_CH3;
}
valueCh3 = map(pulseLengthCh3, MIN_CH3, MAX_CH3, 0, 255);
analogWrite(PIN_LEFTMOTOR, valueCh3);
analogWrite(PIN_RIGHTMOTOR, valueCh3);
}
pulseLengthCh2 = pulseIn(PIN_CH2, HIGH);
servoCh2.writeMicroseconds(pulseLengthCh2);
//Serial.println("Ch3 value: " + String(valueCh3));
//Serial.println("Time for pulseIn 2: " + String(time2));
//Serial.println("Time for pulseIn 3: " + String(time3));
//Serial.println("Ch1: " + String(pulseLengthCh1)); //for debugging
//Serial.println("Ch2: " + String(pulseLengthCh2));
//Serial.println("Ch3: " + String(pulseLengthCh3) + "\n");
}