Display time with push button interrupt Arduino - arduino

I need to write a code that contains an interval loop but contains a push button interrupt that displays RTC values when activated. I have found a way to individually do each task i.e change pushbutton state, loop in intervals, display RTC value but I cannot seem to combine them and create a working. If someone can provide links or an explanation on how to accomplish this I would be so grateful.

If you do not use delay(interval); in your main loop, you can run as many tasks in parallel as you want. Understand the BlinkWithoutDelay sample, and try to extend it to two leds blinking independently. Or read a button while blinking.
And push button and interrupt does not go together well, BTW.
You even might add a small delay(2); to slow down polling the button pin.
This is usually fine for the other parallel running tasks and implements a very simple debounce mechanism.

Related

Is there a way to rescue my processor from a reset deadlock

I have a board with an XMC1400 MCU on it. It is a custom board with LEDs and buttons and so on.
So I accidentally add a '''XMC_SCU_RESET_AssertMasterReset()''' line at the beginning of the code... This function cause xmc to reset but it doesn't just reset the program counter, it also clears everything and ends the debug connection. And since it happens so quickly I cannot reconnect before it resets again. For the one who doesn't know, when you load a program to the xmc, it gets written to the flash, and once it's loaded it will always run with power up.
I know in some boards there are some pins used to clear to board when shorted but there is no such a thing in this one.
There are however several bootstrap loaders(can and uart). But they are disabled by a flash parameter called BMI. I don't have much hope but is there a way to rescue this processor?
Thanks!

Which is a better option for multitasking in arduino - millis( ) or Scheduler Library?

I have an application wherein I want to flash( trigger for a sec) a solenoid every 10 seconds and at the same time receive a serial input to rotate a servo motor.
The delay() creates conflicts so I have gone through the millis() function which is easy to understand.But in the arduino website they have something called the Scheduler library which looks pretty damn easy( haven't tried it though).
So which is better and efficient option to consider, is it millis() or Scheduler?
Thank you,
The Scheduler library uses millis() as well to calculate the delay between tasks.
To link a function to the sheduler, it needs to have a void f(void) prototype.
So to be able to add a function that returns something or has parameters, you need to wrap it in another function of void f(void) prototype.
IMHO, a sheduler library is usefull to organize your code when you have multiple tasks (This library has a maximum of 10 tasks, but you can change it).
In your case, you only have two tasks. It may be better to just use your own little sheduler using millis().
If you want to stay with millis(), then
Simple Multi-tasking in Arduino
will be of help. It covers:- adding a loopTimer to see how slow your loop/tasks are running, removing delays from your code and third party libraries, reading serial input without blocking and sending serial prints without blocking and giving important tasks more time.
Finally it transfers the code unchanged to the ESP32 to add remote control.
The basic code is
void loop() {
callTask_1(); // do something
callTask_2(); // do something else
callTask_1(); // check the first task again as it needs to be more responsive than the others.
callTask_3(); // do something else
}
The trick is that each callTask..() method must return quickly so that the other tasks in the loop get called promptly and often. The rest of the instructable covers how to keep your tasks running quickly and not holding everything up, using a temperature controlled, stepper motor driven damper with a user interface as a concrete example.

Listen keyboard input and update time on LCD at the same time in Arduino

I'm running into a problem on Arduino.
I want my Arduino listen to input from a piece of 12 key's keypad, this is easy, there is already keypad library avaliable.
I want to display time on a 16X2 lcd screen that I read from a RTC module, update per second. This is also easy, implemented already.
BUT, when I try to combine these two tasks, to make arduino listen keypad and keep updating the lcd, there is trouble: I'm using a delay(1000) to tell arduino loop per second, which works for updating time on lcd, but it somehow blocks the keypad listening.
Is there any solutions for this thing? Like multithreading or other clever workaround?
How about using millis() to control the interval of LCD updating like this?
unsigned long lastRanTime;
void loop() {
if (millis() - lastRanTime >= 1000) {
// update the lcd
lastRanTime = millis();
}
// listen for the keypad
}
as Pawel suggests the best practice is tp use interrupts on the key inputs. You can google about the concept of interrupts, there are plenty.
If you are on a project that needs a menu structure where you can access data and trigger functions AND just want to get things done by having a framework to do so, I recommend you using the LCDMenuLib:
https://github.com/Jomelo/LCDMenuLib
it's from a german guy. The project description on arduino.cc forum is mostly in german, but the thorough code comments and examples are in english. This framework will take care of displaying the menu structure and handling the key inputs for you in a non-blocking way (with interrupts). You can pretty much concentrate on what functions to trigger or what to display in the menu leafs.

Find time difference between two pulses using PIC16F628

I want to find time difference between two pulses using PIC16F628.
I am using a 4MHz external oscillator, MikroC compiler.
As a simple example let's assume there is a push button. When we press it, it sends a high signal to a pin. We press this button twice with some delay in between, I want to find the time difference between these two button presses.
Thank you.
As mentioned in the comments, the simplest way to do this is to use a timer/counter combo. I found this quick tutorial on how to do this specifically for PIC: http://www.mikroe.com/chapters/view/17/chapter-4-examples/#c4v5.
Have a look at 4.5 and 4.6, they give you exactly the information you'll need to get the count of timer intervals between pulses. The basic technique is to start a timer, associate an interrupt handler (Read: function) with the timer, and then increment a counter everytime the interrupt handler is called. Next time you see the pulse, read what the counter value is.
After that, all you need to know is the timebase you've set the counter to (which will be some integer subdivision of your oscillator rate, and is selectable in code usually) and you can convert # of timer intervals to time in seconds/millis/nanos.

Need an Arduino Sketch that uses digital write for a certian number of seconds

I need a simple way to run a program using digital write for a certain number of seconds.
I am driving two DC Motors. I already have my setup complete, and have driven the motors using pause() and digitalWrite(). I will be making time measurements in milliseconds
Adjustable runtime, and would preferable have non-blocking code.
You could use a timer-driven interrupt triggering code execution which will handle the output (decrementing required time value and eventually switching off the output) or use threads.
I would suggest using threads.
Your requirement is similar to a "blinking diodes" case I described in a different thread
If you replace defines setting time intervals and use variables instead you could use this code to drive outputs or simplify the whole code by using only one thread working the same way aforementioned timer interrupt would work.
If you would like to try timer interrupt-driven approach this post gives a good overview and examples (but you have to change OCR1A to about 16 to get overflow each 1ms) .

Resources