Can someone tell me where I've gone wrong?

Have hooked up a circuit to try to run a motor on 3 speed settings using a mixture of digitalwrite and pwm.
When I press button 1 I get the expected full power on the motor, button 2 I get what I assume is the expected 2 thirds ish power but on button 3 where I’m expecting less power again I’m getting full power.

Buttons 2 and 1 when pressed together I seem to get less power strangely. . .

Not sure whether it’s my code, wiring or both - can someone point me in the right direction?

const int motorPin = 9;
const int switchPin1 = 2;
const int switchPin2 = 3;
const int switchPin3 = 4;
int switchState1 = 0;
int switchState2 = 0;
int switchState3 = 0;

void setup() {
  pinMode(motorPin, OUTPUT);
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);
  // put your setup code here, to run once:

}

void loop() {
  switchState1 = digitalRead(switchPin1);
  switchState2 = digitalRead(switchPin2);
  switchState3 = digitalRead(switchPin3);
  if (switchState1 == HIGH + switchState2 == LOW + switchState3 == LOW) {
    digitalWrite(motorPin, HIGH);
  } else if (switchState2 == HIGH + switchState3 == LOW + switchState1 == LOW) {
    analogWrite(motorPin, 150);
  } else if (switchState3 == HIGH + switchState2 == LOW + switchState1 == LOW) {
    analogWrite(motorPin, 100);
  } else {
    digitalWrite(motorPin, LOW);
  }
  // put your main code here, to run repeatedly:

}

motorswitch.ino (926 Bytes)

Not sure how to embed the circuit I drew. . .hopefully this link works!

Should you be using "and" or "&&" in your if statements instead of +?

You should change the + signs to && or and. I think if you use the + sign each of the IF tests will return the same value. For example 1 + 0 + 0 is the same as 0 + 1 + 0.

Then you should print the values of the switchStates before the IF statements in case some of them are not what you expect.

...R

Thanks guys, will give it a bash and let you know how I get on

Cheers guys, that's exactly where I went wrong - thought there was still something amiss for a while but I had forgotten to link my battery and board earth!

Thanks again