But what happens is when we press the button, before fully closing the metal plate bounces between the terminals in microseconds. In theory when a button is pressed the contact should be made and switch should be closed. In switches a metal plate is pressed down to short contacts of the terminals. It is a common problem which at times can be very annoying. Switch bouncing is a real world problem that we mostly ignore in theory. The button press is registered multiple times or isn’t registered at all. The circuit is not working as it is supposed to. It will remain on until the button is pressed again. Now press the button once and the LED should turn on. PinMode(LED, OUTPUT) //Set LED as outputĭigitalWrite(LED, LOW) //Sets LED LOW by defaultītn_state = digitalRead(btn) //Store btn state (HIGH/LOW)in variableĪfter uploading the above code, you will notice that the LED is off by default. Int flag = 0 //Declared variable flag to store switch state Int btn_state = 0 //Declared variable to store state (Low by default) Let’s take a look at the code: #define LED 10 //Declared pin 10 as LED Flags are variables that save the state of a pin (High/low or integers). To use the pushbutton as a toggle switch we need to remember the switch state. For that we will have to make some changes to the code. Let’s say we want to toggle LED on and off using the same switch. This works very well but sometimes we need a different arrangement. The circuit is completed when button is pressed and pins 1,3 and 2,4 are shorted. The pins in vertical line are connected together. Usually push buttons have 4 pins marked as 1,2,3 & 4. To understand the working refer to the diagram. But before that, let’s first see how a push button works. Once you have everything we need, we can connect it all and make a circuit.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |