1
00:00:01,020 --> 00:00:01,540
Welcome back.

2
00:00:01,980 --> 00:00:06,840
In the previous lecture, you learned how to read the state of the button in the simplest possible way.

3
00:00:07,250 --> 00:00:12,280
As you can see here, we just use a while, true infinite loop.

4
00:00:12,450 --> 00:00:20,220
And in it we just take a reading of the button, fairly painful, and then act accordingly.

5
00:00:20,610 --> 00:00:24,930
The problem with this method, of course, is that you've got an infinite loop here that looks at the

6
00:00:24,930 --> 00:00:27,510
execution of the program in it.

7
00:00:27,540 --> 00:00:30,470
It's not a very efficient way to use your hardware.

8
00:00:30,990 --> 00:00:37,710
In this lecture, I want to show you an alternative which is more efficient and allows us to read the

9
00:00:37,710 --> 00:00:41,880
state of the button using a hardware interrupt.

10
00:00:42,390 --> 00:00:51,360
So in this line here, line 58, I have to find an IQ and interrupt request on button painful and have

11
00:00:51,360 --> 00:00:53,850
configured it in a way that is very efficient.

12
00:00:53,880 --> 00:01:01,090
So what I'm saying here to the trigger is the falling edge of the signal that is produced by the button.

13
00:01:01,440 --> 00:01:08,910
So when you press the button, the voltage on your full force from high to low, and that is the IQ

14
00:01:08,910 --> 00:01:13,040
falling edge which is detected in triggers this interrupt.

15
00:01:13,320 --> 00:01:18,900
And when this edge, the falling edge is detected, then they interrupt.

16
00:01:19,410 --> 00:01:25,870
Is calling this routine here a function, the button and this compressed underscore ESR interrupted

17
00:01:25,880 --> 00:01:35,100
service request or retain his ISO, which branches the program inside this function and again act accordingly.

18
00:01:35,130 --> 00:01:40,890
Will get to this in a minute in order to display the ality lighting up.

19
00:01:40,890 --> 00:01:45,750
When I press the button, I'm still using a while loop here.

20
00:01:46,020 --> 00:01:51,720
It's just the easiest way just to demonstrate the interaction between an eyesore and then a different

21
00:01:51,720 --> 00:01:52,890
part of your program.

22
00:01:53,160 --> 00:02:02,340
And I'm also demonstrating the ability of Intisar and other parts of your program to communicate using

23
00:02:02,340 --> 00:02:04,020
global variables in this case.

24
00:02:05,160 --> 00:02:07,010
Now let's have a look at some of the details.

25
00:02:07,020 --> 00:02:15,510
You can see up here in line 36, I am importing the PIN function, disable IQ and enable IQ functions

26
00:02:15,510 --> 00:02:16,920
from the machine module.

27
00:02:17,310 --> 00:02:20,550
And again, the slip in there from the time module.

28
00:02:21,150 --> 00:02:29,010
I'm starting by creating the object as usual, and then the pattern object, which is exactly what we

29
00:02:29,010 --> 00:02:30,420
did in the previous example.

30
00:02:30,570 --> 00:02:31,290
Same thing here.

31
00:02:31,310 --> 00:02:32,220
Nothing has changed.

32
00:02:33,300 --> 00:02:37,170
Then I'm declaring a couple of variables.

33
00:02:37,200 --> 00:02:39,030
This is the button pressed portable.

34
00:02:39,030 --> 00:02:46,650
It allows the IQ routine of the ISIS recorded here, retained to communicate with other parts of my

35
00:02:46,650 --> 00:02:47,300
program.

36
00:02:47,550 --> 00:02:51,280
So when a button is pressed, then I update this variable here.

37
00:02:51,300 --> 00:02:59,790
You can see it is updated here and then used here to determine whether they should be turned on or off.

38
00:03:00,430 --> 00:03:06,150
They also have integer like a numerical variable here that keeps track of how many times have pressed

39
00:03:06,150 --> 00:03:08,070
the button before it's reset.

40
00:03:09,820 --> 00:03:11,740
So you can play around with those variables as well.

41
00:03:12,220 --> 00:03:21,040
Now, inside the definition of the routine that we will be called by the IQ, you can see that it requires

42
00:03:21,040 --> 00:03:22,620
one parameter.

43
00:03:22,810 --> 00:03:30,060
And this is the object that has caused the IQ and this is passed by the IQ routine here as well.

44
00:03:30,370 --> 00:03:39,130
And I'm using this object here to print out some information about what is it that has caused the IQ

45
00:03:39,130 --> 00:03:47,140
to see I'm taking PIN and exactly what it is and passing it into the button pin global Viterbo, which

46
00:03:47,140 --> 00:03:54,490
is then printed out down here so I can get some information about the object that caused the IQ.

47
00:03:56,690 --> 00:04:01,760
A couple of other interesting things that are happening in here is, first, that I am calling the disabled

48
00:04:01,790 --> 00:04:07,600
IQ routine, which, as you can probably guess, will disable further excuse.

