BASIC4MCU | 질문게시판 | 아두이노를 이용하여 스텝모터제어 에 관해 질문드립니다.
페이지 정보
작성자 무야홍 작성일2021-06-04 21:55 조회10,516회 댓글2건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=15843
작성일
본문
온습도센서 와 미세먼지 센서를 통해 얻은 값을 LCD에 표시하며 부저음이 일어나고 미세먼지 센서를 통해 측정한 값으로 스텝모터를 일정값 이상이면 정회전 일정값 이하이면 역회전 하도록 코딩 하고 싶습니다 .ㅠㅠ
#include <Wire.h>#include <LiquidCrystal_I2C.h>#include <DHT11.h>#include <Stepper.h>const int stepsPerRevolution = 300; //회전수 200/한바퀴Stepper myStepper(stepsPerRevolution, 3, 4, 6, 7);int ENA=8;int ENB=5;int in1Pin = 10;int in2Pin = 9;int enablePin = 11;#if defined(ARDUINO) && ARDUINO >= 100#define printByte(args) write(args);#else#define printByte(args) print(args,BYTE);#endif//I2C LCD는 일반적으로 0x27혹은 0x3F입니다LiquidCrystal_I2C lcd(0x27, 16, 2);byte temperatureImage[] = {0x04, 0x0A, 0x0A, 0x0A, 0x0E, 0x1F, 0x1F, 0x0E};byte humidityImage[] = {0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x1F, 0x1F, 0x0E};byte doImage[] = {0x1C, 0x14, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00};//LCD "m" 이미지byte microImage[] = {0x11, 0x11, 0x11, 0x13, 0x15, 0x18, 0x10, 0x10};//LCD "3" 이미지byte threeImage[] = {0x18, 0x04, 0x18, 0x04, 0x18, 0x00, 0x00, 0x00};//=====디지털 핀const int DHT_PIN = 1;const int DUST_PIN = 12;const int BUZZER_PIN = 13;//=====온습도센서 관련 변수float humidity = 0;float temperature = 0;DHT11 dht11(DHT_PIN);//=====먼지센서 관련 변수unsigned long duration;unsigned long starttime;unsigned long sampletime_ms = 2000;// 먼지센서의 샘플링시간을 2초로 설정합니다.unsigned long lowpulseoccupancy = 0;float ratio = 0;float concentration = 0;float dustDensity = 0;float dustState = 0;boolean DustCalculate_RUN = false; // DustCalculate_RUN 일때 펄스를 가진다.boolean DustCalculate_Done = false; // DustCalculate_Done 일떄 펄스를 가진다.unsigned int buzzer_count = 0;// 핀 초기화void initPin() {pinMode(DUST_PIN, INPUT);pinMode(BUZZER_PIN, OUTPUT);}// LCD 초기화void initLCD() {lcd.init();lcd.backlight();// 그림아이콘을 등록합니다.lcd.createChar(0, humidityImage);lcd.createChar(1, temperatureImage);lcd.createChar(2, doImage);lcd.createChar(3, microImage);lcd.createChar(4, threeImage);lcd.home();lcd.print("Loading...");}void setup(){myStepper.setSpeed(45);pinMode(enablePin, OUTPUT);pinMode(in1Pin, OUTPUT);pinMode(in2Pin, OUTPUT);pinMode(ENA,OUTPUT); //pinMode(ENB,OUTPUT);//digitalWrite(ENA,HIGH);//digitalWrite(ENB,HIGH);//initPin();initLCD();starttime = millis();}void loop(){//부저가 다 울린 뒤에 미세먼지 측정 시작!//미세먼지 측정 중에는 부저와 시작시간 초기화 안되도록 함.if (DustCalculate_RUN == true) // 측정 시작{calcDustDensity();//미세먼지 측정 후에 온습도 측정 / LCD 표시if (DustCalculate_Done == true) {calcHumidityAndTemperature();printLCD();DustCalculate_Done = false;}} else {/*국제 미세먼지농도에 따른 경계단계 기준분류30ug/m^3 이하 : 좋음 / 30~80ug/m^3 : 보통 / 80~150ug/m^3 : 나쁨 / 150ug/m^3 초과 : 매우 나쁨먼지농도 경계 단계에 따라 LCD 표시내용이 달라지며,부저가 각각 보통 1번 / 나쁨 2번 / 매우 나쁨 3번 연속으로 울리도록 되어있습니다.*/if (buzzer_count > 0 ) {digitalWrite(BUZZER_PIN, HIGH);delay(100);digitalWrite(BUZZER_PIN, LOW);delay(200);buzzer_count--;} else digitalWrite(BUZZER_PIN, LOW);//시작시간 초기화if ((dustState > 0 && buzzer_count == 0) || (dustState == 0)) // dustState > 0 크고 buzzer_count == 0 일때{DustCalculate_RUN = true;starttime = millis();}}if (dustDensity >= 150){myStepper.step(stepsPerRevolution);}else if(dustDensity <= 150){myStepper.step(-stepsPerRevolution);}}void printLCD() {//LCD에 먼지센서와 온습도센서를 출력합니다.lcd.clear();lcd.setCursor(0, 0);lcd.print(dustDensity);lcd.write(3);lcd.print("g/m");lcd.write(4);lcd.setCursor(10, 0);if (dustState == 0)lcd.print(" (^_^)");else if (dustState == 1)lcd.print(" (o_o)");else if (dustState == 2)lcd.print(" (T.T)");else if (dustState == 3)lcd.print(" (ToT)");lcd.setCursor(0, 1);lcd.write(0);lcd.print(" ");lcd.print(humidity);lcd.print("% ");lcd.write(1);lcd.print(" ");lcd.print(temperature);lcd.write(2);lcd.print("C ");}/** 신뢰할 수 있는 먼지밀도 계산하기대부분의 아날로그센서의 경우 값이 튀는 현상이 있는데,이것을 보정하기 위해 여러번 값을 누적한 후,평균값을 내어 신뢰할 수 있는 먼지밀도를 구합니다.*/void calcDustDensity() {duration = pulseIn(DUST_PIN, LOW);lowpulseoccupancy = lowpulseoccupancy + duration;if ((millis() - starttime) > sampletime_ms) {DustCalculate_RUN = false;DustCalculate_Done = true;ratio = lowpulseoccupancy / (sampletime_ms * 10.0); // Integer percentage 0=>100concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curvedustDensity = concentration * 100 / 13000;lowpulseoccupancy = 0;if (dustDensity > 150)buzzer_count = 3;else if (dustDensity > 80) buzzer_count = 2;else if (dustDensity > 30)buzzer_count = 1;else buzzer_count = 0;dustState = buzzer_count;}}어떤 함수와 루프함수에 어떤것을 넣어야 할지 모르겠습니다. ㅜㅜL298N NEMA17 을 사용하였습니다.
댓글 2
조회수 10,516master님의 댓글
master 작성일
L298 스텝모터 예제는 쉽게 찾을 수 있으니 웹검색하시고
if (dustDensity > 150)buzzer_count = 3;
else if (dustDensity > 80) buzzer_count = 2;
else if (dustDensity > 30)buzzer_count = 1;
else buzzer_count = 0;
상기 buzzer_count 변수에 값을 저장하는 것처럼 if문을 작성하면 됩니다.
예를들어서 dustDensity 50이상인 경우 정회전, 그 이하면 역회전 한다고하면
if (dustDensity > 50) 모터정회전
else 모터역회전
무야홍님의 댓글
무야홍
if (dustDensity > 50) 모터정회전
else 모터역회전 만든 변수를 void loop 아래에 넣으면 될까요??