|
as currently written it will turn on and immediately off as digitalRead(button) is probably not edge triggered but instead level-triggered. both if-statements execute every time, probably.
|
# ? Dec 1, 2014 00:32 |
|
|
# ? Apr 23, 2024 22:30 |
|
Nelson MandEULA posted:yospos please help. why is my code not working?
|
# ? Dec 1, 2014 00:33 |
|
actually that still probably won't work
|
# ? Dec 1, 2014 00:34 |
|
code:
|
# ? Dec 1, 2014 00:35 |
|
also this may still suffer from poor debouncing
|
# ? Dec 1, 2014 00:37 |
|
try thiscode:
Sagebrush fucked around with this message at 00:46 on Dec 1, 2014 |
# ? Dec 1, 2014 00:41 |
|
bloody likes using char instead of boolean and 0|1 instead of true|false because he's a big lameoid sperg who doesn't wanna follow the arduino guidelines
|
# ? Dec 1, 2014 00:47 |
|
thanks guys - i really appreciate the help! must sleep now but i will try these tomorrow. as you can tell, i am not very good at this whole programming thing.
|
# ? Dec 1, 2014 00:52 |
|
Sagebrush posted:big lameoid sperg who doesn't wanna follow the arduino guidelines mods
|
# ? Dec 1, 2014 00:53 |
|
also rather than using const int just use #defines, there are technical reasons you'd go one way or another but on arduino if you are just wanting to give a convenient name to a number that won't change the #define saves you memory so instead of const int BUTTON = 2; it's #define BUTTON 2 (note the lack of a semicolon, preprocessor directives like this are just a find/replace so if you put the semicolon in then a line down below like digitalRead(BUTTON); becomes digitalRead(2;); and failes for obvious reasons
|
# ? Dec 1, 2014 00:55 |
|
Sagebrush posted:bloody likes using char instead of boolean and 0|1 instead of true|false because he's a real life dev
|
# ? Dec 1, 2014 00:58 |
|
reminds me I need to rewrite my C program to not be a spaghetti mess of interrupts and if statements
|
# ? Dec 1, 2014 00:59 |
|
Sagebrush posted:bloody likes using char instead of boolean and 0|1 instead of true|false because he's a big lameoid sperg who doesn't wanna follow the arduino guidelines ive never used an ardweener so i dont wanna assume
|
# ? Dec 1, 2014 01:04 |
|
|
# ? Dec 1, 2014 01:05 |
|
Luigi Thirty posted:reminds me I need to rewrite my C program to not be a spaghetti mess of interrupts and if statements lol if your C program is anything but this
|
# ? Dec 1, 2014 01:05 |
|
Bloody posted:ive never used an ardweener so i dont wanna assume tbh i don't even remember what boolean maps to, i think it's just a uint8 so char is functionally equivalent
|
# ? Dec 1, 2014 01:08 |
|
Bloody posted:lol if your C program is anything but this I'm writing a programmable display not a cancer laser gun
|
# ? Dec 1, 2014 01:10 |
|
Sagebrush posted:try this that will flip off again if the user holds down the button for more than 200 ms something like this would probably be good enough: code:
|
# ? Dec 1, 2014 01:12 |
|
yea, a delay is the cheapest and dumbest debouncing you can do, but you know, the guy is still new so one step at a time
|
# ? Dec 1, 2014 01:14 |
|
I'm surpsied you didn't use a delay, sagebrush, because if I recall correctly, you consider delay to be an efficient and space saving method
|
# ? Dec 1, 2014 01:19 |
|
the cheapest and dumbest debouncing you can do is a capacitor.
|
# ? Dec 1, 2014 01:24 |
|
an extra component costs less than a line of code?
|
# ? Dec 1, 2014 01:26 |
|
the arduino language is C++, not C, so bools are a real thing. as is const, which in C++ is a typesafe equivalent of #define getting to the source of your problem, and ignoring the debouncing issues that others have already solved, you say that your button is wired up using a pull-down resistor, yet in your code you are checking to see if the pin is LOW. problem is, the pull-down resistor means the pin will be LOW when the button isn't being pressed, and will be HIGH when it is pressed. another problem is that with arduinos (and any ATmega microcontrollers) that external pull-down resistor is unneeded. the ATmega328 has internal pull-up resistors on all pins capable of being used as input, so just use those unless you really really need that pin to be pulled low when unpressed instead of being pulled high. to enable the internal pull-up resistor for a pin, using the arduino library you just do C++ code:
Doc Block fucked around with this message at 01:36 on Dec 1, 2014 |
# ? Dec 1, 2014 01:28 |
|
adding a delay seems expensive because you are stalling the loop and you gotta keep getting poo poo done the cheapest debounce would be keeping track of it with a counter in the loop so you can update the actual value a few loops later if the input is stable, no?
|
# ? Dec 1, 2014 01:28 |
|
Sagebrush posted:an extra component costs less than a line of code? an extra component is simpler than a line of code
|
# ? Dec 1, 2014 01:29 |
|
actually, what you should do is forego the busy loop and just set the pin up to fire an interrupt when its pulled to the desired level, and the interrupt handler sets up a timer interrupt that goes off after 15 seconds, and the rest of the time keep the microcontroller in the lowest power mode possible that still keeps the timers and pin change sensing enabled. edit: and an extra component is way more complicated and expensive than even many extra lines of code.
|
# ? Dec 1, 2014 01:34 |
|
Doc Block posted:the arduino language is C++, not C, so bools are a real thing. as is const, which in C++ is a typesafe equivalent of #define meant to say pull-up, not pull-down. but yeah, you're right - i forgot about the internal pull-ups and the pinMode. makes things a bit simpler.
|
# ? Dec 1, 2014 01:36 |
|
Whoops, should've been INPUT_PULLUP instead of INPUT_HIGH, fixed.
|
# ? Dec 1, 2014 01:37 |
|
Also, if you're new at this and can't seem to get it to work, start even simpler. Make the button turn the LED on, and that's it. Then make it so pressing the button turns the LED on, but pressing it again turns the LED off. Then add the timer functionality. That way, you start off knowing that each piece works, and if the next thing you add makes it not work, it'll be easier to figure out why.
|
# ? Dec 1, 2014 01:39 |
|
Doc Block posted:actually, what you should do is forego the busy loop and just set the pin up to fire an interrupt when its pulled to the desired level, and the interrupt handler sets up a timer interrupt that goes off after 15 seconds, and the rest of the time keep the microcontroller in the lowest power mode possible that still keeps the timers and pin change sensing enabled. idk, having a lab tech spend a minute soldering a cap on is a lot cheaper than spending 30 seconds even considering pin debouncing! but yeah the best solution is edge-triggered interrupts for the pins and a timer to debounce
|
# ? Dec 1, 2014 01:44 |
|
except that the extra component takes up space on the PCB, affects your design, adds complexity to manufacturing, and the capacitor itself adds to the unit's cost. edit: and if I remember my low-level ATmega poo poo correctly, when going from low power mode to full power mode after a pin change interrupt, the microcontroller can't respond to another pin change interrupt for X number of milliseconds, taking care of your debouncing for you. IIRC. edit2: and if I'm wrong, you can still just immediately disable interrupts (if they aren't automatically disabled by the MCU, I don't remember) until your interrupt handler is ready to exit, which should also take care of debouncing. Doc Block fucked around with this message at 02:00 on Dec 1, 2014 |
# ? Dec 1, 2014 01:49 |
|
also the capacitor is still less flexible and reliable than suffix's code or an interrupt solution
|
# ? Dec 1, 2014 01:54 |
|
shut up nerds
|
# ? Dec 1, 2014 01:56 |
|
Doc Block posted:except that the extra component takes up space on the PCB, affects your design, adds complexity to manufacturing, and the capacitor itself adds to the unit's cost. doc block: mad about caps
|
# ? Dec 1, 2014 01:57 |
|
Bloody posted:doc block: mad about caps also mad about subpixel antialiasing Doc Block fucked around with this message at 02:01 on Dec 1, 2014 |
# ? Dec 1, 2014 01:58 |
|
graph posted:shut up nerds chill out james
|
# ? Dec 1, 2014 01:58 |
|
Apple Macintosh users: hold down Ctrl on the keyboard and then scroll up with your mouse or tragic mackpad. wheeee! Now you can study the subpixel antialiasing of fonts! Yay!
|
# ? Dec 1, 2014 02:02 |
|
who was it that said the thing about wearing your big boy pants and pissing in them too that has made me chuckle all week
|
# ? Dec 2, 2014 03:38 |
|
|
# ? Dec 2, 2014 06:29 |
|
|
# ? Apr 23, 2024 22:30 |
|
there's either a good dad or husband behind all this. or possibly not one at all
|
# ? Dec 2, 2014 06:30 |