Project Guidance on State Selection and Seven Segment Displays

Hi there,

I was hoping to program an Arduino to have 4-5 different "states", with each state turning a motor and having a different length of delay before the motor is activated. The different "states" would need to be toggle-able with a switch or other input device, so only one state runs at one time. Finally, the current state number would need to be displayed on a seven segment display. (Probably displayed as 0-7, or something of the like)

I have so far been able to get the motor running with differing delays, but have a trouble creating different states to select from, and how to translate a state number to a seven segment display. If someone could give some expertise on how to create these different states, how to select only one to run and how to display which state is running on a seven segment display, it would be much appreciated.

Thanks.

Your code . . ?

One way (and I'm no expert here) would be to have a bunch of switches, enough to give a binary represetation of how many states you need. So you could get 4 states with 2 switches, and 3 would give 8. Then have some functions to do the stuff that each state needs.

Then you have a load of "if" statements, looking at the combination of the switches like this... (pseucocode at the this stage)

//this is PSEUDOCODE
if (switch0 is open and switch1 is open and switch2 is open)
{
   display "0" on 7seg
   invoke the function "state0"
}

if (switch0 is closed and switch1 is open and switch2 is open)
{
   display "1" on 7seg
   invoke the function "state1"
}

My code
//L293D
//Motor A
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
void setup(){
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);

}

//This will run only one time.
void loop(){
delay (1000);
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);

//Motor Control - Motor A: motorPin1,motorpin2 & Motor B: motorpin3,motorpin4
//This code will turn Motor A clockwise for 2 sec.
analogWrite(motorPin1, 180);
analogWrite(motorPin2, 0);

delay(1000);
//This code will turn Motor A counter-clockwise for 2 sec.
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 180);

delay(1000);

//And this code will stop motors
analogWrite(motorPin1, 0);
analogWrite(motorPin2, 0);

}

Maybe you should read about functions, and put each of those things you want to do in a function of their own, then call the right one from the right part of the "if" as I pseudo-coded above.

BTW if you didn't already know, there's a seven segment display library.