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
|
\section{Reading potentiometer from Python}
\subsection{Reading the potentiometer}
In this section, we will use a Python script to read the potentiometer
values. 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:python-start} before getting started.
Based on the acquired potentiometer values, we will change
the state of the RGB LED. As explained earlier, the potentiometer
values range from 0 to 1023. We will divide this entire range into 3
bands, 0-319, 320-900, and 901-1023. For each read value, we use an
{\tt if elif} statement and correspondingly turn on either the Red,
Green or Blue LED. The code for this experiment is given in
\pyref{py:pot-100}. As explained earlier in \secref{sec:light-py}, we begin with
importing necessary modules followed by setting up the serial port.
Then, we read the analog input at pin 2 using,
\lstinputlisting[firstline=28,lastline=28]
{\LocPotpycode/pot-threshold.py} where the first
argument is for
%\redcolor {the kit number }
the kit number and the second argument corresponds to the analog pin to be read.
Next, we compare the read values with the set range, and then turn on and off the corresponding LED. For example,
\lstinputlisting[firstline=31,lastline=34]
{\LocPotpycode/pot-threshold.py} where {\tt cmd\_digital\_out}
is used to set the pin 11 high (1) or low (0).
We used {\tt sleep(1)} to retain the LED in the on state for one second.
A similar check is done the other two bands.
While running this experiment,
the readers must rotate the knob of the potentiometer and observe
the change in the color of the RGB LED.
\subsection{Python Code}
\label{sec:pot-python-code}
\addtocontents{pyd}{\protect\addvspace{\codclr}}
\begin{pycode}
\pcaption{Turning on LEDs depending on the potentiometer
threshold}{Turning on LEDs depending on the potentiometer
threshold. Available at
\LocPotpybrief{pot-threshold.py}.}
\label{py:pot-100}
\lstinputlisting{\LocPotpycode/pot-threshold.py}
\end{pycode}
|