Mapping a Key on the Keyboard to Exit Loop in Arduino - arduino

I want to create a function on the Arduino and put it in all the loops in my program such that whenever during use, the user can press the "Esc" key on the keyboard to cancel what it is currently doing. A serial monitor is being used with my current code.
My entire code is very long which is why I don't have it posted, but if you can answer the situation i described above, I will be able to implement it. You can imagine a simple loop running forever that will exit anytime the "Esc" key is pressed.

Related

Simplest code to handle any key press - 8051

I'm trying to figure out the best approach to my problem.
I have an AT89S52 microcontroller with 3 buttons connected to 3 separate GPIO pins so that the microcontroller can perform different functions based on those buttons.
I'm trying to write code that waits for the user to press any of the three keys. By key press, I mean where the system detects any one key is fully pressed down and then fully released.
The code I presented below might work if I added a schmitt trigger in my hardware but I don't want to redo my circuit board (again).
Without adding interrupts, is there a way I can modify the code shown with just a few lines to reliably detect a user key press?
I ask because keys experience a phenomenon called "bouncing" where as soon as someone presses a key, it actually jitters at high speed and the microcontroller will see it as key being pressed and released multiple times. I don't want that to happen if the user legitimately pressed the key only once.
;KEY1I, KEY2I and KEY3I = GPIO pins connected to the keys
;Pin value is low when key is held down
w4key:
;begin key scan
jnb KEY1I,w4keyend
jnb KEY2I,w4keyend
jnb KEY3I,w4keyend
;here, nothing is pressed so scan again
sjmp w4key
w4keyend:
;key pressed. Hope for release
jnb KEY1I,w4key
jnb KEY2I,w4key
jnb KEY3I,w4key
;here, key is released so return.
ret
mainline:
;do something
acall w4key
;do another thing
...
You can use a timer (AT89S52 has several timers, you can use one of them if there are not otherwise used in your project) and a synchronous state machine. The state machine has 4 states for every key and definite transitions. I found this link that explains the concept quite thorough. Although the provided example code in this link is in C, you can easily "translate" it to your assembly code. If you need help with this, just leave a comment.
https://www.eeweb.com/profile/tommyg/articles/debouncing-push-buttons-using-a-state-machine-approach

Display time with push button interrupt 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.

How to emit a delayed signal in PyQt GUI?

In my PyQt GUI app, there are a lot of spinBoxes that trigger a lengthy setup routine when their values are changed.
My problem is this:
When typing in (large) numbers the spinBox.valueChanged() signal is emitted every time I input a single digit. That leads to the setup function being called many more times than necessary.
Is there a way to delay the trigger until I'm done typing the number and then fire the signal only once?
How do you usually take care of that issue in your GUIs?
I found this but I think it would involve creating an extra timer for every unique function I want to call upon emitting. That doesn't seem very elegant.
For spinboxes there is the keyboardTracking property.
If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() signal while typing. It emits the signal later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key.
I believe this solves your issue.
You can also connect to the editingFinished signal, which isn't emitted until the user presses enter or the spinbox loses focus. However, you'll get this signal even if the value doesn't change, so you may want to check it against the last value to prevent having to run the lengthy routine unnecessarily.

Is there a AutoIt keypress example?

I am reading AutoIt Your Quick Guide but still not sure how to actually implemented it.
My setting, a opened notpad waiting for input.
What I want to do is to have a while loop and continue press a keystroke (e.g. keycode 20, #3 key, or multiple keystrokes) in a random period to that opened notepad.
How do I do it? I know send a keypress is using function Send ( "keys" [, flag = 0] ) But there is no number key reference in the book, and how do I tell the autoit to send keystroke to the specific program (notepad, in my example)
How I connect these all together?
Thanks for your help.
Look at ControlSend() in the help file, there is a Notepad example for you to decipher. Send() is a little unpredictable when you wish to send keystrokes to a particular window, as AutoIt simulates the keystrokes and thus if another window takes focus during the loop, then this will receive the keys.

AutoIt: Run next command after finishing the previous one

I'm currently writing a macro that performs a series of control sends and control clicks.
They must be done in the exact order.
At first I didn't have any sleep statements, so the script would just go through each command regardless whether the previous has finished or not (ie: click SUBMIT before finish sending the input string)
So I thought maybe I'll just put some sleep statements, but then I have to figure out how best to optimize it, AND I have to consider whether others' computers' speeds because a slow computer would need to have longer delays between commands. That would be impossible to optimize for everyone.
I was hoping there was a way to force each line to be run only after the previous has finished?
EDIT: To be more specific, I want the controlsend command to finish executing before I click the buttons.
Instead of ControlSend, use ControlSetText. This is immediate (like GuiEdit).
My solution: use functions from the user-defined library "GuiEdit" to directly set the value of the textbox. It appears to be immediate, thus allowing me to avoid having to wait for the keystrokes to be sent.

Resources