Project Guidance -- Coding Structure

Hey everybody, this is one of my first posts here but I've been hawking the forums for a long while now.

I am an electronic engineering technician coming up on graduation. I have been working on my final project here and have most of the fine details worked out. I'm looking for some guidance in the coding structure if anybody has any thoughts or ideas and some time. I don't need somebody to walk me through step by step -- but I am making some sort of mistake here that I need assistance correcting. So I'll try to provide as much detail pertaining to my project as I possibly can so you can get the big picture here.

What I am constructing is a 2 axis servo control for a camera. I hope to apply this either as either a home security system OR as a FPV for an rc car/plane. I've got a pan/tilt module with 2 servos -- 1 for each axis. I also have 3 normally open, press button switches -- that I wish to use to select the "state" in which I will be reading the inputs.

I wish to have state 1 select free look using a 2-pot thumbstick.

I wish to have state 2 LOCK the thumbstick values and continuously pulse the servos according to what the joystick reads at the time of selecting pressbutton 2.

Lastly I wish to have button state 3 reserved as an automated sweep in which I will have a series of pulses to each servo -- guiding the camera to sweep the field of site back and forth automatically.

Essentially, I am trying to establish a structure in which there are 3 conditions. Depending on which pressbutton is activated, I wish to store into a variable either a 0,1, 2, or 3 -- which I hope to then say:

"while variable = 0; do nothing"
"while variable = 1; free look using thumbstick"
"while variable = 2; lock servos in place"
"while variable = 3; automatically sweep"

So I am also multiplexing a tri-color LED to represent which condition is running (red = lock, green = sweep, blue = free look).

When no pressbutton = HI, variable = 0 and do nothing.
When pressbutton 1 = HI, variable = 1, run subroutine 1 (free look)
When pressbutton 2 = HI, variable = 2, run subroutine 2 (lock)
When pressbutton 3 = HI, variable = 3, run subroutine 3 (sweep)

This is the essential goal I am aiming for. I am having trouble initializing the outputs to a specific state. I need my LED outputs to initialize to the HIGH condition (because they are common cathodes and I require a LOW to turn them on).

I also tried using If statements as well as While statements to set up the parameters for each subroutine. But it seems as if "variable" is not changing when I temporarily close any of the selected pressbutton switches. I am trying to upload a basic block diagram depicting the structure I am aiming for. Hopefully this helps visualize my goal.

Any input on how to set up the structure for which I can build upon -- would be greatly appreciated. Even a few pointers to get my in started in the right direction would suffice.

I appreciate you guys taking the time to read through and give me a hand here. MUCH appreciated. Thanks you guys -- I look forward to some responses and getting some positive progress made here!

Forget the while piece - loop will get called repeatedly anyway. In loop, read the buttons and use them to set a single variable that tells you what state you're in. Then use a switch statement on the state variable to invoke the code appropriate to that state.

Look at the following

  1. Blink without delay,

  2. Software debounceing (for the buttons).

  3. FSM (Finite state machines) in the the playground.

Mark

I've already been going mad with switch bounce here. The switch case seems to work but I haven't been able to permanently store the case state into the variable. It only appears to store the variable WHILE i'm pressing the switchbutton -- but not after I've released it.

That and it seems as if my switch is bouncing and acting sparatically.

The switch case seems to have some potential here. I'm going to check out everything else suggested too.

This is what I came up with so far and it seems to work really well. I basically just need to fill out the code under each case for each desired effect on each servo. Thanks a ton for suggesting switch case. Also good looks on the debouncing and blink without delay -- some really good button state features I never knew existed popped right out at me immediately. Any other suggestions are still greatly appreciated.

Does anybody know if I should be alright running 2 small 9g servos off the 5v supply of the arduino or if I should find an alternate source?

const int ledgreenfree = 13; // Pin that Green (1) LED is attached to
const int buttongreenfree = 2; // Pin that Green (Freelook - 1) button is attached to
const int ledbluesweep = 12;
const int buttonbluesweep = 3;
const int ledredlock = 11;
const int buttonredlock = 4;

// These variables will change:
int state = 0; // ConditionSelect

void setup() {

pinMode(ledgreenfree, OUTPUT);
pinMode(buttongreenfree, INPUT);
pinMode(ledbluesweep, OUTPUT);
pinMode(buttonbluesweep, INPUT);
pinMode(ledredlock, OUTPUT);
pinMode(buttonredlock, INPUT);
}

void loop() {
if (digitalRead(buttongreenfree) == HIGH){
state = 1;
}
if (digitalRead(buttonbluesweep) == HIGH){
state = 2;
}
if (digitalRead(buttonredlock) == HIGH){
state = 3;
}
switch (state){
case 0:
digitalWrite(ledgreenfree, HIGH);
digitalWrite(ledbluesweep, HIGH);
digitalWrite(ledredlock, HIGH);
break;
case 1:
digitalWrite(ledgreenfree, LOW);
digitalWrite(ledbluesweep, HIGH);
digitalWrite(ledredlock, HIGH);
break;
case 2:
digitalWrite(ledgreenfree, HIGH);
digitalWrite(ledbluesweep, LOW);
digitalWrite(ledredlock, HIGH);
break;
case 3:
digitalWrite(ledgreenfree, HIGH);
digitalWrite(ledbluesweep, HIGH);
digitalWrite(ledredlock, LOW);

}
}