49
00:04:08,000 --> 00:04:15,350
So when I press the button, the first thing that happens with disability IQ, while the Isar function

50
00:04:15,350 --> 00:04:24,320
is busy, any further button presses will just be ignored until I re-enable the IQ right at the bottom

51
00:04:24,770 --> 00:04:26,420
of the isobutane.

52
00:04:26,420 --> 00:04:34,310
So when I'm done doing whatever needs to be done to deal with the existing IQ, then I will re-enable

53
00:04:34,310 --> 00:04:37,460
it so that the 32 can detect the next button.

54
00:04:37,460 --> 00:04:44,810
Press a couple of other interesting things here that are perhaps a bit unusual for you is that I'm using

55
00:04:44,820 --> 00:04:51,350
the global keyword here so you can see what's happening, of course, available, such as Button Pressed,

56
00:04:51,380 --> 00:04:55,610
which I have already declared at the header of my program.

57
00:04:55,620 --> 00:05:04,400
So you would expect that this variable would be global already just by the fact that it's been declared

58
00:05:04,400 --> 00:05:08,830
the header of the program, but in fact, it isn't.

59
00:05:08,840 --> 00:05:17,150
I won't be able to make any changes to this variable from inside this context before I use the global

60
00:05:17,150 --> 00:05:21,050
keyword to convert it into a variable that I can make changes to.

61
00:05:21,560 --> 00:05:26,290
I've got a link for more information about this here.

62
00:05:26,300 --> 00:05:31,640
If you're curious about how this works, if you don't use this keyword, then you are going to get a

63
00:05:31,640 --> 00:05:38,660
syntax error or you're going to get an interim message on line 53 when you try to make a change to the

64
00:05:38,660 --> 00:05:40,580
value stored in this variable.

65
00:05:41,060 --> 00:05:46,100
So do the same thing with the other two variables that I'm using across different sections of the program.

66
00:05:46,100 --> 00:05:51,920
So button spin and press count, you can see that all of those have been declared up there.

67
00:05:52,160 --> 00:05:56,170
And I still need to use a group of able to be able to make changes to them.

68
00:05:58,410 --> 00:06:07,350
All right, so then I just store to the button, pressed as to the object that is causing the IKEA into

69
00:06:07,350 --> 00:06:11,940
button pin and then I increment the press counter to what?

70
00:06:12,450 --> 00:06:16,670
And then I close the cube in here.

71
00:06:16,680 --> 00:06:23,100
You've got the infinite loop, which is similar to the loop function in the Adreno is just constantly

72
00:06:23,100 --> 00:06:26,100
going around executing whatever codes you have in it.

73
00:06:26,460 --> 00:06:33,300
And in this case, it's constantly checking for the value stored inside the button pressed variable.

74
00:06:33,780 --> 00:06:40,110
And when it's true, you will go inside, change it into force, turn on the ality, print out the two

75
00:06:40,110 --> 00:06:41,850
messages zero.

76
00:06:41,850 --> 00:06:46,310
The counter will reset the counter and keep the lady on for half a second.

77
00:06:46,890 --> 00:06:51,390
If it's not true, then it will turn off the reality and that's about it.

78
00:06:52,990 --> 00:07:03,600
So I am going to save a copy of this program to make replacing it with PCs and it's going to hit control.

79
00:07:03,600 --> 00:07:06,120
See you stop the execution.

80
00:07:06,980 --> 00:07:07,620
All right.

81
00:07:07,620 --> 00:07:14,370
And then try again, save a copy of Python and you can call this.

82
00:07:17,250 --> 00:07:22,090
I tend food interrupt why?

83
00:07:25,110 --> 00:07:34,350
All right, how close these two and not that one, I need to open up the interruptive position, but

84
00:07:34,410 --> 00:07:35,880
program this one right here.

85
00:07:36,510 --> 00:07:43,500
And now that I've got the program opened on the target market Python device, I will play it.

86
00:07:44,570 --> 00:07:45,320
And.

87
00:07:46,280 --> 00:07:48,950
Press the button and works.

88
00:07:51,920 --> 00:07:53,660
Stays on for half a second as well.

89
00:07:54,740 --> 00:07:55,240
Right.

90
00:07:59,070 --> 00:08:06,900
And one thing to notice here is that in the message that starts with Button pressed at it right here,

91
00:08:07,380 --> 00:08:14,970
you can see that the output of button pin, which is this variable here, which contains the object

92
00:08:14,970 --> 00:08:19,830
that has caused the interrupt, it says painful that way.

93
00:08:19,830 --> 00:08:29,160
If you have multiple interrupts from different areas, then you can always differentiate as to which

94
00:08:29,730 --> 00:08:35,340
button or which interrupt is the one that has triggered your interrupt service routine.

95
00:08:36,830 --> 00:08:42,170
OK, so that's about it with the bat, an example using the hardware interrupter.

96
00:08:42,200 --> 00:08:48,260
I want to show you one more variation of the same in the next lecture, which involves this time using

97
00:08:48,260 --> 00:08:50,690
a timer interrupt this ticket out.
