Project question

So I'm trying to write a code using using 2 toggle switches (one 2-way and one 3-way) and a standard button with a servo motor. I've been trying if loops and can't get the motor to stop when it reaches zero. Is there something I'm missing in the code or should I be using a different command rather than the if loop?

Here is my current code and it mostly works.

``/*
Project_Code_5
*/

#include <Servo.h>
Servo myservo; // Define the servo name
int togglePin2 = 2; // toggle switch is connected to pin 2 -- for sitting ('on' for two-way toggle)
int togglePin3 = 3; // toggle switch is connected to pin 3 -- for ascending stairs ('C' for three-way toggle)
int togglePin4 = 4; // toggle switch is connected to pin 4 -- for descending stairs ('G' for three-way toggle)
int buttonPin5 = 5; // button switch is connected to pin 5 -- for general walking
int val2; // variable for reading toggle2 status
int val3; // variable for reading toggle3 status
int val4; // variable for reading toggle4 status
int val5; // varialbe for reading button5 status
int toggleState2; // variable to hold last toggle2 state
int toggleState3; // variable to hold last toggle3 state
int toggleState4; // variable to hold last toggle4 state
int buttonState5; // variable to hold last button5 state

void setup()
{
myservo.attach(10); // servo is on digital pin 10
pinMode(togglePin2, INPUT); // set toggle2 as input
pinMode(togglePin3, INPUT); // set toggle3 as input
pinMode(togglePin4, INPUT); // set toggle4 as input
pinMode(buttonPin5, INPUT); // set button5 as input

}

void loop() {
val2 = digitalRead(togglePin2); // read input value of toggle2 and store it in val2
val3 = digitalRead(togglePin3); // read input value of toggle3 and store it in val3
val4 = digitalRead(togglePin4); // read input value of toggle4 and store it in val4
val5 = digitalRead(buttonPin5); // read input value of button5 and store it in val5

if (val2 > 0) {
myservo.write(200);
delay(30);
}
if (val3 > 0) {
myservo.write(200);
delay(3000);
myservo.write(0);
delay(3000);
}
if (val4 > 0) {
myservo.write(180);
delay(3000);
myservo.write(0);
delay(3000);
}
if (val5 > 0) {
myservo.write(135);
delay(2000);
myservo.write(0);
delay(2000);
}
if (val2 == 0 && val3 == 0 && val4 == 0 && val5 == 0) {
myservo.write(0);
}
}``

