So from the code , basically I want to switch on and let the led increase it brightness steadily and off whenever I want. So I what I need is a toggle ON- OFF switch only.
What I usually do with switches is to connect the switch between the IO pin and ground, put the IO pin into input mode
pinMode(pin, INPUT);
then pull the pin up internally
digitalWrite(pin, HIGH);
So when the switch is ON the IO pin is grounded and thus will read LOW, when the switch is OFF the internal pullup asserts the pin HIGH when read.
You just have to be sure you NEVER put the pin into output mode and set it high or you'll blow the pin. You can avoid that possibility (old sketch on the arduino for example) by connecting a 220 ohm resistor between the switch and ground. Or configure it a different way with very high resistor like 1M between the IO pin and ground, and using the switch between +5V and the IO pin. Then the pin will read HIGH when the switch is on on and LOW when the switch is off. Probably best to use a lower resistor between the switch and +5V as well for safety.
Paul__B:
First circuit almost correct, except that the resistor goes between the switch and the Arduino pin.
Yeah sorry, you're right. It wouldn't work the other way around would it?
Picture amended. Apologies to Paul if I put the switch common in the wrong place.
Remember never to catch java.lang.Exception as well.
Hi, I think what luckystar wants is to know how to connect THAT switch pictured.
If it is like most miniature toggles switches it will be set up so that the centre terminal is common.
The outer terminals will be connected to the common depending on which way the toggle is set.
So to use it as a simple on off switch, use the centre and on of the outer terminals.
If you have a DMM you can quickly work out which way the toggle goes to connect either outer terminal to the centre.
Hi, steinie44, my posting was made to this thread at the same time as yours, you just hit the save button, 9 seconds earlier than me.
No offense mate.
(Great minds think alike>)