1
00:00:00,720 --> 00:00:07,630
Like in this example, I'm going to show you how to read the state of a button using a hardware timer.

2
00:00:08,010 --> 00:00:14,310
Just remind you that in the previous two variations of the same exercise, you learned how to read the

3
00:00:14,310 --> 00:00:18,870
state of a button using an infinite loop, like in this example right here.

4
00:00:19,230 --> 00:00:23,910
And also how to use a hardware interact as in this example here.

5
00:00:24,600 --> 00:00:32,910
The example in which I'm using the hardware timer is really a variation of the hardware in the example.

6
00:00:33,240 --> 00:00:39,630
And you ought to know how to use the hardware timer from an earlier example with reality, which you

7
00:00:39,630 --> 00:00:46,920
can see here in this script as we have defined the time down here and lines three, four and thirty

8
00:00:46,920 --> 00:00:47,370
five.

9
00:00:47,670 --> 00:00:54,360
So in this lecture, basically taking these scripts and putting them together and creating a hybrid

10
00:00:54,360 --> 00:00:55,510
that looks like this.

11
00:00:56,070 --> 00:01:03,480
So what's happening here is that I've got a timer which has a period of 50 milliseconds and this is

12
00:01:03,480 --> 00:01:04,530
a periodic timer.

13
00:01:04,530 --> 00:01:11,430
So it fires every 50 milliseconds and each time it fires, it will call the interruption of this routine

14
00:01:11,640 --> 00:01:16,710
called button on this compressed and this call ISO, which is right here.

15
00:01:17,130 --> 00:01:20,400
What it does is to check the state of the button.

16
00:01:20,400 --> 00:01:24,540
And if it is pressed, then it will update these variables here.

17
00:01:24,930 --> 00:01:33,330
At the same time, we've got the while loop infinite loop constantly checking for the value stored inside

18
00:01:33,330 --> 00:01:35,700
the global pattern and this compressed variable.

19
00:01:35,700 --> 00:01:41,520
And if it is true, then it will turn on the LCD and wait here for a second.

20
00:01:42,030 --> 00:01:48,270
It's a very it's a very simple way of going about reading the state of a button, taking advantage.

21
00:01:48,270 --> 00:01:55,320
Had we interrupt in terms of efficiency, I'm not quite sure which of these two is more efficient is

22
00:01:55,320 --> 00:01:56,400
hard really to say.

23
00:01:56,580 --> 00:02:02,490
I would say that the hardware interrupted is perhaps more efficient from the point of view that you

24
00:02:02,490 --> 00:02:12,360
don't occupy a hardware timer to keep firing every 15 milliseconds and calling the interrupt service

25
00:02:12,510 --> 00:02:13,170
routine.

26
00:02:13,510 --> 00:02:20,490
In this example here with the hardware interrupt, the ISI is only called when the button is actually

27
00:02:20,490 --> 00:02:21,000
pressed.

28
00:02:21,450 --> 00:02:29,970
But in this example here, I thought that this our way of using the hardware timer is interesting and

29
00:02:29,970 --> 00:02:32,400
worthwhile dedicating a lecture for it.

30
00:02:33,240 --> 00:02:34,980
Let's have a quick look at the beginning.

31
00:02:35,400 --> 00:02:36,840
There's nothing new really here.

32
00:02:36,840 --> 00:02:43,920
We import the appropriate functions from the machine in time modules, define the ality, define the

33
00:02:43,920 --> 00:02:46,020
button with its pull up resistor.

34
00:02:46,380 --> 00:02:54,210
We've got our button pressed variable, the interrupt service routine with a global keyword so that

35
00:02:54,210 --> 00:03:00,300
the button pressed and button event variables are global and then we can make changes to those variables

36
00:03:00,300 --> 00:03:01,800
from inside the ISO.

37
00:03:02,550 --> 00:03:10,080
Then when the ISI is called, we check for the value of the button and if it is down there, meaning

38
00:03:10,080 --> 00:03:16,140
that button is pressed so the value returned by value is going to be false or zero.

39
00:03:16,500 --> 00:03:25,650
Remember that of got this wire here which grounds the button value when it's pressed and therefore GPL

40
00:03:26,070 --> 00:03:32,390
four will read a low value or false value in the python speak.

41
00:03:32,790 --> 00:03:34,080
So Button is pressed.

42
00:03:34,080 --> 00:03:36,330
In this case, you apply the variables.

43
00:03:36,330 --> 00:03:43,020
We also store the event object in the event variable.

44
00:03:43,380 --> 00:03:45,750
Otherwise button is false.

45
00:03:46,560 --> 00:03:55,440
And notice that I have declared the button, pressed the function before I create the end, initialize

46
00:03:55,440 --> 00:04:01,590
the hardware timer because I need to pass it to the timer via this callback parameter.

47
00:04:02,040 --> 00:04:08,670
And if I call the ISO before it's actually declared, you're going to get an error message by the compiler

48
00:04:08,970 --> 00:04:11,820
when you try to compile and run the script.

49
00:04:12,600 --> 00:04:19,080
After that, we've got the while true, which constantly checks for the value stored in the button on

50
00:04:19,080 --> 00:04:25,300
this compressed file and then it just turns on reality and keeps it on for one second.

51
00:04:25,320 --> 00:04:26,400
Otherwise, it will turn it off.

52
00:04:27,600 --> 00:04:28,170
All right.

53
00:04:29,280 --> 00:04:35,880
So I'm going to make a copy of this script onto the device.

54
00:04:35,920 --> 00:04:39,630
I will call it button for time.

55
00:04:39,630 --> 00:04:41,610
I thought he y.

56
00:04:43,810 --> 00:04:45,790
And let's run it.

57
00:04:50,130 --> 00:04:50,600
OK.

58
00:04:53,900 --> 00:05:01,220
And it stays on for one second and you can see the event with it, we're passing and printing out here,

59
00:05:01,580 --> 00:05:07,520
starting the button and it's called event variable, which is created up here.

60
00:05:07,580 --> 00:05:14,960
And it's really its origins are the event parameter that is passed by the time I had with him up to

61
00:05:14,960 --> 00:05:16,090
the ISO.

62
00:05:16,490 --> 00:05:19,100
And when you print out, this is what you get.

63
00:05:20,120 --> 00:05:25,370
Okay, so that's about it with a button, you know, enough now to be able to use buttons in your sketches.

64
00:05:25,910 --> 00:05:31,820
Now there's one more lecture in this section, which is the next one in which I'll show you how to use

65
00:05:31,820 --> 00:05:36,320
a particular mirror and make use of the analog to digital converter.