if (val2 == 0 && val3 == 0 && val4 == 0 && val5 == 0) {
   myservo.write(0);

Is this a continuous rotation ex-servo?
Did you mean

if (val2 == 0 && val3 == 0 && val4 == 0 && val5 == 0) {
   myservo.write(90);

?

What measures do you have to prevent your switch inputs floating?

I'm not entirely sure what you mean by "switch inputs floating".
The Servo is a brushing digital servo. Traxxas 2018, to be specific.
And no, I want the motor to stop at a designated 0 degree origin, not 90 degrees.

with goto's it should be simpler,lighter and faster :wink:

How so?

Don't go there.
Therein lies madness and . . . hairloss

KatBME:
I'm not entirely sure what you mean by "switch inputs floating".

This means the Arduino pin connected to the switch is not connected to GND or 5v when the switch is in one position. In that situation digitalRead() can give unpredictable results. The simplest solution is to use pinMode(pin, INPUT_PULLUP) which connects the pin internally to 5v through a resistor and then use your switch to pull the pin to GND when pressed.

...R

so in this case, the wire that would normally connect the switch the power is connected to the ground? Or am I miss understanding this?

So I rewrote my code with the INPUT_PULLUP config and I want to make sure I'm setting it all up correctly.

``/*
Project_Code_7
*/

#include <Servo.h>
Servo myservo; // Define the servo name
int togglePin2 = 2; // toggle switch is connected to pin 2 -- for sitting ('on' for two-way toggle)
int togglePin3 = 3; // toggle switch is connected to pin 3 -- for ascending stairs ('C' for three-way toggle)
int togglePin4 = 4; // toggle switch is connected to pin 4 -- for descending stairs ('G' for three-way toggle)
int buttonPin5 = 5; // button switch is connected to pin 5 -- for general walking
int val2; // variable for reading toggle2 status
int val3; // variable for reading toggle3 status
int val4; // variable for reading toggle4 status
int val5; // varialbe for reading button5 status
int toggleState2; // variable to hold last toggle2 state
int toggleState3; // variable to hold last toggle3 state
int toggleState4; // variable to hold last toggle4 state
int buttonState5; // variable to hold last button5 state

void setup()
{
myservo.attach(10); // servo is on digital pin 10
pinMode(togglePin2, INPUT_PULLUP); // set toggle2 as input
pinMode(togglePin3, INPUT_PULLUP); // set toggle3 as input
pinMode(togglePin4, INPUT_PULLUP); // set toggle4 as input
pinMode(buttonPin5, INPUT_PULLUP); // set button5 as input

}

void loop() {
val2 = digitalRead(togglePin2); // read input value of toggle2 and store it in val2
val3 = digitalRead(togglePin3); // read input value of toggle3 and store it in val3
val4 = digitalRead(togglePin4); // read input value of toggle4 and store it in val4
val5 = digitalRead(buttonPin5); // read input value of button5 and store it in val5

if (val2 = LOW) {
myservo.write(200);
delay(30);
}
if (val3 = LOW) {
myservo.write(200);
delay(3000);
myservo.write(0);
delay(3000);
}
if (val4 = LOW) {
myservo.write(180);
delay(3000);
myservo.write(0);
delay(3000);
}
if (val5 = LOW) {
myservo.write(135);
delay(2000);
myservo.write(0);
delay(2000);
}
if (val2 = HIGH && val3 = HIGH && val4 = HIGH && val5 = HIGH) {
myservo.write(0);
}
}``

if (val2 = LOW) {

Oops

I fixed the = to ==. How does it look now?

``/*
Project_Code_7
*/

#include <Servo.h>
Servo myservo; // Define the servo name
int togglePin2 = 2; // toggle switch is connected to pin 2 -- for sitting ('on' for two-way toggle)
int togglePin3 = 3; // toggle switch is connected to pin 3 -- for ascending stairs ('C' for three-way toggle)
int togglePin4 = 4; // toggle switch is connected to pin 4 -- for descending stairs ('G' for three-way toggle)
int buttonPin5 = 5; // button switch is connected to pin 5 -- for general walking
int val2; // variable for reading toggle2 status
int val3; // variable for reading toggle3 status
int val4; // variable for reading toggle4 status
int val5; // varialbe for reading button5 status
int toggleState2; // variable to hold last toggle2 state
int toggleState3; // variable to hold last toggle3 state
int toggleState4; // variable to hold last toggle4 state
int buttonState5; // variable to hold last button5 state

void setup()
{
myservo.attach(10); // servo is on digital pin 10
pinMode(togglePin2, INPUT_PULLUP); // set toggle2 as input
pinMode(togglePin3, INPUT_PULLUP); // set toggle3 as input
pinMode(togglePin4, INPUT_PULLUP); // set toggle4 as input
pinMode(buttonPin5, INPUT_PULLUP); // set button5 as input

}

void loop() {
val2 = digitalRead(togglePin2); // read input value of toggle2 and store it in val2
val3 = digitalRead(togglePin3); // read input value of toggle3 and store it in val3
val4 = digitalRead(togglePin4); // read input value of toggle4 and store it in val4
val5 = digitalRead(buttonPin5); // read input value of button5 and store it in val5

if (val2 == LOW) {
myservo.write(200);
delay(30);
}
if (val3 == LOW) {
myservo.write(200);
delay(3000);
myservo.write(0);
delay(3000);
}
if (val4 == LOW) {
myservo.write(180);
delay(3000);
myservo.write(0);
delay(3000);
}
if (val5 == LOW) {
myservo.write(135);
delay(2000);
myservo.write(0);
delay(2000);
}
if (val2 == HIGH && val3 == HIGH && val4 == HIGH && val5 == HIGH) {
myservo.write(0);
}
}``

How does it look now?

If you want comments on the aesthetics, I'd say the code tags could do with some work, I can't see why the pin numbers aren't constants, I don't know why val2...val5 have global scope, or what the toggleState variables are for.

I assume the compiler is happy?

KatBME:
so in this case, the wire that would normally connect the switch the power is connected to the ground? Or am I miss understanding this?

I am a little concerned that you may be thinking of wiring in such a way as to cause a short circuit. With INPUT_PULLUP the power connection is inside the Arduino and the switch is connected so that when pressed it connects the pin to ground.

...R

hehe hairloss
Kat try to use uint8_t when possible

Thanks everyone, I got it working. :smiley: