# Having trouble alternating two outputs per single input

Hello, I am new to all of this but addicted already. I have a project with a model vehicle with a rechargeable battery and a 24 volt motor .It rides a line track and when it reaches the end it receives a charge from a bus for thirty seconds via an external circuit and proximity switches and reverses motor polarity and at the other end it repeats. I have achieved this with relay logic but now I believe the UNO can do this via h-bridge for the polarity change but the problem Im having is the H-bridge has two inputs one for forward and one for reverse.Each time it "sees" the charge bus with a single input, I need to alternate two inputs individually and hold . How would one input trigger an output the first time and hold for forward and then that same input on the other end of the track with a charge bus trigger another output state only and repeat so that the motor reverses polarity and so on? I have tried to word this in my searches before posting as to not waste anybody's time but I keep getting unsatisfactory results. Like alternating leds per input for example. Thank you in advanced if you have the time Im stuck and again a complete novice.

If the H bridge inputs are compatible with the Uno outputs, and if the input to the Uno is compatible with the signals at the ends of the line, then this is easy to do.

What code do you have so far?

If you want code written for you, try in Gigs and Collaborations and bring your wallet.

Welcome.
"addicted " Yes it sounds like you have the disease.
Personally I think hearing relays click is more soothing

Each time it "sees" the charge bus with a single input, I need to alternate two inputs individually and hold . How would one input trigger an output the first time and hold for forward and then that same input on the other end of the track with a charge bus trigger another output state only and repeat so that the motor reverses polarity and so on?

Can you explain this down a bit more?

Just asking if it was a simple structure statement or a more advanced code. The arduino will only receive 5 volts on its input and only 5 volts are needed from its outputs. I dont need a program from start to finish just trying to figure out if one input received can alternate two. for example, say input 2 receives a "1" momentarily and output 7 delivers 5v until input 2 receives a "1" again output 8 delivers 5 volts and ouput 7 is back to zero until input 2 receives a "1" again and the process loops. When one output is on the other is off.

Yes you can do this.
There is an example called 'StateChangeDetection' you should look at.

.

This is called a state machine. It's a pretty standard pattern:

``````enum State {
CHARGING_END_A,
MOVING_TO_END_B,
CHARGING_END_B,
MOVING_TO_END_A
}
state = CHARGING_END_A; // starting state

void loop() {
switch(state) {
case CHARGING_END_A:
if(we have finished charging) {
digitalWrite(hbridge_forward, HIGH);
state = MOVING_TO_END_B;
}
break;

case MOVING_TO_END_B:
if(we have hit the bumper at end B) {
digitalWrite(hbridge_forward, LOW);
state = CHARGING_END_B;
}
break;

case CHARGING_END_B:
if(we have finished charging) {
digitalWrite(hbridge_reverse, HIGH);
state = MOVING_TO_END_A;
}
break;

case MOVING_TO_END_A:
if(we have hit the bumper at end A) {
digitalWrite(hbridge_reverse, LOW);
state = CHARGING_END_A;
}
break;

}
}
``````

On the outputs, the current required is important also.

More than 40 mA when high or low would be a concern.

This works for some H bridges, others need a driver.

seems over complicated to me because Im so new . All I need is help figuring out as if the Arduino Had a momentary button and two leds. Say a green one and a red one. You push the button and only the green led lights, you push the button again and the green one turns off and the the red led lights up .Then when it is depressed again back to green and so on. That is actually all I need. Nevermind it being a vehicle on a track or if the Arduino can handle this or that current. I have all of that figured out with a stable circuit that only needs a high and a low to work. I have done this with relays by latching and de-latching relays but I just want to go solid state for the fun of it but with an Arduino . To give you an idea on how new I am I have only bought an Arduino a week ago and just now starting learn c++. Im a greenhorn but can see its potential . Sorry if I wasted anybodys time.

" Sorry if I wasted anybodys time."

Asking for help is never a waste of time.

If you detect when a switch goes from LOW to HIGH and you are in state ONE your code does something, then you go to state TWO.
When you go from LOW to HIGH and you are in state TWO your code does something else, then you go back to state ONE.
The process then repeats.

.

Mechanical switches bounce. This does not matter if you just want something to happen while the switch is closed (or open). It does matter when you are going to count something with the switch (like states). A little debouncing is in order. It is not very complicated and will avoid a lot of frustration.

There are debouncing examples available.

for now im just trying to make the leds do what I explained. im lost

.

All I need is help figuring out as if the Arduino Had a momentary button and two leds. Say a green one and a red one. You push the button and only the green led lights, you push the button again and the green one turns off and the the red led lights up .Then when it is depressed again back to green and so on. That is actually all I need.

``````byte buttonState = 0;
byte lastButtonState = 0;
unsigned long debouncePeriod = 25;
byte configuration = 0;
const byte redLed = 11;
const byte greenLed = 12;
const byte buttonPin = 5;

void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

if (configuration == 1)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}
else if (configuration == 2)
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
}

if ((buttonState == LOW) && (lastButtonState == HIGH))//was button pressed and changed using logic for input pullup
{
delay(debouncePeriod);
if ((buttonState == LOW) && (lastButtonState == HIGH)) {
configuration++;
//reset configuration
if (configuration == 3)
configuration = 1;
}
}
lastButtonState = buttonState;
}
``````

This is the sketch I tried but kept getting my brackets in the wrong place.I need to study so much more but I am enjoying it.
I used the state change example that Larry d proposed as a skeleton to build on.Then was going to try to utilize the serial print within that to keep track of how many cycles it made. But Im thinking thats not a good idea because maybe it takes up too much memory. However, Cattledog actually made it and it works great. But I want to look at that sketch learn from it plus much more studying needed. I signed up for a year of Proamming Electronics Academyand make my way through those mods for better understanding.If it is ok I would like to use the sketch Catlledog created in my circuit.I really appreciate the help and quite scincere on learning and not just cut and pasting other peoples work. Just wanted to see this thing go. Thank you again for being so helpful. When I first uploaded it it worked but at the getgo both leds are off until it is first engaged. Going to try figure out how to make one of them on when it is powered up.

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int gledPin = 7; // the pin that the LED is attached to
const int rledPin = 8;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(gledPin, OUTPUT);
pinMode(rledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:

// compare the buttonState to its previous state
if (buttonState != lastButtonState)} {
// if the state has changed, increment the counter
if (buttonState == HIGH) }{
// if the current state is HIGH then the button
// wend from ccw to cw:
buttonPushCounter++;
Serial.println("cw");
Serial.print("monorail cycled ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from cw to ccw:
Serial.println("ccw"); }

{
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

if {(buttonState == 0) {

digitalWrite(gledPin, HIGH);

digitalWrite(rledPin, LOW);
if (buttonState ==1);

}else {
digitalWrite(gledPin, LOW);

digitalWrite(rledPin, HIGH);
}
}

when: byte lastButtonState = 0; was changed to : byte lastButtonState = 1; one of the two leds was on.

when: byte lastButtonState = 0; was changed to : byte lastButtonState = 1; one of the two leds was on.

If you have your button wired for INPUT_PULLUP where a pressed button reads LOW, this should not work with the sketch I provided.

If you want a light to be on at startup without pressing the button make configuration = 1 or 2 when the variable is declared.