###### tags: `Arduino`
# 溫感整合stepo
using ATmega2560
```cpp=
#include <Unistep2.h>
//IN1, IN2, IN3, IN4, 總step數(一圈), 每步的延遲(in micros)
//水平
Unistep2 stepper1(22, 24, 26, 28, 4096, 1000);
Unistep2 stepper2(23, 25, 27, 29, 4096, 1000);
//垂直
Unistep2 stepper3(40, 42, 44, 46, 4096, 1000);
Unistep2 stepper4(41, 43, 45, 47, 4096, 1000);
int hight = 0;
//溫感設定
#include "DHT.h"
#define DTpin 2
#define DHTTYPE DHT22
DHT dht(DTpin,DHTTYPE);
//lcd設定
#include<Wire.h>
#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int button[4] = {3,4,5,6}; //左,右,上,下控制按鈕
const int ledr = 11, ledy = 10, ledg = 9; //紅黃綠燈
const int buz = 13; //蜂鳴器
void setup(){
Serial.begin(9600);
dht.begin();
for(int i=3;i<7;i++){
pinMode(i,INPUT);
}
for(int i=7;i<=53;i++){
pinMode(i,OUTPUT);
}
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("start");
delay(1000);
lcd.clear();
}
void loop(){
stepper1.run(); //步進機啟動
stepper2.run();
stepper3.run();
stepper4.run();
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
/*if (isnan(h) || isnan(t) ) {
Serial.println(F("感測器讀取失敗"));
return;
}*/
Serial.print("濕度: ");
Serial.print(h);
Serial.print("% 溫度: ");
Serial.print(t);
Serial.print("°C");
Serial.println();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("T:");lcd.print(t);lcd.print((char) 0xDF);
lcd.print("C") ; //顯示度C的那個度的符號
lcd.setCursor(0,1);
lcd.print("H:");lcd.print(h);lcd.print("%");
if(t>=40){ //紅燈亮
digitalWrite(ledr,1);
digitalWrite(ledy,0);
digitalWrite(ledg,0);
lcd.setCursor(1,0);
lcd.print("Temp Alarming");
for(int i=0;i<20;i++){ //i上限是蜂鳴器次數
tone(13, 440);
delay(25);
noTone(13); // 停止播放
delay(25);
}
}else if(t>=35){ //黃燈亮
digitalWrite(ledr,0);
digitalWrite(ledy,1);
digitalWrite(ledg,0);
}else{ //綠燈亮
digitalWrite(ledr,0);
digitalWrite(ledy,0);
digitalWrite(ledg,1);
}
--------
if(Serial.available()){
char a = Serial.read();
if(a == "L"){ //預設為左
Serial.println('L');
if ( stepper1.stepsToGo() == 0 && stepper2.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper1.move(cal(1)); //水平移動 預設1cm 在1秒內轉
stepper2.move(cal(-1));
}
}
else if(a == "R"){ //預設為右
Serial.println('R');
if ( stepper1.stepsToGo() == 0 && stepper2.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper1.move(cal(-1)); //水平移動 預設1cm 在1秒內轉
stepper2.move(cal(1));
}
}
else if(a == "U"){ //上
Serial.println('U');
if ( stepper3.stepsToGo() == 0 && stepper4.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper3.move(cal(1)); //水平移動 預設1cm 在1秒內轉
stepper4.move(cal(1));
}
}
else if(a == "D"){ //下
Serial.println('D');
if ( stepper3.stepsToGo() == 0 && stepper4.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper3.move(cal(-1)); //水平移動 預設1cm 在1秒內轉
stepper4.move(cal(-1));
}
}
}
if(digitalRead(button[0])==1){ //預設為左
Serial.println('L');
if ( stepper1.stepsToGo() == 0 && stepper2.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper1.move(cal(1)); //水平移動 預設1cm 在1秒內轉
stepper2.move(cal(-1));
}
}
if(digitalRead(button[1])==1){ //預設為右
Serial.println('R');
if ( stepper1.stepsToGo() == 0 && stepper2.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper1.move(cal(-1)); //水平移動 預設1cm 在1秒內轉
stepper2.move(cal(1));
}
}
if(digitalRead(button[2])==1){ //上
Serial.println('U');
if ( stepper3.stepsToGo() == 0 && stepper4.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper3.move(cal(1)); //水平移動 預設1cm 在1秒內轉
stepper4.move(cal(1));
}
}
if(digitalRead(button[3])==1){ //下
Serial.println('D');
if ( stepper3.stepsToGo() == 0 && stepper4.stepsToGo() == 0){ // 如果stepsToGo=0,表示步進馬達已轉完應走的step了
stepper3.move(cal(-1)); //水平移動 預設1cm 在1秒內轉
stepper4.move(cal(-1));
}
}
}
int cal(int n){ //垂直 n -> cm
return n*4096; //4096 = 比例
}
}
```