아두이노 코딩 및 모터드리이버에 따른 스테핑모터 구동

아뭐라노 2018-11-22 (목) 18:37 7개월전 306  

현재 blynk를 이용하여 온습도센서와 강우센서의 데이터를 토대로 창문을 열고 닫는 걸 실현하려는데요...

코딩은 아래와 같습니다... 

#define BLYNK_PRINT Serial

#define SERIAL_WIFI Serial3

 

#include <SPI.h>

#include <WizFi310.h>

#include <BlynkSimpleWizFi310.h>

#include <Stepper.h>

#include <DHT.h>

 

const int stepsPerRevolution = 2048; //모터 각도 설정, 2048= 360도 회전

const int stepsPerRevolution2 = 1024; //모터 각도 설정, 1024= 180도 회전

int a=0; //a가 1이면 창문 열린상태, 0이면 닫힌 상태

int status = WL_IDLE_STATUS;

Stepper myStepper(stepsPerRevolution,8,5,7,4); // 창문제어, 모터 드라이브에 연결된 핀 IN4, IN2, IN3, IN1 

 

int ENA=3;

int IN1=4;

int IN2=5;

int ENB=6;

int IN3=7;

int IN4=8;

 

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "토큰번호";

 

 

// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "와이파이 이름";

char pass[] = "와이파이 비밀번호";

 

WidgetLCD lcd(V4);

 

int rainSensor=A2;

float rainValue=0;

 

#define stepsPerRevolution 200

#define DHTPIN 2         

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

BlynkTimer timer;

 

void sendSensor()

{

  float h = dht.readHumidity();

  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

 

  if (isnan(h) || isnan(t)) {

    Serial.println("Failed to read from DHT sensor!");

    return;

  }

  // You can send any value at any time.

  // Please don't send more that 10 values per second.

  Blynk.virtualWrite(V5, h);

  Blynk.virtualWrite(V6, t);

}

 

void setup()

{

  // Debug console

  Serial.begin(9600);

  SERIAL_WIFI.begin(115200);

  WiFi.init(&SERIAL_WIFI);

  pinMode(ENA,OUTPUT);

 pinMode(ENB,OUTPUT);

 pinMode(IN1,OUTPUT);

 pinMode(IN2,OUTPUT);

 pinMode(IN3,OUTPUT);

 pinMode(IN4,OUTPUT);

 digitalWrite(ENA,HIGH);

 digitalWrite(ENB,HIGH);

 

  Blynk.begin(auth, ssid, pass);

  // You can also specify server:

  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);

  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

   dht.begin();

   timer.setInterval(1000L, sendSensor);

  lcd.clear();

}

 

void loop()

{

  Blynk.run();

    timer.run();

  digitalWrite(8,HIGH);

  digitalWrite(9,HIGH);

  int rainPoint = analogRead(rainSensor);

  if (analogRead(A2) <= 800)

  {

 digitalWrite(IN1,HIGH);

 digitalWrite(IN2,LOW);

 digitalWrite(IN3,HIGH);

 digitalWrite(IN4,LOW);

 delay(10);

 

 digitalWrite(IN1,HIGH);

 digitalWrite(IN2,LOW);

 digitalWrite(IN3,LOW);

 digitalWrite(IN4,HIGH);

 delay(10);

 

 digitalWrite(IN1,LOW);

 digitalWrite(IN2,HIGH);

 digitalWrite(IN3,LOW);

 digitalWrite(IN4,HIGH);

 delay(10);

 

 digitalWrite(IN1,LOW);

 digitalWrite(IN2,HIGH);

 digitalWrite(IN3,HIGH);

 digitalWrite(IN4,LOW);

 delay(10);

  myStepper.step(-stepsPerRevolution); 

    delay(200);

    myStepper.step(-stepsPerRevolution2); 

    a=0;

  } 

}

BLYNK_WRITE(V1) //창문 제어

{

  int btnValue = param.asInt();

  if(btnValue == 1) //창문 열음

  {

 digitalWrite(IN1,LOW);

 digitalWrite(IN2,HIGH);

 digitalWrite(IN3,HIGH);

 digitalWrite(IN4,LOW);

 delay(10);

 

 digitalWrite(IN1,LOW);

 digitalWrite(IN2,HIGH);

 digitalWrite(IN3,LOW);

 digitalWrite(IN4,HIGH);

 delay(10);

 

 digitalWrite(IN1,HIGH);

 digitalWrite(IN2,LOW);

 digitalWrite(IN3,LOW);

 digitalWrite(IN4,HIGH);

 delay(10);

 

 digitalWrite(IN1,HIGH);

 digitalWrite(IN2,LOW);

 digitalWrite(IN3,HIGH);

 digitalWrite(IN4,LOW);

 delay(10);

  myStepper.step(stepsPerRevolution);

    delay(500);

    myStepper.step(stepsPerRevolution2);

    a=1;

  }

  if(btnValue == 0) //창문 닫음

  {

 digitalWrite(IN1,HIGH);

 digitalWrite(IN2,LOW);

 digitalWrite(IN3,HIGH);

 digitalWrite(IN4,LOW);

 delay(10);

 

 digitalWrite(IN1,HIGH);

 digitalWrite(IN2,LOW);

 digitalWrite(IN3,LOW);

 digitalWrite(IN4,HIGH);

 delay(10);

 

 digitalWrite(IN1,LOW);

 digitalWrite(IN2,HIGH);

 digitalWrite(IN3,LOW);

 digitalWrite(IN4,HIGH);

 delay(10);

 

 digitalWrite(IN1,LOW);

 digitalWrite(IN2,HIGH);

 digitalWrite(IN3,HIGH);

 digitalWrite(IN4,LOW);

 delay(10);

 myStepper.step(-stepsPerRevolution);

    delay(500);

    myStepper.step(-stepsPerRevolution2);

  }

}

 

그런데 지금 여기서 온습도를 blynk어플에서 확인도 가능하고, 강우센서에서 물을 감지하면 모터도 돌아갔다가 물을 닦으면 모터가 멈춥니다....

그런데.... 여기서 blynk어플을 통해 창문을 열고 닫는게 지금 안되는데.... 도움주세요.....


모바일 버전으로 보기