본문 바로가기

애플리케이션 개발/HRC(Home Remote Control)

5-4. 아두이노 코딩 3

이번 시간에는 Actuator 구동 관련된 코드 부분을 살펴보자. 

 

웹페이지에서 button이 on상태로 바뀌면, Actuator가 동작하게 된다. 이 Actuator는 Limit Switch를 만날 때까지 한 방향으로 가다가, Limit Switch를 누르게 되면, 다시 돌아와야 한다. 해당 동작은 while문을 이용해 구현하였다. 

bool toggle = false;          // from web page
bool buttonState = false;     // to store state of past toggle

// 0: end of the actuator, 1: next to the motor
bool switchState[2] = {false, false};

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (buttonState == true && toggle == false) {
    toggle = true;
    while (switchState[0] == 0) {     // go to the end of the actuator
      switchState[0] = digitalRead(limitSwitch[0]);
      delay(1);
    }
    while (switchState[1] == 0) {     // return to the motor
      switchState[1] = digitalRead(limitSwitch[1]);
      delay(1);
    }
    for (int i = 0; i < 3; i++) {     // for margin away motor

    }
  }
}
  

첫 번째 while 문에서는

Actuator 끝단에 있는 Limit Switch를 누를 때까지 Actuator는 창문을 닫게 된다. 

두 번째 while 문에서는

모터 쪽에 있는 Limit Switch를 누를 때까지 반대로 움직인다. 

마지막에 for문을 이용해서, 모터를 일정 횟수 회전시켜 Limit Switch가 계속해서 눌려져 있지 않게 했다.