hi, sorry for my bad english,
i don't know if i put this post in the right place,
i have this program :
if you press limit-switch No.1 more than one. the built-in LED will on,
and if you press limit-switch No.2, it will reset the counter and the built-in LED will off,
and the sketch program something like this :
void setup(){
byte count = 0;
// put pin setup in here
}
void loop(){
if limitnumber01pressed; // if limit number 1 pressed
{
count++;
}
if limitnumber02pressed; // if limit number 2 pressed
{
count=0;
}
if count => 2
{
//turn on the lamp
digitalwrite(LED_BUILTIN, high)
}
else
{
//turn off the lamp
digitalwrite(LED_BUILTIN, low)
}
}
// note: the syntax is wrong because i sketch it using notepad on android (and most part is because of my poor coding skill).
but my self is not an electric guy, so this electronic thing is quite a nightmare for me,
can the code work without resistors and/or without fry up the arduino?
if it can, can you give me a guide, hints, tips, things to avoid, links, keyword in the search engine (note: i not looking for resistor tutorial)
and thanks for reading
Then a resistor inside of the chip will power the pin very weakly.
You would connect the switch to the pin on one side and to ground (GND) on the other side, just pin-wire-switch-wire-ground. The resistor already in the chip is all the resistor you need.
If that pin is not grounded (the switch is open, not conducting) then a digitalRead( switch1 ) will return 1 (same as HIGH).
If that pin is grounded (the switch is closed, conducting) then a digitalRead( switch1 ) will return 0 (same as LOW).
You should find out more about buttons to get around what is called bounce. In your case, a delay(20) after detecting the pin changing state (from HIGH to LOW or LOW to HIGH) should be enough.
Your Arduino IDE has example sketches that show how to do simple things and each example has a page on the Arduino.cc main site, if you set things up the versions you see may be in your native language and be much easier to follow but here is the english link page:
In section 2.Digital you may want to go through the Button, Digital Input Pullup, and State Change Detection examples first.
You can learn a lot of good from sections 1,2,3 and 5. Section 4 shows bad habits for Arduino but okay for PC, avoid it.
Then a resistor inside of the chip will power the pin very weakly.
You would connect the switch to the pin on one side and to ground (GND) on the other side, just pin-wire-switch-wire-ground. The resistor already in the chip is all the resistor you need.
If that pin is not grounded (the switch is open, not conducting) then a digitalRead( switch1 ) will return 1 (same as HIGH).
If that pin is grounded (the switch is closed, conducting) then a digitalRead( switch1 ) will return 0 (same as LOW).
You should find out more about buttons to get around what is called bounce. In your case, a delay(20) after detecting the pin changing state (from HIGH to LOW or LOW to HIGH) should be enough.
Your Arduino IDE has example sketches that show how to do simple things and each example has a page on the Arduino.cc main site, if you set things up the versions you see may be in your native language and be much easier to follow but here is the english link page:
In section 2.Digital you may want to go through the Button, Digital Input Pullup, and State Change Detection examples first.
You can learn a lot of good from sections 1,2,3 and 5. Section 4 shows bad habits for Arduino but okay for PC, avoid it.
thanks for the reply, i have tried the input_pullup before in another project,
and yes that bounce problem appear
(i just know that non-accurate button called bounce and fix called debounce, thanks)
for the "BuiltInExamples" i ignore it before because it usually requires resistors,
but now i will try "the tutorials" if "the tutorial" not require resistor,
anyway is arduino have any syntax to "once the button is pressed" instead "when the button is pressed",
sorry if ask too many and thanks for the reply
thanks for the reply, i study it for some time,
its have a tutorial about not using the external resistor and debounce,
but i will ignore about resistor and the formula in that page because the previous reply said this:
"The resistor already in the chip is all the resistor you need.",
but great news i found "Debouncing without delay" in that page, YAY
and about if statement it seems i wrong write the syntax,
thanks alot
Kucingmiow:
for the "BuiltInExamples" i ignore it before because it usually requires resistors,
but now i will try "the tutorials" if "the tutorial" not require resistor,
The built-in examples are not only there to run, but also to study
It's only a little bit of work to modify them for use with the internal pull-up instead of a external pull-down.
Add the below near or at the top of the code
#define ISPRESSED LOW
Everywhere in the code where the button is tested, you can replace the HIGH or LOW by ISPRESSED and !ISPRESSED respectively.
The commented lines represent the original lines of the example; under them the updated version for use with the internal pull-up
Just be accurate and don't forget to change the *pinMode* statement to change to *INTERNAL_PULLUP*.
thanks for the reply,
yes i know the example for the study, but i still will ignore the resistor, thanks,
that high with ispressed is nice trick, i will add it to my tips and trick list,
but, is that right? "#define ISPRESSED LOW", i assume if is pressed is high,
anyway i think i will added new define : #define ISPRESSED HIGH #define NOTPRESSED !ISPRESSED,
but is there only 2 state? !high == low?
Kucingmiow:
(i just know that non-accurate button called bounce and fix called debounce, thanks)
It is not just about accuracy of the button/switch though there are buttons that do rock the contacts.
It is the physics of electrons sparking across a gap due to voltage (electrical potential).
No matter how small the difference across the gap there is a small enough gap for sparks to jump, always some "bounce".
The name bounce is used because the on-off nature of the sparks makes the signal look like extremely fast bouncing. With an Uno I have sensed bounces as short as 12 microseconds, many of those. And that is as small as my code would see, I'm sure that many smaller bounces occurred.
You have the link to Nick Gammon's blog with his scope picture. You see one way to debounce is putting a capacitor across the switch?
GoForSmoke:
It is not just about accuracy of the button/switch though there are buttons that do rock the contacts.
It is the physics of electrons sparking across a gap due to voltage (electrical potential).
No matter how small the difference across the gap there is a small enough gap for sparks to jump, always some "bounce".
The name bounce is used because the on-off nature of the sparks makes the signal look like extremely fast bouncing. With an Uno I have sensed bounces as short as 12 microseconds, many of those. And that is as small as my code would see, I'm sure that many smaller bounces occurred.
You have the link to Nick Gammon's blog with his scope picture. You see one way to debounce is putting a capacitor across the switch?
thanks for the reply,
i have read it, but i skip it, i don't understand about transistor, resistor, capacitor, etc, how to count their formula, why using it and what happens if i not using it (i bet it will fry up the arduino).
because of my current machinerry project is just edit the count ratio using the limit switch,
i don't think i really need to study electricity furthermore,
thank again for reply, and sorry if i rude
And you know that bounce is part of every button and you debounce in software because of that.
You need to stick to the cookbook, what schematics you can get. No play, just assemble and you get what you need as long as the cookbook is right and you follow carefully you are good.
You will be back next time there is a need to make some little minder machine.
but, is that right? "#define ISPRESSED LOW", i assume if is pressed is high,
Yes it is right, you assume wrong if you want to use the internal pull up resistors.
why using it and what happens if i not using it (i bet it will fry up the arduino).
Not connecting things will not fry the Arduino, but if you don't connect an input to anything it will float. That means it can return a high or a low at random for no apparent reason.
A common mistake beginners make, is thinking if nothing is connected to an input pin it will read as a low. In fact it will read as either a high or low. The resistor to ground or to +5V makes the un-driven state of an input return a steady value. Your limit switch then can be driven into the opposite state by switching the opposite voltage to that the pull resistor is being connected to.