Hello all,
I'm currently working on controlling a "multifunction" robot with the use of an RC transmitter, a receiver, and an Arduino. I would like to be able to send commands to the receiver, then have the receiver send a signal to the Arduino. The Arduino should then execute the action corresponding to the received signal. Right now I am beginning to brainstorm a decent way of reading the different states of the RC controller. Depending on the action I want the robot to perform, there could be a large number of active button combinations.
As of now, I have a series of if/elseif statements that check the state of the RC channels to see what commands are being received. I have 14 different state combinations so far that I've thought of. I'm sure more will be discovered later. After checking the button states, I initialize a state integer that I then use in a switch statement. Inside the switch statement is where I will place the functions that will carry out the desired actions.
My question is this: Are there any less cumbersome ways to implement this type of communication? There's always a better way to do things... Whether the proposed way will fit my time constraint (12 days), I do not know. Below I've posted what I currently have. Please note that this is an outline in the strictest sense. My team members have not yet completed the hardware portion of the project, so I have not even defined input/output pins.
#define DMR_A // Right Drive Motor dir. 1
#define DMR_B // Right Drive Motor dir. 2
#define CH1 // Right Drive Motor RC Channel
#define DML_A // Left Drive Motor dir. 1
#define DML_B // Left Drive Motor dir. 2
#define CH2 // Left Drive Motor RC Channel
#define AR_A // Right Actuator dir. 1
#define AR_B // Right Actuator dir. 2
#define CH3 // Right Actuator RC Channel
#define AL_A // Left Actuator dir. 1
#define AL_B // Left Actuator dir. 2
#define CH4 // Left Actuator RC Channel
#define SSMR_A // Right Slingshot Motor dir. 1
#define SSMR_B // Right Slingshot Motor dir. 2
#define CH5 // Right Slingshot Motor RC Channel
#define SSML_A // Left Slingshot Motor dir. 1
#define SSML_B // Left Slingshot Motor dir. 2
#define CH6 // Left Slingshot Motor RC Channel
#define GBALL_A // Golf Ball Piston dir. 1
#define GBALL_B // Golf Ball Piston dir. 2
#define CH7 // Golf Ball Piston RC Channel
#define LATCH_A // Latching Piston dir. 1
#define LATCH_B // Latching Piston dir. 2
#define CH8 // Latching Piston RC Channel
#define COMP // Compressor Pin
#define CH9 // Compressor Power RC Channel
int unsigned state = 0; // Initialize state integer
void setup()
{
pinMode(DMR_A, OUTPUT);
pinMode(DMR_B, OUTPUT);
pinMode(CH1, INPUT); // Right Drive Motor Channel
pinMode(DML_A, OUTPUT);
pinMode(DML_B, OUTPUT);
pinMode(CH2, INPUT); // Left Drive Motor Channel
pinMode(AR_A, OUTPUT);
pinMode(AR_B, OUTPUT);
pinMode(CH3, INPUT); // Right Actuator Motor Channel
pinMode(AL_A, OUTPUT);
pinMode(AL_B, OUTPUT);
pinMode(CH4, INPUT); // Left Actuator Motor Channel
pinMode(SSMR_A, OUTPUT);
pinMode(SSMR_B, OUTPUT);
pinMode(CH5, INPUT); // Right Slingshot Motor Channel
pinMode(SSML_A, OUTPUT);
pinMode(SSML_B, OUTPUT);
pinMode(CH6, INPUT); // Left Slingshot Motor Channel
pinMode(GBALL_A, OUTPUT);
pinMode(GBALL_B, OUTPUT);
pinMode(CH7, INPUT); // Golf Ball Piston Channel
pinMode(LATCH_A, OUTPUT);
pinMode(LATCH_B, OUTPUT);
pinMode(CH8, INPUT); // Slingshot Latching Piston Channel
pinMode(COMP, OUTPUT);
pinMode(CH9, INPUT); // Compressor Channel
}
void loop()
{
// HIGH values correspond to analog values greater than the resting point of the analog sticks. LOW values correspond to analog values lower than the resting point of the analog sticks.
// OFF means the control is in the resting position
if (digitalRead(CH1) == HIGH && digitalRead(CH2) == HIGH) { // Drive Forward
state = 1;
}
else if (digitalRead(CH1) == LOW && digitalRead(CH2) == LOW) { // Drive Backward
state = 2;
}
else if (digitalRead(CH1) == HIGH && digitalRead(CH2) == LOW) { // Turn Left
state = 3;
}
else if (digitalRead(CH1) == LOW && digitalRead(CH2) == HIGH) { // Turn Right
state = 4;
}
else if (digitalRead(CH1) == HIGH && digitalRead(CH2) == HIGH && digitalRead(CH7) == HIGH) {
// Drive Forward w/ Piston Down
state = 5;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH7) == HIGH) { // Golf Ball Piston Extend
state = 6;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH7) == LOW) { // Golf Ball Piston Retract
state = 7;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH3) == HIGH && digitalRead(CH4) = HIGH) { // Actuators Extend
state = 8;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH3) == LOW && digitalRead(CH4) == LOW) { // Actuators Retract
state = 9;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH5) == HIGH && digitalRead(CH6) == HIGH) { // Retract Slingshot
state = 10;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH5) == OFF && digitalRead(CH6) == OFF && digitalRead(CH8) == HIGH) { // Latch Slingshot
state = 11;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH5) == LOW && digitalRead(CH6) == LOW) { // Release Slingshot
state = 12;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH5) == OFF && digitalRead(CH6) == OFF && digitalRead(CH8) == LOW) { // Unlatch Slingshot
state = 13;
}
else if (digitalRead(CH1) == OFF && digitalRead(CH2) == OFF && digitalRead(CH9) == HIGH) { // Start Compressor
state = 14;
}
switch (state) {
case 1:
forward();
break;
case 2:
backward();
break;
case 3:
turnRight();
break;
case 4:
turnLeft();
break;
case 5:
forward_pist();
break;
case 6:
gb_piston_ext();
break;
case 7:
gb_piston_retr();
break;
case 8:
lift();
break;
case 9:
lower();
break;
case 10:
ss_retr();
break;
case 11:
latch();
break;
case 12:
ss_release();
break;
case 13:
unlatch();
break;
case 14:
comp_power();
break;
}
}
If any of you have any feedback, I'd more than appreciate it. I'd love to discover that my method will not work sooner rather than later. Thank you!