summaryrefslogtreecommitdiff
path: root/user-code/push/push-julia.tex
blob: 49783993327ac2f18ff3fc8c3b4b64cbf021d187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
\section{Reading pushbutton status from Julia}
\subsection{Reading the pushbutton status} 
In this section, we discuss how to carry out the experiments of the
previous section from Julia.  We will list the same two experiments,
in the same order.  The Shield has to be attached to the \arduino\ board
before doing these experiments and the \arduino\ needs to be connected to the computer 
with a USB cable, as shown in \figref{arduino}.
The reader should go through the instructions given in \secref{sec:julia-start} before getting started.

\begin{enumerate}
\item In the first experiment, we will read the pushbutton status. 
The code for this experiment is given in \juliaref{julia:push-100}. 
As explained earlier in \secref{sec:light-julia}, we begin with importing the SerialPorts 
\cite{julia-serial-ports} package and the module ArduinoTools followed by setting up the serial port.
Then, we read the input coming from digital pin 12 using the following
command: 
\lstinputlisting[firstline=7,lastline=7]
  {\LocPushjuliacode/push-button-status.jl}
  Note that the one leg of the pushbutton on the Shield is connected to digital
pin 12 of Arduino Uno as given in \figref{fig:pushbuttonconn}. The read value is displayed (or
printed) by the following lines:
\lstinputlisting[firstline=6,lastline=8]
  {\LocPushjuliacode/push-button-status.jl} where {\tt val} contains the pushbutton value acquired by the previous command.
  When the pushbutton is not pressed, {\tt val} will be ``0''. On the other hand,
  when the pushbutton is pressed, {\tt val} will be ``1''. To encourage the user to have a good hands-on, we run these commands in a
{\tt for} loop for 20 iterations. The readers are encouraged to change the number
of iterations as per their requirements. While running this experiment, the readers
must press and release the pushbutton and observe the values being printed
on the Command Prompt (on Windows) or Terminal (on Linux), as the case
maybe.

\item This experiment is an extension of the previous
  experiment. Here, we control the state of an LED as per the status
  of the pushbutton. In other words, digital output to an LED is
  decided by the digital input received from the pushbutton. The code
  for this experiment is given in \juliaref{julia:push-200}. After reading
  the pushbutton status, we turn the LED on if the pushbutton is
  pressed, otherwise we turn it off. The following lines, 
  \lstinputlisting[firstline=10,lastline=13]
  {\LocPushjuliacode/led-push-button.jl} perform the condition check
  and corresponding LED state control operation. While running this experiment, the readers must press and release the push-
  button. Accordingly, they can observe whether the LED glows when the push-
  button is pressed.
\end{enumerate}

\subsection{Julia Code}
\label{sec:push-julia-code}
\addtocontents{juliad}{\protect\addvspace{\codclr}}

\begin{juliacode}
\jcaption{Read the status of the pushbutton and display it on Command Prompt or the Terminal.}{Read the status of the pushbutton and display it on Command Prompt or the Terminal.  Available at
  \LocPushjuliabrief{push-button-status.jl}.}
\label{julia:push-100}
\lstinputlisting{\LocPushjuliacode/push-button-status.jl}
\end{juliacode}

\begin{juliacode}
\jcaption{Turning the LED on or off depending on the pushbutton}
  {Turning the LED on or off depending on the pushbutton.  Available at
  \LocPushjuliabrief{led-push-button.jl}.}
\label{julia:push-200}
\lstinputlisting{\LocPushjuliacode/led-push-button.jl}
\end{juliacode}