Lolin32 lite with dust sensor

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

if I upload this code , it’s always high state … what’s the problem ??

#include <Wire.h>

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = “d1f33c6406bd4a3ea93d177a888295a4”; //토큰
char ssid[] = “AndroidHotspot8838”;
char pass[] = “1q2w3e4r5t”;

#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include “SSD1306Wire.h” // legacy include: #include "SSD1306.h"
// Initialize the OLED display using Wire library
SSD1306Wire display(0x3c, 19, 22); // SH1106 display(0x3c, sda, scl);
#define DEMO_DURATION 3000

typedef void (*Demo)(void);

int demoMode = 0;
int counter = 1;
int intialrun=0;

//=============================
int dustPin=15;
int dustPin2=12;

float dustVal=0;
float dustVal2=0;
float dustDensity = 0;
float dustDensity2 = 0;

int ledPower=13;
int ledPower2=17;
int delayTime=280;
int delayTime2=40;
float offTime=9680;

float voltage=0;
float voltage2=0;
//==========================button
int button_pin2 = 16; // 2,4,6,8 버튼
int button_pin4 = 4; // 2,4,6,8 버튼
int button_pin6 = 0; // 2,4,6,8 버튼
int button_pin8 = 2; // 2,4,6,8 버튼

int buttonstate2 = LOW; // 초기값
int buttonstate4 = LOW;
int buttonstate6 = LOW;
int buttonstate8 = LOW;

int reading2; // SW 상태
int reading4; // SW 상태
int reading6; // SW 상태
int reading8; // SW 상태

int previous2 = LOW; // SW 이전 상태
int previous4 = LOW; // SW 이전 상태
int previous6 = LOW; // SW 이전 상태
int previous8 = LOW; // SW 이전 상태

long time2 = 0; // LED가 ON/OFF 토글된 마지막 시간
long time4 = 0; // LED가 ON/OFF 토글된 마지막 시간
long time6 = 0; // LED가 ON/OFF 토글된 마지막 시간
long time8 = 0; // LED가 ON/OFF 토글된 마지막 시간

long debounce = 100; // Debounce 타임 설정

//=====================

//====================
//
//int red = 17;
//int green = 5;
//int blue= 18;
int ledpin[] = {12, 14, 27};
int freq=5000;
int ledChannel[]={0,1,2};
int resolution = 8;
int relay=18;

#define S_LOW 0
#define S_HIGH 8888
#define resolution 16
#define freq 50
#define S_CHANNAL 0

BLYNK_WRITE(V3)
{
int state = param.asInt();
ledcWrite(S_CHANNAL, state);
delay(100);
}

//BLYNK_WRITE(V8)
//{
// int state = param.asInt();
// ledcWrite(S_CHANNAL, state);
// delay(100);
//}

void sendSensor()
{
float c=dustDensity;
float d=dustDensity2;
Blynk.virtualWrite(V1, c);
Blynk.virtualWrite(V2, d);
}

void setup() {
Serial.begin(115200);
pinMode(ledPower,OUTPUT);
pinMode(ledPower2,OUTPUT);
pinMode(relay,OUTPUT);
pinMode(button_pin2,INPUT_PULLUP);
pinMode(button_pin4,INPUT_PULLUP);
pinMode(button_pin6,INPUT_PULLUP);
pinMode(button_pin8,INPUT_PULLUP);

for(int pin = 0; pin<3; pin++){
ledcSetup(ledChannel[pin], freq, resolution);
ledcAttachPin(ledpin[pin], ledChannel[pin]);
//ledcWrite(ledChannel[pin], 255);
}

Serial.print(“go”);
display.init();

display.clear();

display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);

ledcSetup(S_CHANNAL, freq, resolution);
ledcAttachPin(32, S_CHANNAL);

Blynk.begin(auth, ssid, pass);

ledcSetup(S_CHANNAL, freq, resolution);
ledcAttachPin(5, S_CHANNAL);

}

void username() { //username을 표시하는 OLED 중간함수
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
display.drawString(0, 0, “DKU”);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 20, “Mechatronics”);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 30, “32131936 KYD”);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 40, “32140876 KYJ”);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 50, “32142575 SJW”);
}

void dustlevel() { // 미세먼지 값을 OLED에 띄우기 위한 중간 함수
float d = dustDensity;
float e = dustDensity2;
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "NO.1 "+String(d)+“mg/m3”);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 30, "NO.2 "+String(e)+“mg/m3”);

return;

}

void modestae() { // 미세먼지 값을 OLED에 띄우기 위한 중간 함수
float d = buttonstate2;
Serial.print(d);

if (d == 1){
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, “Mode state”);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 11, “Auto”);
}
else{
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, “Mode state”);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 11, “Manual”);
}

return;

}

Demo demos[] = {username, dustlevel,modestae}; // Demo라는 변수에 demos라는 배열형 변수를 넣는다.
int demoLength = (sizeof(demos) / sizeof(Demo));
long timeSinceLastModeSwitch = 0;

void loop() {

Blynk.run();
//========================================

reading2 = digitalRead(button_pin2); // 2번 버튼의 상태를 입력한다
reading4 = digitalRead(button_pin4);// 4번 버튼의 상태를 입력한다
reading6 = digitalRead(button_pin6); // 6번 버튼의 상태를 입력한다
reading8 = digitalRead(button_pin8); // 8번 버튼의 상태를 입력한다

//=========
if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce) {
if (buttonstate2 == HIGH) // LED 가 HIGH 면 LOW 로 바꿔준다.
buttonstate2 = LOW;
else // LED 가 LOW 면 HIGH 로 바꿔준다.
buttonstate2 = HIGH;

time2 = millis();

}
Serial.print(buttonstate2);

//digitalWrite(led, buttonstate2);

previous2 = reading2;
//=========
if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce) {
if (buttonstate4 == HIGH) // LED 가 HIGH 면 LOW 로 바꿔준다.
buttonstate4 = LOW;
else // LED 가 LOW 면 HIGH 로 바꿔준다.
buttonstate4 = HIGH;

time4 = millis();

}
Serial.print(buttonstate4);

//digitalWrite(led, buttonstate2);

previous4 = reading4;
//=========
if (reading6 == HIGH && previous6 == LOW && millis() - time6 > debounce) {
if (buttonstate6 == HIGH) // LED 가 HIGH 면 LOW 로 바꿔준다.
buttonstate6 = LOW;
else // LED 가 LOW 면 HIGH 로 바꿔준다.
buttonstate6 = HIGH;

time6 = millis();

}
Serial.print(buttonstate6);

//digitalWrite(led, buttonstate2);

previous6 = reading6;
//=========

if (reading8 == HIGH && previous8 == LOW && millis() - time8 > debounce) {
if (buttonstate8 == HIGH)    // LED 가 HIGH 면 LOW 로 바꿔준다.
  buttonstate8 = LOW;
else                  // LED 가 LOW 면 HIGH 로 바꿔준다.
  buttonstate8 = HIGH;

time8 = millis();

}
Serial.print(buttonstate8);

//digitalWrite(led, buttonstate2);

previous8 = reading8;
//=========
// Serial.print(buttonstate2);
// Serial.print(buttonstate4);
// Serial.print(buttonstate6);
// Serial.print(buttonstate8);

//========================================
switch (buttonstate2){

case LOW: // Manual mode
Serial.print(“low”);

  switch (buttonstate4){
  case HIGH:
    digitalWrite(relay,HIGH);
    Serial.print("relay");
      break;
  case LOW:
  digitalWrite(relay,LOW);
  break;
  }

 
 
  
break;

case HIGH:
// Auto mode

Serial.print(“high”);

break;

}

//ledcWrite(S_CHANNAL, 4444);
//delay(50);

//for (int i = 0; i<333; i++){
// ledcWrite(S_CHANNAL, i);
// delay(50);
// Serial.print(i);
//}
//========================================

// ledPower is any digital pin on the arduino connected to Pin 3 on the sensor
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal=analogRead(dustPin); // read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(offTime);

//======================================

digitalWrite(ledPower2,LOW); // power on the LED
delayMicroseconds(delayTime);
dustVal2=analogRead(dustPin2); // read the dust value via pin 5 on the sensor
delayMicroseconds(delayTime2);
digitalWrite(ledPower2,HIGH); // turn the LED off
delayMicroseconds(offTime);

  Serial.print("------");
  Serial.print(dustVal);
  Serial.print("------");
  Serial.print(dustVal2);
  Serial.print("------");

delay(1000);

dustDensity = ((dustVal-755.35)/5.66)+10;
dustDensity2 =(dustVal2-755.35)/5.66;
Serial.print("Dust density 1(mg/m3) = “);
Serial.print(dustDensity);
Serial.print(” Dust density 2(mg/m3) = ");
Serial.println(dustDensity2);

if(dustDensity2<=75.0){ // 좋음,파란색
ledcWrite(ledChannel[0] ,0);
ledcWrite(ledChannel[1] ,0);
ledcWrite(ledChannel[2] ,255);
Serial.println(" blue");

}
else if(75.0<dustDensity2&&dustDensity2<=150.0){ // 보통,녹색
ledcWrite(ledChannel[0] ,0);
ledcWrite(ledChannel[1] ,255);
ledcWrite(ledChannel[2] ,0);
Serial.println(" green");

}
else if(150.0<dustDensity2&&dustDensity2<=200.0){ // 나쁨,노란색
ledcWrite(ledChannel[0] ,255);
ledcWrite(ledChannel[1] ,155);
ledcWrite(ledChannel[2] ,0);
Serial.println(" yellow");

}
else{ // 매우 나쁨,빨간색
ledcWrite(ledChannel[0] ,255);
ledcWrite(ledChannel[1] ,0);
ledcWrite(ledChannel[2] ,0);
Serial.println(" red");

}

//======================================================display
// clear the display
//display.clear();
display.clear();
// draw the current demo method

if (intialrun ==0){

display.clear();
// draw the current demo method
demos1;

display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(10, 128, String(millis()));
// write the buffer to the display
display.display();
Serial.println(“run”);
Serial.println(intialrun);
}

demosdemoMode;

display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.drawString(10, 128, String(millis()));
// write the buffer to the display
display.display();

if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
demoMode = (demoMode + 1) % demoLength;
timeSinceLastModeSwitch = millis();
}

counter++;
intialrun++;
delay(100);

//========================================display

}

I guess you didn’t bother reading that?

Pete.

1 Like

yes this code is unreadable.
oh my headache !

Mmm if you are using a dust sensor and allways is high… maybe the sketch is telling you that something is with dust… maybe your post XD

I hope someone can answer you… but… with the things you posted… you never will get the answer that you request… i can’t talk for all of the people of this community… but at least me… i’ll never debug your code… is too large even for read it one time…and i’m not a fulltime code debugger… try to simplify your code, make the problem a group of little problems and then try to fix each one… one by one…

Is like you post the program of Apollo 13 and ask us where is the problem? if we didn’t see the film (trust me… is more easy remember the “thanks guy” film that know the history itself) we can’t answer it…

2 Likes