Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing Switches Using Interrupts

Similar presentations


Presentation on theme: "Implementing Switches Using Interrupts"— Presentation transcript:

1 Implementing Switches Using Interrupts

2 DISCLAIMER & USAGE The content of this presentation is for informational purposes only and is intended for students attending Louisiana Tech University only. The authors of this information do not make any claims as to the validity or accuracy of the information or methods presented. Any procedures demonstrated here are potentially dangerous and could result in damage and injury. Louisiana Tech University, its officers, employees, agents and volunteers, are not liable or responsible for any injuries, illness, damage or losses which may result from your using the materials or ideas, or from your performing the experiments or procedures depicted in this presentation. The Living with the Lab logos should remain attached to each slide, and the work should be attributed to Louisiana Tech University. If you do not agree, then please do not view this content. boosting application-focused learning through student ownership of learning platforms

3 Implementing the Switch Circuit
ANALOG RESET 3V3 5V Gnd Vin AREF GND DIGITAL POWER Arduino 10kW switch When the switch is pressed, electricity can pass through the normally open (NO) path. Test the switch setup with this code: void setup(){ pinMode(7,INPUT); Serial.begin(9600); } void loop(){ Serial.println(digitalRead(7)); 5V Node connected to 10kΩ Resistor/Ground and digital pin

4 Using Input from the Switch to Light an LED
Add an LED circuit to your breadboard, where the LED is connected to a digital pin. Recall in 120, the following code was implemented to make an LED light up when the switch is pressed. void setup(){ pinMode(7,INPUT); pinMode(8,OUTPUT); Serial.begin(9600); } void loop(){ int whisker1=digitalRead(7); Serial.print(whisker1); if(whisker1==1) {digitalWrite(8,HIGH);} else {digitalWrite(8,LOW); } What if you want to have the Arduino doing something else, but whenever the switch is pressed it immediately responds? The given code is not written to be executed in that manner. Incorporating an interrupt can accomplish this.

5 Interrupts Allows the processor to respond quickly to specific scenarios. Is not used in the main loop which leaves the main loop available to be used for primary tasks. How the interrupt works: The processor can be executing code, but when a specific signal is detected it will immediately “interrupt” whatever the processor is doing to respond to the signal. The processor will implement the code written in response to the signal. When that code is complete, the processor will return to whatever it was originally doing.

6 Interrupts Volatile int lets the processor know that this is a variable that will likely change in value outside of the control of the program (like from a user input). volatile int whiskerState = 0; //variable for reading whisker status void setup(){ pinMode(2,INPUT); pinMode(8,OUTPUT); Serial.begin(9600); attachInterrupt(0, whisker, CHANGE); //interrupt 0 maps to pin 2 } void loop(){ //Nothing has to be written in main loop for interrupt to work void whisker(){ whiskerState=digitalRead(2); digitalWrite(8, whiskerState); Serial.println(whiskerState); Whisker must be plugged into digital pin 2 whisker (the second argument in the command) is the name of the user defined function associated with the interrupt. You can add some primary code here for the processor to execute while also being ready to implement the interrupt when needed. whiskerState will either be a 1 or a 0 depending on if the switch is pressed. If 0, then the LED will be off. If 1, then the LED will be on. Note: You cannot use time in the interrupt function (no delay(), delayMicroseconds(), or millis()). The area where the interrupt command is processed does not have a time chip.

7 Example with Interrupts
int redLED = 9; int greenLED = 10; int blueLED = 11; volatile int whiskerState = 0; //variable for reading whisker status void setup(){ pinMode(2,INPUT); pinMode(8,OUTPUT); pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); pinMode(blueLED, OUTPUT); Serial.begin(9600); attachInterrupt(0, whisker, CHANGE); //interrupt 0 maps to pin 2 } void loop(){ for (int i=0; i<256; i++) { analogWrite(redLED,255-i); analogWrite(greenLED,i); delay(10); analogWrite(redLED,i); analogWrite(greenLED,255-i); void whisker(){ whiskerState=digitalRead(2); digitalWrite(8, whiskerState); Serial.println(whiskerState); RGB LED is changing colors in the main loop. Interrupt allows the red LED to be turned on when switch is pressed.


Download ppt "Implementing Switches Using Interrupts"

Similar presentations


Ads by Google