Hello everyone, i have a question regarding the use of toggle switches. I am developing a color and money detector for the blind.
My program uses a loop with pre-set color values (If/Else) and returns name of the color as audio. I would like to use a toggle switch to switch between two loops with pre-set values, one loop returns color names and the other one returns monetary values based on colors.
I would like to use 3 pin Toggle Switch SPDT for that, how would i be able to detect the switch state in my code and execute one of the two loops based on that (Would that be a boolean?)?
All help is very much apreciated, i am brand-new to Arduino.
Lberbigier:
I would like to use 3 pin Toggle Switch SPDT for that, how would i be able to detect the switch state in my code and execute one of the two loops based on that (Would that be a boolean?)?
There are several ways to do it, but yes, a "boolean" approach would be the simplest.
Connect the middle pin of the switch to a digital input on Arduino. Connect one side pin to the 5V line (through a pull-up resistor). Connect another side pin to the GND. Done. You can mirror the setup and use pull-down resistor instead of pull-up one. Or even both.
Now you will be able to digitalRead the state of the switch as either LOW or HIGH value on that pin.
You can also achieve the same with a 2-pin toggle switch, by connecting it in the very same way push buttons are typically connected to Arduino inputs (https://www.arduino.cc/en/Tutorial/Button). By default a digital pin is "dominated" by a pull-down resistor, but when you close the switch it connects it to 5V, pulling it up to HIGH. Again, you can mirror the setup and use pull-up resistor instead of pull-down one.
You can perfectly fine use your 3-pin switch in that 2-pin role as well. Simply use the center pin and any one of the side pins.
This latter approach might even be the better one. In the former approach the center pin will remain in floating state in that short period while the switch is switching from one side pin to the other. In the latter approach the pin is never floating.
I was indeed able to do this with your feedback and a little more online research, however, i'm using 3.3v, as 5v seems to draw too much power and powers off my other modules.
Thank you very much!
Then you wired it incorrectly. You have inadvertently connected 5V to ground. Or 3.3V.