티스토리 뷰

Technology/Arduino

ISR or Falling??

캡틴테크 2017. 1. 11. 00:00
반응형

Today, I would like to show a fundamental function while making solution. If you want to turn on and off a LED, how to make the code for that? we have 2 choices to make it which are 'falling mechanism' and 'ISR'. Here is detailed description what difference it has.

# Falling mechanism.

Once we need to set up the time how often it will be detected if you use falling mechanism. Assuming If we set up a sensor like temperature in your home and make it to gather every 10 sec from Arduino. Arduino can get the data every 10 sec from attached temperature sensor and then utilize the date like uploading it into cloud server or sending to user. Here is the code based on falling mechanism how to turn on and off for LED and button.

Make up the circuit like the picture because pull-up is brighter than normal. This circuit is using PIN 12 for LED and PIN 6 for Button, but I used PIN 12 for LED and PIN 2 for Button. I will explain why I changed the pin number. Here is the code to blink according to button.


Have you heard about ISR before? which stands for 'Interrupt Service Routine

#define BUTTON 2
#define LED 12

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
  Serial.println("START");
}

void loop() {
  if(digitalRead(BUTTON) == HIGH){
    digitalWrite(LED, HIGH);
  }
  else{
    digitalWrite(LED, LOW);
  }
}

If you type the code and flash into Arduino, the LED will be turned on and off according to Button clicking. 'loop()' section is calling every single time because it's kind a super routine. The specification of Arduino is not that good so that it is better to develop for simple work. I don't mean that more program is not possible. I expect Arduino is adaptable in case of simple work like gather sensor data. Anyway I was able to turn on and off using the falling mechanism codes. However, I will add up more functionalities like uploading data, waiting signal and controlling server motor. So I do not want to spend resource only for one case. Because falling mechanism should be checked every few sec and take some resources to check the same logic. 

# ISR.

ISR stands for Interrupt service routine. Arduino UNO provides 2 external ISR and I decided to use for receiving data. As far as I read the specification for Arduino for ISR, Int.0 is PIN 2, Int.1 is PIN 3. If I connect the pin 2 I can use external ISR 0. Before the ISR section, I used pin 2 for Button in falling mechanism coding. but now I will use pin 2 for ISR. What does it mean by ISR by the way? 'Interrupt' which is triggering if the value is changed. ISR routine is not be run normally, in other word, 'loop()' is the super routine but ISR will not be checked every single time and will be called it the value is changed. Here is code for blinking according to button.

#define LED 12
#define INTERRUPT_PIN 2
volatile int state = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(INTERRUPT_PIN, INPUT_PULLUP);
  attachInterrupt(0, blink, CHANGE);
  Serial.println("START");
}

void loop() {
  digitalWrite(LED, state);
}

void blink()
{
  state = !state;
}

I coded 'attachInterrupt' function to set up and dedicate callback function and option like 'CHANGE'

Here is official description for attachInterrupt()

- Descrption

Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: number 0(on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2(pin21), 3(pin 20), 4(pin 19), and 5(pin 18).

- Syntax

AttachInterrupt(interrupt, function, mode)

- Parameters

interrupt: the number of the interrupt(int)

function: the function to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.

mode defines when the interrupt should be triggered. Four constants are predefined as valid values;

LOW to trigger the interrupt whenever the pin is low.

CHANGE to trigger the interrupt whenever the pin changes value

RISING to trigger when the pin goes from low to high.

FALLING for when the pin goes from high to low.

In a nut shell, INT 0 is set with CHANGE and blink function. If the value of INT 0 is changed, blink() will be called and set the state value. 

# Result

Comparing the results between falling mechanism and ISR, each thing has pros and cons. I don't think ISR is always better. But aspect of management resource. I recommend you to use ISR. In case of me, I will use the ISR in order to receive commands from other device. it will not be sent every time so ISR is proper and consider what requirement is more fit-able. Next time I will check wifi module for Arduino. See you soon~!!!!

반응형

'Technology > Arduino' 카테고리의 다른 글

[Project] Pet Feeder with Arduino + EV3  (0) 2017.02.12
WIFI + WebServer + Arduino + LED control  (0) 2017.01.31
EV3 + Arduino connection via I2C  (0) 2017.01.22
Actuator control - SG90  (0) 2017.01.15
Let's start Arduino Development ~!  (0) 2017.01.02
댓글