Hi All,
I've been using arduino for ages (even made the cheatsheet for it!), but this has got me scratching my head a little to figure out the 'right' way to do it, and more importantly to explain to others how to do the same.
I've got a slab of code that compiles successfully in the main sketch, and I'm trying to break it out into different files to make it more elegant.
According to
http://www.arduino.cc/en/Guide/Environment, you can choose no extension (i.e. pde/ino), or .c, .cpp, .h. No problem. I'd prefer to use the native ino format (since then I don't have to do all sorts of preprocessor stuff to check if my code's already defined, and include the arduino libs manually, etc.).
If I use the 'new tab' function and make a file 'radio', for example, then remove the radio code and put it there, it will give compile errors that are consistent with it compiling the second 'radio' tab after the main one. If I move my global variables to the main tab it compiles, and finds the methods in the second tab without issue. But it's inelegant to have all my globals related to the second tab cluttering up my first.
Is there a way to make the second tab compile first? I've tried putting #include "radio", or #include "radio.ino" at the top of my main tab, and they didn't work. I could of course turn 'radio' into separate '.cpp' and '.h files, but that's unwieldy and almost impossible to explain easily to newbs.
Cheers,
Gavin
Code below, for ref, but it's more advice on the philosophy I'm after, rather than this specific example:
/******************************************************/
/********* Hobby RC Radio code ***********/
/******************************************************/
boolean RC_RX_healthy = false;
const int RC_pitch_pin = 12; //Arduino digital pins. Inputs by default
const int RC_yaw_pin = 11;
const int RC_aux_pin = 10;
int RC_pitch = 0;
int RC_yaw = 0;
int RC_aux = 0;
int RC_MAX = 1887;
int RC_MIN = 1084; //set these to your RX's limits. Will depend on hardware.
int read_servo(int servo_pin, int servo_min, int servo_max){
// Read a standard RC hobby servo signal on a digital pin.
// Limits to
int servo_val = pulseIn(servo_pin, HIGH, 20000);
if (servo_val == 0) {
return -1;
}
servo_val = constrain(servo_val,servo_min,servo_max);
servo_val = map(servo_val,servo_min, servo_max,0,255);
return servo_val;
}
void read_RC_RX(){
//Read from the reciever, but if any is invalid or times out, then short circuit and declare invalid.
int yaw = 0;
int pitch = 0;
int aux = 0;
RC_RX_healthy = true;
// Pitch
if (RC_RX_healthy){
pitch = read_servo(RC_pitch_pin, RC_MIN, RC_MAX);
if (pitch >=0){
RC_pitch = pitch;
}
else {
RC_RX_healthy = false;
}
}
// Yaw
if (RC_RX_healthy){
yaw = read_servo(RC_yaw_pin, RC_MIN, RC_MAX);
if (yaw >=0){
RC_yaw = yaw;
}
else {
RC_RX_healthy = false;
}
}
// Aux
if (RC_RX_healthy){
aux = read_servo(RC_aux_pin, RC_MIN, RC_MAX);
if (aux >=0){
RC_aux = aux;
}
else {
RC_RX_healthy = false;
}
}
if ( ! RC_RX_healthy){
//Pitch invalid, RX probably off or no signal, avoid using data.
RC_pitch = 0;
RC_yaw = 0;
RC_aux = 0;
return;
}
}
/******************************************************/
/********* Main code ***********/
/******************************************************/
void setup(){
Serial.begin(115200);
}
void loop(){
//Serial.println("hello world");
int a = analogRead(1);
int b = analogRead(2);
read_RC_RX();
Serial.print(a);
Serial.print("\t ");
Serial.print(b);
Serial.print("\t ");
Serial.print(a);
Serial.print("\t ");
Serial.println(b);
delay(10);
if ( RC_RX_healthy){
//do stuff
}
}