J 창고

[Arduino] LCD, interrupt 구현 본문

컴퓨터/Firmware

[Arduino] LCD, interrupt 구현

JSFamily 2013. 11. 2. 15:48


#include <LiquidCrystal.h>


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


int state = 0;


void setup() {

  lcd.begin(16, 2);

  attachInterrupt(0, lcd_print_F, CHANGE);


}


void loop() {

  lcd.setCursor(0, 0);

  lcd.print(millis()/1000);

  if(state == HIGH){

    lcd.setCursor(0, 1);

    lcd.print("Button ON");

  }

  else{

     lcd.setCursor(0, 1);

     lcd.print("                ");

  }

}


void lcd_print_F(){

 state = !state;

}



=====================================================================


2번핀(int.0)에 연결되어 있는 버튼의 상태가 변화할 때

state의 값이 HIGH가 되면 LCD (0,1)에 Button ON 문자 출력

반대로 LOW가 되면 LCD (0,1)을 공백.


하지만 전원이 들어가고부터 LCD(0,0)에선 mills 함수를 이용해 초 카운팅


여기서 헤맸던 점은

인터럽트 되었을 때 LCD에 Button On을 출력해야하는 줄 알고 계속 시도했는데

기존 Loop단과 충돌이 나는지 문자가 제대로 안나오고 맛이 갔는데..


인터럽트 되어 생기는 함수는 단순히 상태만 저장하고

그 상태에 따른 LCD 출력은 loop단에서 구현해도 된다는점? 








Comments