is it possible to use this kind of switch's in arduino for toggle on and off some functions?/
if soo how does the schematic and coding work for this.
Yes
how does the schematic and coding works for this?
How did you manage to sneak such a brief reply past the forum software?
because he can do it
Yes, however you should use pull-up or pull-down resistors aswell.
Also keep in mind, if necessary, to implement some debouncing.
Presumably, it is a switch; it is either open or closed.
Assuming that the switch has 2 contacts that open and close then just connect one to Arduino GND and the other to an Arduino pin with a pinMode() of INPUT_PULLUP just like any other switch and use digitalRead() to determine whether the switch is open or closed
I could tell you but you know what the consequences for you would be..
Actually I have no idea. It just worked
somthing like this?
#define button 2
int buttonpress = OPEN;
void setup() {
Serial.begin(9600);
pinMode(button,INPUT_PULLUP);
}
void loop() {
buttonpoll = digitalRead(button);
if ( buttonpress == OPEN ){
Serial.print("off");
if( buttonpoll == CLOSED ){
Serial.print("on");
}
}
}
What did the compiler say?
i was expecting this..lol...instead of open or close i tried 0 and 1
but it just power off the arduino if i close the switch on once i open the switch its powers on back
Yes, but that could get confusing if "closed" means digitalRead returns 0.
with use of 1 and 0 as int values... its just power off the arduino board..
It sounds like you have wired the switch wrongly. Post a diagram of your circuit
That looks OK if you are using pin 4 in the code (unlike the code in post #11). For now, remove the switch from the circuit and use a patch wire to connect/disconnect pin 4 from GND which is, after all, what the switch is doing
Please post the full sketch that you are using
Would this work?
#define button 4
int buttonpress = 0;
void setup() {
Serial.begin(9600);
pinMode(button,INPUT_PULLUP);
}
void loop() {
buttonpoll = digitalRead(button);
if ( buttonpress == 0 ){
Serial.print("off");
if( buttonpoll == 1 ){
Serial.print("on");
}
}
}
A simple "else" is all that's required