Using Arduino interrupts

So far, we have seen how to use the push buttons to change the speed of the DC motor. The previous sketch is written to continuously loop and check for the state of each push button one by one in a sequential manner. First the sketch checks button 1, followed by button 2, and then button 3 - this sequence of checking the state of the buttons continues endlessly. This method of checking for user input may lead to timing issues. Let us understand how.

Let us say you press button 3 (to stop the motor) and at that point in time, if the sketch was checking the state of button 1, then we would have to wait for the sketch to first finish checking button 1, then button 2, and finally button 3 would be checked. Since our sketch is small in size and not doing too many things, you will hardly notice any issues. However, in real-world situations, this is not an ideal way to handle time-sensitive input events (for example, a button press).

Time sensitive input events should be handled in a responsive manner. In order to achieve responsiveness, Arduino provides the mechanism of interrupts. Interrupts provide a real-time reaction (signal processing) mechanism in the micro-controller world. It provides the ability to execute a function as soon as there is a change in the signal level on a pin (connected to a peripheral device like a simple button in the next example).

Only digital pins 2 and 3 can be used for the purpose of interrupts in the Arduino Uno board. You can refer to the following URL for a comprehensive list of Arduino boards and their interrupt pins:
https://www.arduino.cc/en/Reference/AttachInterrupt

To demonstrate the interrupt handling technique, we will utilize the previous example with three push buttons controlling the DC motor speed. In this case, we will make a very small change to the circuit setup and the C sketch.

In the C sketch, we are going to designate digital pin 2 (for receiving the stop signal via button 3) as an interrupt and associate this pin to execute a pre-defined C function as soon as button 3 is pressed. After loading and executing this sketch, you may notice a slight difference in the responsiveness of button 3 (for stopping the motor), as the sketch will directly jump to executing the function attached with the pin for button 3. In real-world situations, this slight improvement in responsiveness can work wonders and solve many timing and peripheral device synchronization issues.

Go ahead and load the following sketch for learning Arduino Interrupts into your board:

//**********************************************************/ 
// Step-1: CONFIGURE VARIABLES
//**********************************************************/
int motorPin = 3; //this is a PWM capable pin
int buttonPin = 8; //button to start at low speed
int buttonPin2 = 7; //button to start at high speed
int buttonPin3 = 2; //button to stop
int buttonState = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;
//**********************************************************/
// Step-2: INITIALIZE I/O PARAMETERS
//**********************************************************/
void setup()
{
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);

// This line specifies that as soon as Pin 2 (for Button 3)
// is HIGH, the stop() function should be executed
attachInterrupt(digitalPinToInterrupt(buttonPin3),stop,HIGH);
}
//**********************************************************/
// Step-3: MAIN PROGRAM
//**********************************************************/
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);

if(buttonState == HIGH)
{
analogWrite(motorPin, 170); //run at low speed
}
else if(buttonState2 == HIGH)
{
analogWrite(motorPin, 240); //run at high speed
}
}

void stop()
{
analogWrite(motorPin, 0); //stop the motor
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.125.219