1. 程式人生 > >進階之路(中級篇) - 018 基於arduino的簡易版智能衣架

進階之路(中級篇) - 018 基於arduino的簡易版智能衣架

檢驗 dig cloc 布線 pin on() -- mage 根據

一. 設備及要求

目的:制作一個可以自動根據事實的天氣的狀況進行對衣架上的衣服進行晾曬。

基礎裝置:可伸縮的晾衣架。

開發環境:Arduino1. 8.1

主控板:Arduino UNO

動力裝置:二相四線步進電機、電機控制板

供電設備:5V/500mA電源、12V/2A電機供電電源

傳感器:雨滴傳感器、接觸傳感器

其它:雙面膠若幹(固定傳接觸感器)

二. 布線:

技術分享

三.代碼:

  1 // I/O引腳定義
  2 #define PUL 2         //電機時鐘
  3 #define L_ENA 3     //A電機使能
  4 #define L_DIR 4     //A電機轉向
  5
#define L_key 5 //A電機復位按鍵(0) 6 #define R_ENA 6 //B電機使能 7 #define R_DIR 7 //B電機轉向 8 #define R_key 8 //B電機復位按鍵(1) 9 #define Rain A0 //雨水 10 11 //宏定義 12 bool Clock_status = 0; 13 14 void set_init_pinMode(){ 15 pinMode(PUL,OUTPUT); //時鐘 16 pinMode(L_ENA,OUTPUT); //
L 17 pinMode(L_DIR,OUTPUT); 18 pinMode(L_key,INPUT); 19 pinMode(R_ENA,OUTPUT); //R 20 pinMode(R_DIR,OUTPUT); 21 pinMode(R_key,INPUT); 22 pinMode(Rain,INPUT); //雨水 23 } 24 /*************************************/ 25 //時鐘函數 26 void Clock_function(void){ 27 delayMicroseconds(10000
); //時鐘 28 digitalWrite(PUL,Clock_status); 29 Clock_status = !Clock_status; 30 } 31 /***********************************/ 32 //電機使能關閉 33 void Close_motor(void){ 34 digitalWrite(L_ENA,1); 35 digitalWrite(R_ENA,1); 36 } 37 //電機使能開啟 38 void start_motor(void){ 39 digitalWrite(L_ENA,0); 40 digitalWrite(R_ENA,0); 41 } 42 /**********************************/ 43 //拉回方向 44 void Pull_direction(){ 45 digitalWrite(L_DIR,0); 46 digitalWrite(R_DIR,0); 47 } 48 //推出方向 49 void Push_direction(){ 50 digitalWrite(L_DIR,1); 51 digitalWrite(R_DIR,1); 52 } 53 /********************************/ 54 //拉回電機 55 void Pull_motor(){ 56 start_motor(); //開啟電機 57 Pull_direction(); //拉回電機鳳方向設置 58 } 59 //推出電機 60 void Push_motor(){ 61 start_motor(); //開啟電機 62 Push_direction(); //拉回電機鳳方向設置 63 } 64 //無檢驗停止 65 void No_test_stop_motor(){ 66 Close_motor(); //關閉使能 67 } 68 //檢驗停止 69 void test_stop_motor(int key1,int key2){ 70 if(key1)digitalWrite(L_ENA,1); 71 else digitalWrite(L_ENA,0); 72 if(key2)digitalWrite(R_ENA,1); 73 else digitalWrite(L_ENA,0); 74 } 75 /********************************/ 76 void printf_all(int mode,unsigned int step1){ 77 Serial.print("mode ="); 78 Serial.print(mode); 79 Serial.print(" "); 80 Serial.print("PUL ="); 81 Serial.print(digitalRead(PUL)); 82 Serial.print(" "); 83 Serial.print("L_DIR ="); 84 Serial.print(digitalRead(L_DIR)); 85 Serial.print(" "); 86 Serial.print("L_ENA ="); 87 Serial.print(digitalRead(L_ENA)); 88 Serial.print(" "); 89 Serial.print("L_key ="); 90 Serial.print(digitalRead(L_key)); 91 Serial.print(" "); 92 Serial.print("R_DIR ="); 93 Serial.print(digitalRead(R_DIR)); 94 Serial.print(" "); 95 Serial.print("R_ENA ="); 96 Serial.print(digitalRead(R_ENA)); 97 Serial.print(" "); 98 Serial.print("R_key ="); 99 Serial.print(digitalRead(R_key)); 100 Serial.print(" "); 101 Serial.print("Rain ="); 102 Serial.print(analogRead(Rain)); 103 Serial.print(" "); 104 Serial.print("step1 ="); 105 Serial.println(step1); 106 } 107 //初始化 108 void setup(){ 109 set_init_pinMode(); 110 Serial.begin(19200); 111 Serial.println("DHTxx test!"); 112 } 113 void loop(){ 114 int mode = 0; //模式 115 int a,b; 116 unsigned int step1 = 0; 117 while(1){ 118 switch(mode){ 119 case 0:{ //拉回電機 120 Pull_motor(); 121 mode = 1; 122 }break; 123 case 1:{ //檢測狀態 124 a = digitalRead(L_key); 125 b = digitalRead(R_key); 126 digitalWrite(L_ENA,a); 127 digitalWrite(R_ENA,b-1); 128 step1--; 129 if((1==a)&&(0==b)&&(analogRead(Rain) > 850)){ 130 mode = 2; 131 step1 = 0; 132 Push_motor(); 133 } 134 }break; 135 case 2:{ //雨板檢測 136 if(analogRead(Rain) < 850)mode = 0; 137 else if(7000 < step1++){ 138 Close_motor(); 139 step1 = 65000; 140 } 141 }break; 142 } 143 //printf_all(mode,step1); 144 Clock_function(); 145 } 146 }

四. 缺陷與不足:

  1. 雨板模擬量幹擾沒有處理
  2. 按鍵幹擾沒有處理
  3. 沒有電源檢測
  4. 沒有光線檢測
  5. 沒有衣物幹燥度檢測
  6. 沒有備用電源
  7. 沒有電源停電檢

進階之路(中級篇) - 018 基於arduino的簡易版智能衣架