Pages: [1]   Go Down
Author Topic: serialEvent with yes/no dialogue?  (Read 480 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

serialEvent works perfectly as advertised. For the moment I use it with the Arduino terminal as I havn't started to link that to Processing.

Many times, you'd like your sketch to return an acopalyptic message requiring confirmation of a command. For example, if you type:

apocalypse <send>

Arduino could reply:

Delete Earth, Solar system, Galaxy, Universe; or Wait another day (E/S/G/U/W)?

It should be easy enough to program (presumably using static variables) but, since this is a commonly-required action, does anyone have a library that does the job in the professional manner?
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It should be easy enough to program (presumably using static variables)

With a finite state machine, sure it wouldn't be difficult at all to program.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 651
Posts: 50842
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Keep in mind that serialEvent is only called when there is serial data to be processed, and the end of loop is reached.

It does not get called so the Arduino can send a message, nor is there anything about it that causes a wait for a reply.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS : then what I'm asking for would be an additional library.

For those of us new to this sort of thing, the logic behind serialEvent is hard to follow. As it's built in to Arduino, the user doesn't see whether or not 'void serialEvent()' is an interrupt handler that's called any time data arrives. Is the the following part of the comment before void serialEvent() in the example a little bit misleading?

'This routine is run between each time loop() runs,...

If it is an interrupt handler, should the reader just be advised to get back into loop() as frequently as necessary?  In my case they are typed in the terminal and can wait a few seconds for action.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It isn't interrupt driven. It is called after each iteration of loop(), in main.cpp, which the Arduino IDE adds to your sketch:

Code:
#include <Arduino.h>

int main(void)
{
init();

#if defined(USBCON)
USBDevice.attach();
#endif

setup();
   
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
       
return 0;
}
Logged

Pages: [1]   Go Up
Jump to: