A Sketch that Reports Time Using a Button
A Software Development Workflow for the Arduino Uno
By John Frederick Chionglo
jfchionglo@yahoo.ca • Brgy. Sta. Teresita, Quezon City Philippines • 1114
System Requirements
Attiq_Lone (2019) wants a sketch for the Arduino Uno to start counting after pressing a button and stop counting after pressing the button again
and so on, and to report on the time periodically via the serial monitor between the two button presses.
A button click is a button press followed by a button release. The system considered here will wait for a button release after a button is pressed
before recognizing the next button press. In other words the system will wait for a button click before recognizing the next button press. Thus at
the beginning of a button click (when a button press has been recognized and a button release has not occurred yet) the report on time begins.
At the end of the click (when the button release has been recognized) the system waits for the beginning of the next button click (when the next
button press has been recognized but the next button release has not occurred yet). At the start of the next button click the report on time ends.
And the system repeats again.
Summary of Software Development Workflow
If you find it challenging to write a sketch directly from requirements you remember and/or from requirements described in an essay then you
should consider creating a Petri Net diagram first and then write your sketch based on the Petri Net diagram. The following describes how I
created a sketch that reports time in the serial monitor of the Arduino IDE by:
1. Developing software prototypes
a. Using Petri Net notations
i. Creating a Petri Net diagram and related annotations of the system and its control logic.
ii. Generating a token game of the Petri Net as a PDF form application (Figure 1) (see Note 2).
iii. Testing the prototype of the control logic by playing the token game.
b. Using symbols for circuits, a pushbutton and a high level view of the Arduino Uno R3
i. Creating a circuit diagram of the system.
ii. Generating a simulation of the system based on the token game and circuit diagram (Figure 2) (see Note 3).
iii. Testing the simulation by pushing and releasing the pushbutton.
2. Developing the software
a. Adding snippets of sketches as annotations to the Petri Net diagram (Figure 3).
b. Generating a complete sketch of the control logic for the Arduino Uno R3 based on the Petri Net diagram and its annotations
(Figure 4).
3. Prototyping
a. Setting up a breadboard with a pushbutton and an Arduino Uno R3 connected by jumper wires.
b. Copying the sketch to the Arduino IDE, opening the serial monitor and uploading the sketch to the Arduino Uno R3.
c. Testing the prototype.
Anatomy of a Petri Net Model
Transition T2 simulates a button press and T1 simulates a button release. The control system keeps track of the button clicks. Transition T4
recognizes the “first button press” and the mark in place P4 (𝑚𝑚4 = 1) is the recognition of the “first button press”. The mark in P3 (𝑚𝑚3 = 1) is
the recognition of the “first button release”, the mark in P2 (𝑚𝑚2 = 1) is the recognition of the “second button press” and the mark in P0 (𝑚𝑚0 =
1) is the recognition of the “second button release”. The mark in P5 (𝑚𝑚5 ) represents the number of “ticks” in the clock. If 𝑚𝑚5 = 0 then the time
is reported (𝑚𝑚6 ) and resets P5 (𝑚𝑚5 = 8). For the token game in Figure 1 the “loop” executes about every 500 milliseconds (fast) or about every
1500 milliseconds (slow). Thus if the token games executes in fast mode then time is reported about every 4 seconds (8 x 500ms). The report
time format is: <year>:<month>:<date>:<hour>:<minute>:<second>. For example 2019:4:2:2:19:31. The month ranges from 0 (Jan.) – 11 (Dec.).
P2
Transitions
T3
Computer-controlled
User-controlled
P5
P0
T5
T8
Serial Monitor
3
USB
T0
Arduino Uno R3
(A High Level View)
P3
Release Button
Report on Time
+5V
T1
T6
P4
P1
T2
𝑘𝑘
T4
P6
T7
Press Button
Figure 1 Token Game of the System
Figure 2 Simulation of the System
Sketching
The token game and simulation show that the control logic performs according to the requirements. Annotations for the Arduino Uno have been
added to the Petri Net diagram (Figure 3) and a sketch (Figure 4) based on the Petri Net diagram and annotations have been manually derived.
The sketch was verified using the Arduino IDE and uploaded to an Arduino Uno with a breadboard prototype of the system. After several tests
the initial value for 𝑚𝑚5 = 409600 was chosen (time should be reported in the serial monitor every 3 to 4 seconds).
P2
Transitions
T3
Computer-controlled
User-controlled
P5
P0
T5
T0
T8
P3
Release Button
T1
digitalRead
T2
digitalRead
Serial
Pin 3
T6
T4
Press Button
Figure 3 Petri Net Diagram with Arduino Uno Annotations
May 4, 2019
P4
P1
𝑘𝑘
P6
T7
Serial.println
#define BUTTON 3
#define NK 409600
byte m_0, m_2, m_3, m_4;
int m_1, i;
byte s_t_0, s_t_1, s_t_2, s_t_3, s_t_4, s_t_5, s_t_6, s_t_7, s_t_8;
unsigned long m_5;
void setup() {
pinMode(BUTTON, INPUT);
m_0 = false;
m_1 = LOW;
m_2 = false;
m_3 = false;
m_4 = false;
m_5 = 0;
s_t_0
= false;Sketch
Figure
4 Example
Page 1 of 2
Recap
If you are having problems writing a sketch directly then it may be worthwhile to draw a Petri Net diagram of your system and add the logic of
your system as annotations to the diagram. Then write the sketch based on the Petri Net diagram and its annotations. It is the method I used to
write the sketch and the method I would use for all of my applications (such as the PDF form applications in Figure 1 and Figure 2).
References
Attiq_Lone (2019). Start and Stop Timer Using Push Button. Arduino Community Forums [Using Arduino-Programming Questions].
<https://forum.arduino.cc/index.php?topic=612456.msg4151361#msg4151361> (Apr. 29, 2019).
Chionglo, J. F. (2018). Create Hyper Form – A Workflow in Progress Update on the Net Programming Research.
<https://www.academia.edu/37697498/Create_Hyper_Form_A_Workflow_in_Progress_Update_on_the_Net_Programming_Research>.
Chionglo, J. F. (2017). Net Programming: A Research Proposal for Developing PDF Form Applications with PowerPoint and Acrobat.
<https://www.academia.edu/33374809/Net_Programming_A_Research_Proposal_For_Developing_PDF_Form_Applications_with_Powe
rPoint_and_Acrobat>.
Chionglo, J. F. (2014). Net Elements and Annotations for Computer Programming: Computations and Interactions in PDF. <
https://www.academia.edu/26906314/Net_Elements_and_Annotations_for_Computer_Programming_Computations_and_Interactions
_in_PDF>.
Banzi, M. (2009). Really Getting Started with Arduino. Getting Started with Arduino. Sebastopol, CA: Make:Books. 29 – 52.
Postscript
Software Development/Authoring Tools
PowerPoint 2013, Word 2013, Acrobat Pro XI, Notepad.
Notes
1. Please use a PDF reader (such as Adobe Acrobat) that supports JavaScript and the Acrobat/JavaScript API for Figure 1 and Figure 2.
2. It is possible to systematically create a JavaScript program and related form fields for a token game based on a Petri Net diagram and
related annotations (Chionglo, 2014). However the token game in Figure 1 was created semi-automatically using GenerateStdPTNet and
createHyperForm, work-in-progress utilities for generating automatically a Place/Transition token game based on a Petri Net diagram on
a Powerpoint slide (Chionglo, 2018; 2017).
3. Figure 3 was treated as a high-level views of Figure 2. Thus is uses the same constructor as Figure 2 with specialized methods and
properties overriding the constructor.
About the Author
John Frederick Chionglo develops JavaScript programs that use the Acrobat/JavaScript API for PDF form applications. The programs may be used
at various levels or contexts such as field, page, document, folder, action wizard or batch and console. The types of PDF form applications he
creates include forms for data entry and evaluation; and forms with dynamic and interactive diagrams for learning, reporting, software
prototyping, simulation and analysis, visualization and presentation. He also develops VBA scripts and JScript.NET programs to facilitate the
application of Net Elements and Annotations and to facilitate the development of PDF form applications via Adobe Acrobat’s inter-application
communication (IAC) API; the scripts and programs may be used with applications such as Word and PowerPoint.
He conducts research and development in the application of Petri Nets (Net Elements and Annotations in particular) to computer programming.
He is a member of several social networking sites such as Academia.edu, Researchgate.net, LinkedIn.com and Slideshare.net. To find more of his
works, search the Internet for PDF documents or images using the keywords “Acrobat”, “JavaScript” and (“Petri Nets” or “Petri Net”).
May 4, 2019
Page 2 of 2