본문
Home Control System Project
사물인터넷(IoT) 2017. 5. 17. 20:30
반응형
# Home Control System Project
Arduino를 이용한 홈 제어 프로젝트
1. 기능소개
1. Led On Off
2. Bright & Servo Moter (Servo Moter를 이용하여 창문 커튼 여닫이 기능 구현 가능)
3. Temperature / DC Moter
4. Intrusion Detection
2. 시연영상
https://www.youtube.com/watch?v=1_keZS0Sbew
3. source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | ============= // homeControl ============= void initSystem(); void showMenu(); void getUserCmd(); void processCmd(); void setup() { // put your setup code here, to run once: Serial.begin(9600); while(!Serial) { ; } Serial.println(__FUNCTION__); initSystem(); showMenu(); } void loop() { // put your main code here, to run repeatedly: getUserCmd(); processCmd(); } ============= // funcs ============= #include <TimerOne.h> const int ledPin = 3; struct MenuFunc { bool enable; char * pMenu; void (* pFInit)(bool); void (* pFunc)(); }; void funcLedOnOffInit(bool); void funcLedFadingInit(bool); //void func2init(bool); void funcSensingButtonInit(bool); void funcInterruptButtonInit(bool); void funcLedOnOff(); void funcLedFading(); //void func2(); void funcSensingButton(); void funcInterruptButton(); void timerLoop(); MenuFunc menu_funcs[] = { {false, "Led On Off", funcLedOnOffInit, funcLedOnOff}, {false, "Led Fading", funcLedFadingInit, funcLedFading}, {false, "Button Sensing", funcSensingButtonInit, funcSensingButton}, {false, "Button Interrupt", funcInterruptButtonInit, funcInterruptButton}, {false, "Timer Interrupt", timerInit, timerLoop}, }; void funcLedOnOffInit(bool bInit) { if(bInit) pinMode(ledPin, OUTPUT); } void funcLedOnOff() { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); } void funcLedFadingInit(bool bInit){} extern const int ledPin; const int buttonPin = 2; int currentButtonState; void funcSensingButtonInit(bool bInit) { if(bInit) { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } } int pin = 3; volatile int state = LOW; void funcInterruptButtonInit(bool bInit) { if(bInit) { pinMode(pin, OUTPUT); attachInterrupt(0, blink, CHANGE); } else { detachInterrupt(0); } } void funcSensingButton() { currentButtonState = digitalRead(buttonPin); digitalWrite(ledPin, currentButtonState); Serial.println(currentButtonState); } //int pin = 3; //volatile int state = LOW; void funcInterruptButton() { digitalWrite(pin, state); } void blink() { state = !state; Serial.println(__FUNCTION__); } void funcLedFading() { int br; for(br=0;br<255;br++) { analogWrite(ledPin, br); delay(4); } for(br=255;br>0;br--) { analogWrite(ledPin, br); delay(4); } } void timerInit(bool bInit) { if(bInit) { pinMode(13, OUTPUT); Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second) Timer1.attachInterrupt( timerIsr ); // attach the service routine here } else { Timer1.detachInterrupt(); } } void timerLoop() { // TODO } void timerIsr() { // Toggle LED digitalWrite( 13, digitalRead( 13 ) ^ 1 ); Serial.println(__FUNCTION__); } ============= work ============= static char cmd = '?'; static String inputString = ""; static boolean stringComplete = false; struct MenuFunc; extern MenuFunc menu_funcs[]; void initSystem() {} void showMenu() { int i; // Serial.println(__FUNCTION__); for(i=0;i<sizeof(menu_funcs)/sizeof(menu_funcs[0]);i++) { Serial.print(i+1); Serial.print(". "); Serial.println(menu_funcs[i].pMenu); } Serial.println("\nINPUT> "); } void getUserCmd() { if (stringComplete) { cmd = inputString[0]; inputString = ""; if(cmd != '\r' && cmd != '\n') { int cmdIndex = cmd - '0' - 1; int howManyCmds = sizeof(menu_funcs)/sizeof(menu_funcs[0]); if(cmdIndex>=0 && cmdIndex<howManyCmds) { bool * pFuncEn = &menu_funcs[cmdIndex].enable; *pFuncEn = !*pFuncEn; menu_funcs[cmdIndex].pFInit(*pFuncEn);//--- } } stringComplete = false; } } void processCmd() { int cmdIndex; int howManyCmds = sizeof(menu_funcs)/sizeof(menu_funcs[0]); for(cmdIndex=0;cmdIndex<howManyCmds;cmdIndex++) if(menu_funcs[cmdIndex].enable) { menu_funcs[cmdIndex].pFunc(); bool * pFuncEn = &menu_funcs[cmdIndex].enable; *pFuncEn = !*pFuncEn; } } void serialEvent() { while (Serial.available()) { char inChar = (char)Serial.read(); inputString += inChar; //if (inChar == '\n') stringComplete = true; } } | cs |
# Link
1. Github
반응형
댓글