PROGRAMMING
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
65
66
//Void Loop - repeats forever
void
loop
()
{
//DIM DISPLAY USING POTENTIOMETER
potRead =
analogRead
(Pot);
potDimVal =
map
(potRead,0,1023,255,0);
analogWrite
(OE,potDimVal);
// Set brightness
//MODE SWITCHING
swState =
digitalRead
(sw1);
//read the pushbutton input pin
if
(swState != lastswState)
// compare swState to its previous state, if it changed...
{
if
(swState == HIGH)
// ...and the current state is HIGH then the button was pressed
{
if
(mode < 4)
// if mode is less than the number of modes
{
mode++;
// add 1 to mode, thus changing to the next mode
}
else
// if mode is not less than the number of modes...
{
mode = 1;
// ...set mode to 0 (the first mode)
}
}
delay
(5);
// Delay a little bit to avoid bouncing
}
lastswState = swState;
// save the current state as the last state
PWM value for the shift register’s OE pin (
line
43
). Here we use the function
analogWrite
,
which requires the pin, followed by a value
(from 0-255). Our pin is labeled OE and
the value has been saved in the potDimVal
variable.
Mode Switching
To display more information on the Digitiser’s
display than just the time, we will use one of
the switches to act as a mode select.
When the switch is pressed, the number
stored in the variable “mode” will change.
Later in the code we will use this number to
determine what is displayed.
If we told the microcontroller to do some-
thing every time the switch read HIGH (when
it is pressed down), that something would
happen every time the code looped. As the
code loops several hundred times per sec-
ond, our code would switch modes at that
speed. Obviously not ideal.
For our code to recognise that the switch has
been pressed once, regardless of whether it
is still pressed, we need to compare its state,
with the state it was in the last time void loop
lopped. If there is no change, then nothing
will happen.
We don’t want the mode to change when
we let go of the switch either, so we should
only change the mode when the state has
changed and reads HIGH. Both things need
to be true for the mode to change.
Let’s look at the code. On line
47
we simply
save the current state of our switch’s input
pin to the swState variable.
We then use an
if
statement to compare the
current state swState to the last state saved
to lastswState. Obviously we haven’t got to
the part of the code that saves the last state,
but bear in mind that once you’ve switched
the device on, it’s gone through this code
lots and lots of times. The condition of our
if statement is that these two values are not
the same. We use the Not Equal To opera-
tor (!=). So, if they are not equal, the code
contained within the subsequent { } will be
executed.
As we have another condition to check,
we need to use another
if
statement within
the last (
line 51
). This
if
statement needs to
make sure the current state is high. To check
if something is equal to something else we