3 pin 3 position toggle switch

so im using some 3 way toggle switches to control a motor. ive got the switch wired so that the pins on the end go to digital 9 and 10 with the common going to gnd. it runs to motor forward in position one nd stops in positions 2 put has no response to what should make it run in reverse . any ideas?

below is my code:

int m1 = 6; // motor 1 enable
int in1 = 8; // motor 1 input 1
int in2 = 7; // motor 1 input 2
int m2 = 3; // motor 2 enable
int in3 = 5; // motor 2 input 1
int in4 = 4; // motor 2 input 2
int pot = A1; // pot for speed control
int pos1 = 9; // forward position
int pos2 = 10; // rear position

void setup() {
pinMode(m1, OUTPUT); // set pin as output
pinMode(in1, OUTPUT); // set pin as output
pinMode(in2, OUTPUT); // set as output
pinMode(m2, OUTPUT); // set as output
pinMode(in3, OUTPUT); // set as output
pinMode(in4, OUTPUT); // SET AS output
pinMode(pos1, INPUT_PULLUP); // set button to inpput w/ pullup resisstor
pinMode(pos2, INPUT_PULLUP);

}

void loop() {
int spd = analogRead(pot); // read pot positi on

spd = map(spd, 0, 1023, 70, 254); // map pot range to pwm range

byte state1 = digitalRead(pos1); // set btn state
byte state2 = digitalRead(pos2); // set btn state

if (state1 == LOW) { // if swith in forward postion run motor forward a set speed
digitalRead(spd);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(m1, spd); // set motor 1
}

if (state2 == LOW) { // if switch in rear position run motor backwards at set speed
digitalRead(spd);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(m1, spd);

}

else { // motor off, mid position
analogWrite(m1, 0);
analogWrite(m2, 0);
}
}

What kind of motor?

To me, it seems like all state2 does is pull motor ones enable pin down, which apparently makes it not run, which is why nothing happens when that selection is made. But this is a complete guess because I know nothing about your motor.

Use the “how to use these forums” links on top of this page and reformat your post.

It's just a lil 5v geared hobby motor. Like one of the yellow ones.
My understanding is that when the switch is in the forward on position (on-off-on switch) it should trigger the code that runs the motor forward. Mid position should be off and then the rear on position code should run the motor in reverse.

Have you verified that the code is seeing the switch states you expect?

The else clause only associates with the preceding if so your else needs to be replaced with

if (state1 == HIGH) && (state2 == HIGH) { // motor off

The motors turn off fine it's getting it to run in reverse that's being a pain.

DougP yes I did verify that it is detecting both positions for the switch. Just isn't translating to the motor

zjett23:
The motors turn off fine it's getting it to run in reverse that's being a pain.

Well if you use m1 in both clauses how can it work? You declare m1 and m2 and only set m1.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
What model Arduino.
Link to specs/data of motor controller please.

Thanks.. Tom... :slight_smile: