#include <OneWire.h>
#include <DallasTemperature.h>
int sensorPin = 2;
OneWire oneWire(sensorPin);
DallasTemperature sensor(&oneWire);
const int stepPin = 25;
const int dirPin = 26;
const int enPin = 27;
const int step = 200;
int LED_STOP = 5;
const int limit = 4;
int LED_SKY = 12;
int LED_GREEN = 13;
int LED_YELLOW = 14;
int LED_ORENGE = 33;
int LED_RED = 32;
int counter = 0;
int currentState = 0;
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
pinMode(limit, INPUT);
pinMode(LED_STOP, OUTPUT);
sensor.begin ();
pinMode(LED_SKY, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_ORENGE, OUTPUT);
pinMode(LED_RED, OUTPUT);
}
void loop() {
const int Limit = digitalRead(limit);
digitalWrite(dirPin, HIGH);
for (int i = 0; i < step; i++) { //รอบ*พัลส์ต่อรอบ
digitalWrite(stepPin, HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin, LOW);
delayMicroseconds(5000);
}
if ((Limit == HIGH)&&(Limit == 1)){
while((Limit == HIGH)){
motorstop();
readPMS5003();
}
}
}
void motorstop(){
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 0; i++) { //รอบ*พัลส์ต่อรอบ
digitalWrite(stepPin, HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin, LOW);
delayMicroseconds(5000);
}
//delay(1000);
}
void motor(){
{digitalWrite(dirPin, HIGH);
for (int i = 0; i < 200; i++) { //รอบ*พัลส์ต่อรอบ
digitalWrite(stepPin, HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin, LOW);
delayMicroseconds(5000);
}
}
}
void readPMS5003(){
sensor.requestTemperatures ();
float Temp = sensor.getTempCByIndex (0);
Serial.print ("Temperature: ");
Serial.print (Temp);
Serial.println ("ºC");
delay (1000);
digitalWrite(LED_STOP, HIGH);
//=============SKY================
if ((Temp >= 0)&&(Temp <=15 )) {
digitalWrite(LED_SKY, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_ORENGE, LOW);
digitalWrite(LED_RED, LOW);
}
//=============GREEN================
if ((Temp >= 16)&&(Temp <=25 )) {
digitalWrite(LED_SKY, LOW);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_ORENGE, LOW);
digitalWrite(LED_RED, LOW);
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 2*200; i++) { //รอบ*พัลส์ต่อรอบ
digitalWrite(stepPin, HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin, LOW);
delayMicroseconds(5000);
currentState = 1;
}
if(currentState >= 1){
currentState == 1;
motorstop();
Serial.println(currentState);
}
}
//============YELLOW================
if ((Temp >= 26)&&(Temp <= 37)) {
digitalWrite(LED_SKY, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_ORENGE, LOW);
digitalWrite(LED_RED, LOW);
}
//=============ORENGE================
if ((Temp >= 38)&&(Temp <= 75)) {
digitalWrite(LED_SKY, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_ORENGE, HIGH);
digitalWrite(LED_RED, LOW);
}
//==============RED===================
if (Temp >= 76) {
digitalWrite(LED_SKY, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_ORENGE, LOW);
digitalWrite(LED_RED, HIGH);
digitalWrite(dirPin, HIGH);
}
delay(10000);
}