I am just starting a project that involves reading what position a 3 position mechanical switch is in (off, single, burst) and controlling a servo based on that position if a mechanical button (momentary switch) is pressed.
off = do nothing... except maybe make sure the servo stays at 0
single = turn 0 to 180 and back to zero one time per button press as quickly as possible (servo speed seems very slow using the servo library)
burst = turn 0 to 180 to 0 to 180 ... continuously as long as button is held down, returning to 0 when it is released
I'm getting quickly frustrated. I tested the servo first with the a simple push botton and the example code. It worked fine, but way too slow. Now,
I'm testing the switch with a simple serial write to see if the voltage is going where it's supposed to... but it's not. I have the 5v from the Uno board going through the momentary switch, then through the 3 position switch to three input pins on the Uno. Right now, without the momentary pressed, I have voltage coming through. Here's the code that tests it. I don't know if I need pull-down resistors or something similar somewhere in my circuit or if I just need to debounce and use edge detection... or something else. Any help is greatly appreciated. If I could, I'd upload a schematic of my circuitry, but I don't know how to do that.
here's the code portion I'm testing right now and having trouble with the voltages...
int pos = 0; // variable to store the servo position
const int buttonPinSingle = 2; // the number of pin V will be on if switch is set to single shot
const int buttonPinBurst = 4; // the number of pin V will be on if switch is set to burst
const int buttonPinOff = 6; // the number of pin V will be on if switch is set to off
const int ledPin = 13; // the number of the LED pin
const int servoPin = 9;
int buttonStateSingle = 0; // variable for reading the switch status - high if 5V, low if 0V.
int buttonStateBurst = 0; // variable for reading the switch status - high if 5V, low if 0V.
int buttonStateOff = 0; // variable for reading the switch status - high if 5V, low if 0V.
int setting = 0; // variable for changing the case dependant on the switch setting
void setup()
{
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize each switch pin as an input:
pinMode(buttonPinSingle, INPUT);
pinMode(buttonPinBurst, INPUT);
pinMode(buttonPinOff, INPUT);
// initialize serial communication
Serial.begin(9600);
}
void loop()
{
// read the state of the 3 position switch:
buttonStateSingle = digitalRead(buttonPinSingle);
buttonStateBurst = digitalRead(buttonPinBurst);
buttonStateOff = digitalRead(buttonPinOff);
/* pos = 0; // ? set servo position to 0 to begin */
// check which setting the switch is on.
// the buttonState is HIGH for the position it is set to (off, single, burst), but all should read LOW if the momentary switch isn't pushed
if ((buttonStateOff == HIGH && buttonStateSingle == LOW && buttonStateBurst == LOW) || (buttonStateSingle == LOW && buttonStateBurst == LOW && buttonStateOff == LOW )) {
setting = 0;
}
else if (buttonStateBurst == HIGH && buttonStateSingle == LOW && buttonStateOff == LOW) {
setting = 2;
}
else if (buttonStateSingle == HIGH && buttonStateBurst == LOW && buttonStateOff == LOW) {
setting = 1;
}
switch (setting) {
case 0: //switch set to off
Serial.println("off");
break;
case 1: //switch set to single
Serial.println("single");
break;
case 2: //switch set to burst
Serial.println("burst");
break;
}
Besides the switch voltage problems I'm having, I wonder if this is worth proceeding with because I'm very new to all of this and keep wondering how safe my circuits and code are. I don't want to burn up the Uno or my servo. Also, this particular application cannot allow for a stray servo command. I'm not sure what safety measures (code and circuitry) I need to use.