Hi, I am making a sim racing button box and I was making a 3-way switch, but I want it to only make one input and not make multiple inputs off of just one push. My code is posted below. Any help will be greatly appreciated.
Schematic, please. So we can see what you are seeing.
The extra pin is not used in the schematic because my switch only has 3 soldering points and I couldn't find a 3 way switch in the schematic builder i used.
That is NOT a three-way switch. It is a double pole, double throw switch. AKA DPDT switch. What are you connecting to D12? Are both D12 and D13 defined as input Pullup?
I know its not a Three way, I said I couldn't find a three way in the builder. To answer the second question, yes the D12 and D13 are defined as input pull-up.
Most 3-way or more switches are rotary switches. They should be rather easy to find.
ALL contacts, switches and buttons bounce and go on and off several times for a few milliseconds. This would be equivalent to multiple presses of the switch.
What you need is to include code for debouncing. You can find an example in the Arduino IDE, and many more in this forum and elsewhere.
And please post your code (not pictures). Chances are you are already debouncing...
HI,
Welcome to the forum.
Can you please post a table and tell us what you want each input D12 and D13 to be with the switch in each position.
I assume your switch is two position and not three?
D12 and D13, HIGH or LOW.
SwitchPosition | D12 | D13 |
---|---|---|
POS1 | ||
POS2 |
Thanks. Tom...
Draw your circuit by hand and post an image.
If you are using a three POSITION switch a table of what its function would help.
Can you post and image of your switch so we can see its terminals?
Thanks.. Tom...
int switchFivePin = 12;
int switchSixPin = 13;
int switchFiveState = HIGH;
int switchSixState = HIGH:
void setup ()
{
Serial.begin(9600);
pinMode(switchFivePin, INPUT_PULLUP);
pinMode(switchSixPin, INPUT_PULLUP);
}
void loop() {
int switchFive = digitalRead(switchFivePin);
int switchSix = digitalRead(switchSixPin);
//Extra Switch 3a
if (switchFive < switchFiveState)
{
Serial.println("Extra Switch 3a");
switchFiveState < switchFive;
}
//Extra Switch 3b
if (switchSix < switchSixState)
{
Serial.println("Extra Switch 3b");
switchSixState < switchSix;
}
delay (100);
}
New to the fourm, hope this works lol
What are you trying to do here?
To be honest I'm also new to coding in general, its basically supposed to mean that the switch should return back to the normal state, at least it works somehow
Ok, one step at a time. Although not "formally", this line gets the debouncing out of the way.
Now, try changing
to this:
//Extra Switch 3a
if (switchFive != switchFiveState)
{
Serial.println("Extra Switch 3a");
switchFiveState = switchFive;
}
You can read a discussion on the debouncing issue here: https://forum.arduino.cc/t/leading-edge-debouncing/964407
It does work the way I want it too, except it makes another input when it goes back to the center. I only want it to make no inputs when it goes back to the center.
Can you share a diagram or a picture of how your 3-way switch is connected?
Then, perhaps your 3-way switch is a DPDT center-off switch?
Let's see.
Say we start with the switch in the CENTER position.
Then we move it to the TOP position. We need the sketch to detect that.
Then we move it back to the CENTER position. We don't need to know that. The sketch still thinks the switch is in the TOP position.
Then we move it back to the TOP position. Nothing should happen.
Then we move it to the BOTTOM position. We need to know that.
Then we move it back to the CENTER position. We don't need to know that. The sketch still thinks the switch is in the BOTTOM position.
Etc...
Is this how the sketch should work?
I like the idea, but this switch (and many other switches) will be used for sim racing things like brake bias or differential, basically, it needs to go back into the center with the sketch knowing its in the center, but it does nothing.
So basically you need to know when the switch is on TOP, CENTER or BOTTOM
TOP (D13 LOW, D12 HIGH)
CENTER (D13 HIGH, D12 HIGH)
BOTTOM (D13 HIGH, D12 LOW)
And then take whatever action is applicable to each of the 3 positions