IoT (구 유비쿼터스)/아두이노

[아두이노] Motor Fan 결과 및 소스코드

개발자_이훈규 2015. 3. 11. 01:24


[아두이노] Motor Fan 결과 및 소스코드


 

아두이노 DFROBOT의 비기너 킷의 project 13입니다.





1. 소스



motor_fan.ino


/*

   Motor Fan

*/


int buttonPin = 2;                          // button pin -- Digital 2

int relayPin = 3;                           // relay pin -- Digital 3

int relayState = HIGH;                      

int buttonState;                            

int lastButtonState = LOW;                 

long lastDebounceTime = 0;                  

long debounceDelay = 50;                    


void setup() {

  pinMode(buttonPin, INPUT);

  pinMode(relayPin, OUTPUT);


  digitalWrite(relayPin, relayState);       

}

void loop() {

   // read the state of the switch into a local variable:

  int reading = digitalRead(buttonPin);  

  

  // check to see if you just pressed the button 

  // (i.e. the input went from LOW to HIGH),  and you've waited 

  // long enough since the last press to ignore any noise:  


  // If the switch changed, due to noise or pressing:

  if (reading != lastButtonState) {   

    lastDebounceTime = millis();

  } 

  

  if ((millis() - lastDebounceTime) > debounceDelay) {

    // whatever the reading is at, it's been there for longer

    // than the debounce delay, so take it as the actual current state:


    // if the button state has changed:

    if (reading != buttonState) {

      buttonState = reading;

      

      // only toggle the Relay if the new button state is HIGH

      if (buttonState == HIGH) {

        relayState = !relayState;

      }

    }

  }

 

   // set the relay: 

  digitalWrite(relayPin, relayState);


  // save the reading.  Next time through the loop,

  // it'll be the lastButtonState:

  lastButtonState = reading;

}



2. 결과 화면








3. 분석 및 커스텀 마이징


우선 relay의 역활을 이해하고, 구조는 버튼의 상태의 변화에 따라 relay의 값이 변하는 것을 이해하면 더욱 쉽습니다.
코드 중 loop함수내에서 위에 부터 읽기 시작하면, 시간 변화에 따라 버튼의 상태를 바꿔주고 버튼의 상태에 따라서 relay의 값을 바꿔주게 됩니다. 
이해를 더욱 잘 하기 위해서 print문을 넣어보겠습니다.

print문을 넣고 시그널 모니터를 열어 놓은 상태로 버튼을 누르니 아두이노와 컴퓨터의 연결이 끊기네요...
원인을 알 수 없지만 우선 코딩은 시작하겠습니다.

제가 바꿀 부분은 스위치가 눌린 상태면 모터가 움직이는 것으로 만들려고 합니다.
[working]


4. 기타


개인적으로 커스텀마이징을 하려고 하는데, Relay의 역활과 소스의 이해를 필요로 하는 것 같습니다.