Implementing Switches Using Interrupts

Slides:



Advertisements
Similar presentations
temperature system wiring
Advertisements

Using the servo library living with the lab Libraries are computer programs written to add functionality to Arduino programs. A library to control hobby.
Analog and Digital Measurements living with the lab 14 digital input / output pins 6 analog input pins © 2012 David Hall.
Waterproofing a thermistor ENGR 121 living with the lab © 2013 David Hall.
Using the Arduino to Make an LED Flash Work in teams of two! living with the lab digital I/O pins (I/O = input / output) USB cable plug power pins.
User-defined functions in Arduino sketches living with the lab © 2012 David Hall.
Calibration of conductivity sensors living with the lab.
Using Your Arduino, Breadboard and Multimeter Work in teams of two! living with the lab 1 © 2012 David Hall.
Thermistor calibration living with the lab © 2013 David Hall.
Cascaded switching of a solenoid valve living with the lab transistor relay solenoid valve © 2012 David Hall.
Assembly of conductivity flow loop living with the lab (in preparation for calibrating conductivity sensor)
Switches & whiskers on the Arduino living with the lab lever arm switches mounted to Arduino © 2012 David Hall.
220  470  Gnd5V Currents Through Parallel Resistors 1 living with the lab © 2012 David Hall.
Using Hobby Servos with the Arduino living with the lab © 2012 David Hall.
Kirchoff’s Current Law (KCL) living with the lab University of Pennsylvania Library and Wikipedia Gustav Kirchoff (left) and Robert Bunsen (right) Bunsen.
Using for loops to control LEDs living with the lab 1 1 arduino.cc the for statement allows us to repeat a block of commands a limited number of times.
Building Circuits.
Pump Fabrication Day Group A will draw their pump
Controlling Servos with the Arduino
Connecting Switches.
Series and Parallel Resistors
Pump Project Requirements
Introduction to the Arduino
Why Won’t My Arduino Work?
Troubleshooting Your Multimeter
Using servos.
calibration of conductivity sensors
Servo Library and Functions
Line Following Tips photoresistor circuits
What’s in your kit?.
Pump Project Overview.
Robot Assembly.
How to Use Dial Calipers
Controlling a Motor with Cascading Switches
Introduction to Transistors
Introduction to the Fishtank
RGB LEDs.
Conservation of Mass Problem
Maxbotix Ultrasonic Distance Sensor
Conductivity Sensor.
Introduction to Transistors
Servo Library and Functions
Troubleshooting Your Multimeter
a few of my favorite sensors
Relays.
using for loops to control LEDs
using the Arduino to make LEDs flash
Acquiring Data from an ADXL335 Accelerometer
Using Photoresistors with an Arduino
Line Following Tips photoresistor circuit
Torque and RPM of Gears
Conservation of Mass Problem
Data Types.
analog and digital measurements
Using “if” statements.
Controlling the Heater
Design Project Forecast
Digital Input from Switches
Arduino: For Loops.
Non-Concurrent Force Systems
IR Object Detection IR detector IR LED IR light reflected off object
Radio Frequency Transmitter and Receiver
Interfacing a Rotary Encoder with an Arduino
Conservation of Mass Problem
Non-Concurrent Force Systems
Evaluating Design Alternatives
Counting Servo Gear Teeth (FS90R Servos)
Static Equilibrium Problem
Reservoir Loop.
Freshman Design Expo Presentations
Presentation transcript:

Implementing Switches Using Interrupts

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

Implementing the Switch Circuit ANALOG RESET 3V3 5V Gnd Vin 0 1 2 3 4 5 7 6 5 4 3 2 1 0 3 2 1 0 9 8 1 1 1 1 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

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.

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.

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.

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.