Hot answers tagged

4

It's basically a push button. Possible examples depend on what you want to do. As it seems, you simply want to start interfacing it with Arduino, just use it as a normal push button. Use the DigitalInputPullup example, that comes with the Arduino IDE: Middle pin to pin 2 on the Arduino One of the other pins to ground (depending on which state you want to be ...


3

Let's assume the switch is on pin 9, that the pinMode() has already been set, and that the switch reads HIGH for loop operation. #define MODE_SWITCH 9 #define DO_LOOP HIGH // This loop will execute once if the mode-switch is off, or // will execute continously if the mode-switch is on: do { servo_things(); stepper_things(); motor_things(); } while( ...


3

When you want to perform actions in response to button presses, you should take care of reacting to the signal edges rather than levels. In other words, you perform the action only when the signal changes from LOW to HIGH, not every time you see it HIGH. You should also read about button bounce, and either implement your own debouncing or use an existing ...


3

Since it has two possible positions, and three pins, you have a connection between pin 1 and 2 when position 1 is selected, and between pin 1 and 3 when position 2 is selected. As BSF comment below, the middle pin is common. This means it will be connected to the outside pins depending on where the switch is moved towards. However, you always can verify ...


3

I suggest this simple circuit: simulate this circuit – Schematic created using CircuitLab Set both inputs to INPUT_PULLUP. If the input reads LOW, that means it has been grounded by the switch. Edit: Per Duncan’s request, here is the alternative circuit with the polarities inverted. With the switch’s common connected to Vcc, the inputs should be set ...


2

As others have said, this is a SPDT (Single Pole Double Throw) switch. The center pin is common. When the switch is slid one way, it connects the center pin to the outer pin on that side. When you slide the switch the other way, it connects the center pin to the opposite outer pin. See the link about SPDT switches Edgar provided in his comment: http://www....


1

You talk about wiring a switch to control lights directly. Instead, you want to use your switch to send information to your Arduino. You link to an SPDT (single pole, double throw) switch. When flipped one way, it connects the center pin to one of the outside pins, and when flipped the other way, it connects the center pin to the other outside pin. If you ...


1

These switches are actually a lot more simple than you would think. You have two positions, but you really only care about one of them, since if it's not in that position it must be in the other. The central pin is the common, and when slid to one side it connects that central pin to the corresponding outside pin. So you just use the central pin and one ...


1

Michael has already given you some excellent advice on how to clean up your code. Your switch statement does not make much sense. You're switching based on an out-of-range index into an array of constants. Once you've read the states of all your buttons as outlined by Michael, your loop needs to decide what to do about the state of each switch. Is this a ...


1

Next time align your code with ctrl-K (after selecting). And yes, you can heavily optimize your code (for maintainability) using arrays: Instead of const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 5; const int buttonPin5 = 6; use const int buttonPins[] = { 2, 3, 4, 5, 6 }; In software engineering it's ...


1

According to specs I found online, the NodeMCU wants 7-12V on Vin. Most LiPo cells put out around 4 volts, which isn't enough. The battery voltage may drop and the MCU's voltage regulator probably fails to keep putting out 3.3V when the fan's average power draw exceeds some threshold. It might also be that the fan itself is slowing when the duty cycle ...


1

You could simulate the switch with the button by using a state variable that hold the state of the "switch". I don't know your code, so I can only give you an example on how to do that: // the includes are here, if you need any int flagActive = -1; // ... setup() { // ... set the internal LED to blue } loop() { if ( < switch pressed > ) ...


1

Here is a program that should fade the LED as you specified. Initially the LED is off. If you press the button the LED starts fading within the time frame to its maximum. Then it switches to zero waits a couple of seconds and starts fading from new. If you press the button a second time the LED stops fading and is set to darkness. ;-) This happens emediately,...


1

As usual with programming, there are many solutions, for example: You can use a bitmask, put 4 variables in an array with binary values 1000, 0100, 0010 and 0001 and when the switch is pressed, you store the value into each LED pin by comparing the value. However, a more simpler solution is just use a counter from 0 to 4, if the value is 1, the first LED ...


Only top voted, non community-wiki answers of a minimum length are eligible