Arduino Foundation Series: What is an Interrupt?
So first, let me show you the layout on the board. These wires on the side are just connecting our power rails. Together, we have a switch with one side going to ground the other side going to digital two. We have an led, grounded out its anode, going through a 330 ohm resistor into digital four. Then we have simply brought out power and ground so really theres. Only one thing this circuit can do right to toggle its a light toggle. So what makes this special? Nothing particularly its a very simple circuit, but if you havent taken classes in arduino or been exposed or anything this could be. You know something that might be very puzzling to you, because heres the thing im going to show you the code theres nothing going on in loop now. I know that half youve already learned well, of course, but thats, because its in an interrupt its an interrupt lets. Take a look at it: okay, so heres a short little sketch, and there is nothing to it – were creating a integer variable called pin and were setting it a value of four that is going to be our output. Now we are creating a volatile, integer called state and were setting its output to low the volatility, simply means it can change. This is important. This means that the interrupt that were going to use is going to be able to pass that value back somewhere all right. So here we have our setup pin mode pin, which we just talked about right here.
Number four is set as output and dont forget your semicolons. When youre dealing with dc language, we have digital write to hi, so were setting that pin high now were going to attach our interrupt thats interrupt 0. There are two interrupts 0 and 1 and they attach the pins 2 and 3 as a toggle on the falling edge now heres a loop of our program and, as you can see other than a little comment, theres nothing going on here. Nobody is reading the button. Nothing is looking for the button in the loop of the program, so you can have all sorts of things going on here, including delays, but when we did this here, when we attached an interrupt, it is going to bypass all that see. The interrupt is an asynchronous communication, a synchronous one would be youre not going to read the button until you get to line 15 here, and it says digital read, uh number, two right: no, you have to do any of that. You can have whatever you want going on here. Here is our interrupt. Remember we called it toggle so down here we just create a little subroutine called toggle, and then we say if state which we created up here, which starts out as low, then it toggles to high otherwise its low. And then we do a digital write to the pin called state and thats what you just saw over here. It simply flicks it back and forth between one and the other, now theres a hundred more things you can do with it, but this is just a very simple explanation of what to do and how to set up an interrupt on the arduino.
So i hope you enjoyed it. If you did please give me a thumbs up, feel free to comment, share and dont forget to subscribe a big thanks to all the patrons big thanks to you guys for watching thats.