# data[].attributes.body
import controlP5.*;

import processing.serial.*;

import cc.arduino.*;

import ddf.minim.*;

ControlP5 cp5;

Arduino arduino;

Minim minim;

AudioPlayer[] sounds = new AudioPlayer[10];

int a=2, b=3, c=4, d=5, e=6, f=7, g=8, dp=9;

int buttonPin = 12;

int count = 1;

boolean lastButtonState = false;

void setup() {

size(300, 400);

cp5 = new ControlP5(this);

cp5.addButton("RESET").setPosition(100, 350).setSize(100, 30);

arduino = new Arduino(this, "COM11", 57600);

int[] pins = {a, b, c, d, e, f, g, dp};

for (int i = 0; i < pins.length; i++) {

arduino.pinMode(pins[i], Arduino.OUTPUT);

arduino.digitalWrite(pins[i], Arduino.HIGH);

}

arduino.pinMode(buttonPin, Arduino.INPUT);

minim = new Minim(this);

for (int i = 1; i <= 9; i++) {

sounds[i] = minim.loadFile("t" + i + ".mp3");

}

showDigit(count);

}

void draw() {

boolean buttonState = arduino.digitalRead(buttonPin) == Arduino.HIGH;

if (buttonState && !lastButtonState) {

count++;

if (count > 9) count = 1;

showDigit(count);

if (sounds[count] != null) {

sounds[count].rewind();

sounds[count].play();

}

delay(300);

}

lastButtonState = buttonState;

}

void controlEvent(ControlEvent theEvent) {

if (theEvent.getController().getName().equals("RESET")) {

count = 1;

showDigit(count);

}

}

void showDigit(int num) {

switch (num) {

case 1: showChar(false,true,true,false,false,false,false); break;

case 2: showChar(true,true,false,true,true,false,true); break;

case 3: showChar(true,true,true,true,false,false,true); break;

case 4: showChar(false,true,true,false,false,true,true); break;

case 5: showChar(true,false,true,true,false,true,true); break;

case 6: showChar(true,false,true,true,true,true,true); break;

case 7: showChar(true,true,true,false,false,false,false); break;

case 8: showChar(true,true,true,true,true,true,true); break;

case 9: showChar(true,true,true,true,false,true,true); break;

default: showChar(false,false,false,false,false,false,false); break;

}

}

void showChar(boolean aS, boolean bS, boolean cS, boolean dS, boolean eS, boolean fS, boolean gS) {

arduino.digitalWrite(a, aS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(b, bS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(c, cS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(d, dS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(e, eS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(f, fS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(g, gS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(dp, Arduino.HIGH);

}

# data[].attributes.body
import controlP5.*;

import processing.serial.*;

import cc.arduino.*;

import ddf.minim.*;

ControlP5 cp5;

Arduino arduino;

Minim minim;

AudioPlayer[] sounds = new AudioPlayer[10];

int a = 2, b = 3, c = 4, d = 5, e = 6, f = 7, g = 8, dp = 9;

int count = 1;

void setup() {

size(300, 400);

cp5 = new ControlP5(this);

cp5.addButton("NEXT").setPosition(50, 150).setSize(80, 40);

cp5.addButton("RESET").setPosition(170, 150).setSize(80, 40);

arduino = new Arduino(this, "COM8", 57600);

int[] pins = {a, b, c, d, e, f, g, dp};

for (int i = 0; i < pins.length; i++) {

arduino.pinMode(pins[i], Arduino.OUTPUT);

arduino.digitalWrite(pins[i], Arduino.HIGH);

}

minim = new Minim(this);

for (int i = 1; i <= 9; i++) {

sounds[i] = minim.loadFile("t" + i + ".mp3");

}

showDigit(count);

}

void draw() {

background(0);

fill(255);

textSize(20);

textAlign(CENTER);

text("Current: " + count, width / 2, 100);

}

void controlEvent(ControlEvent theEvent) {

String name = theEvent.getController().getName();

if (name.equals("NEXT")) {

count++;

if (count > 9) count = 1;

showDigit(count);

if (sounds[count] != null) {

sounds[count].rewind();

sounds[count].play();

}

} else if (name.equals("RESET")) {

count = 1;

showDigit(count);

if (sounds[count] != null) {

sounds[count].rewind();

sounds[count].play();

}

}

}

void showDigit(int num) {

switch (num) {

case 1: showChar(false,true,true,false,false,false,false); break;

case 2: showChar(true,true,false,true,true,false,true); break;

case 3: showChar(true,true,true,true,false,false,true); break;

case 4: showChar(false,true,true,false,false,true,true); break;

case 5: showChar(true,false,true,true,false,true,true); break;

case 6: showChar(true,false,true,true,true,true,true); break;

case 7: showChar(true,true,true,false,false,false,false); break;

case 8: showChar(true,true,true,true,true,true,true); break;

case 9: showChar(true,true,true,true,false,true,true); break;

default: showChar(false,false,false,false,false,false,false); break;

}

}

void showChar(boolean aS, boolean bS, boolean cS, boolean dS, boolean eS, boolean fS, boolean gS) {

arduino.digitalWrite(a, aS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(b, bS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(c, cS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(d, dS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(e, eS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(f, fS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(g, gS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(dp, Arduino.HIGH);

}

# data[].attributes.body
#include <Wire.h>

#include <DHT.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define DHTPIN 26

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

#define OLED_RESET -1

#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int set_t = 26;

void setup() {

Serial.begin(115200);

Wire.begin(21, 22);

if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {

Serial.println(F("SSD1306 allocation failed"));

while (true);

}

dht.begin();

}

void loop() {

float h = dht.readHumidity();

float t = dht.readTemperature();

if (isnan(h) || isnan(t)) return;

display.clearDisplay();

display.setTextColor(WHITE, BLACK);

display.drawLine(64, 0, 64, 48, WHITE);

display.drawLine(0, 15, 128, 15, WHITE);

display.drawLine(0, 50, 128, 50, WHITE);

display.setCursor(23, 0);

display.setTextSize(1);

display.println("TEM");

display.setCursor(89, 0);

display.println("HUM");

display.setCursor(15, 25);

display.print(t);

display.setCursor(50, 25);

display.println("*c");

display.setCursor(83, 25);

display.print(h);

display.setCursor(120, 25);

display.println("%");

display.setCursor(0, 55);

display.println("SET");

display.setCursor(64, 60);

display.setTextSize(1);

display.display();

delay(2000);

}

# data[].attributes.body
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
#define sw1 7
#define sw2 8
DHT dht(DHTPIN, DHTTYPE);
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int x;
int set_t=26;
void setup() {
  Serial.begin(9600);
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  dht.begin();
  pinMode(sw1, INPUT);
  pinMode(sw2, INPUT);
}
void loop(){
  int st1 =digitalRead(sw1);delay(200);
  int st2 =digitalRead(sw2);delay(200);
  display.clearDisplay();
  display.setTextColor(WHITE,BLACK);
  display.drawLine(64,0,64,48,WHITE);
  display.drawLine(0,15,128,15,WHITE);
  display.drawLine(0,50,128,50,WHITE);
  display.setTextColor(WHITE,BLACK);
  display.setCursor(23,0);
  display.setTextSize(1);
  display.println("TEM");
  display.setCursor(89,0);
  display.println("HUM");
  display.setCursor(50,25);
  display.println("*c");
  display.setCursor(120,25);
  display.println("%");
  display.setCursor(0,55);
  display.println("SET");
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  display.setCursor(15,25);
  display.setTextSize(1);
  display.print(t);
  display.setCursor(83,25);
  display.setTextSize(1);
  display.print(h);
  display.setCursor(64,60);
  display.setTextSize(1);
  if(st1==0){
  x++;
  display.println(x);
}
  if(st2==0){
  x--;
  display.println(x);
  }
  display.display();
  delay(200);
}

# data[].attributes.headline
ขอcode arduino esp32 ส่งค่าระยะทางขอ Sensor ขึ้น Blynk

# data[].attributes.subject
ขอcode arduino esp32 ส่งค่าระยะทางขอ Sensor ขึ้น Blynk

# data[].attributes.body
#define BLYNK_TEMPLATE_ID "เอาของมึงมาใส่"
#define BLYNK_TEMPLATE_NAME "เอาของมึงมาใส่"
#define BLYNK_AUTH_TOKEN "เอาของมึงมาใส่"

#include <Wire.h>
#include <DHT.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

char ssid[] = "electronic24";
char pass[] = "1234567890";

#define DHTPIN 26
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

float t, h;

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  dht.begin();
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    while (true);
  }
}

void loop() {
  Blynk.run();

  h = dht.readHumidity();
  t = dht.readTemperature();
  if (isnan(h) || isnan(t)) return;

  Blynk.virtualWrite(V14, t);
  Blynk.virtualWrite(V15, h);

  display.clearDisplay();
  display.setTextColor(WHITE, BLACK);
  display.drawLine(64, 0, 64, 48, WHITE);
  display.drawLine(0, 15, 128, 15, WHITE);
  display.drawLine(0, 50, 128, 50, WHITE);

  display.setCursor(23, 0);
  display.setTextSize(1);
  display.println("TEM");
  display.setCursor(89, 0);
  display.println("HUM");

  display.setCursor(15, 25);
  display.print(t);
  display.setCursor(50, 25);
  display.println("*c");

  display.setCursor(83, 25);
  display.print(h);
  display.setCursor(120, 25);
  display.println("%");

  display.setCursor(0, 55);
  display.println("SET");

  display.setCursor(64, 60);
  display.setTextSize(1);
  display.display();

  delay(2000);
}

# data[].attributes.body
import processing.serial.*;

Serial myPort;

Button btn1, btn2, btn3;

void setup() {

size(400, 200);

String portName = "COM4";

myPort = new Serial(this, portName, 9600);

btn1 = new Button(50, 100, 80, 40, "LED1", '1');

btn2 = new Button(150, 100, 80, 40, "LED2", '2');

btn3 = new Button(250, 100, 80, 40, "OFF", '0');

}

void draw() {

background(220);

textSize(20);

text("ควบคุมไฟ LED", 120, 40);

btn1.display();

btn2.display();

btn3.display();

}

void mousePressed() {

if (btn1.isPressed(mouseX, mouseY)) {

myPort.write(btn1.command);

}

if (btn2.isPressed(mouseX, mouseY)) {

myPort.write(btn2.command);

}

if (btn3.isPressed(mouseX, mouseY)) {

myPort.write(btn3.command);

}

}

class Button {

int x, y, w, h;

String label;

char command;

Button(int x, int y, int w, int h, String label, char command) {

this.x = x;

this.y = y;

this.w = w;

this.h = h;

this.label = label;

this.command = command;

}

void display() {

fill(180);

rect(x, y, w, h);

fill(0);

textAlign(CENTER, CENTER);

text(label, x + w/2, y + h/2);

}

boolean isPressed(int mx, int my) {

return mx > x && mx < x + w && my > y && my < y + h;

}

}

# data[].attributes.headline
สอบ ตรง fill เอาไปเปลี่ยนสี มั่วเอาหรือไรก็ได้ ใครใช้เหลืองตามเป็นเก

# data[].attributes.subject
สอบ ตรง fill เอาไปเปลี่ยนสี มั่วเอาหรือไรก็ได้ ใครใช้เหลืองตามเป็นเก

# data[].attributes.body
import processing.dxf.*;

import controlP5.*;

import processing.serial.*;

import cc.arduino.*;

ControlP5 cp5;

Arduino arduino;

int a = 2, b = 3, c = 4, d = 5, e = 6, f = 7, g = 8, dp = 9;

int count = 0;

boolean segA, segB, segC, segD, segE, segF, segG;

void setup() {

size(300, 400);

cp5 = new ControlP5(this);

cp5.addButton("UP").setPosition(50, 300).setSize(80, 40);

cp5.addButton("DOWN").setPosition(170, 300).setSize(80, 40);

arduino = new Arduino(this, "COM8", 57600);

int[] pins = {a, b, c, d, e, f, g, dp};

for (int i = 0; i < pins.length; i++) {

arduino.pinMode(pins[i], Arduino.OUTPUT);

arduino.digitalWrite(pins[i], Arduino.HIGH);

}

showDigit(count);

}

void draw() {

background(0);

fill(255, 255, 0);

noStroke();

if (segA) rect(100, 30, 100, 10);

if (segB) rect(200, 40, 10, 100);

if (segC) rect(200, 150, 10, 100);

if (segD) rect(100, 250, 100, 10);

if (segE) rect(90, 150, 10, 100);

if (segF) rect(90, 40, 10, 100);

if (segG) rect(100, 140, 100, 10);

}

void controlEvent(ControlEvent theEvent) {

String name = theEvent.getController().getName();

if (name.equals("UP")) {

count++;

if (count > 9) count = 0;

showDigit(count);

} else if (name.equals("DOWN")) {

count--;

if (count < 0) count = 9;

showDigit(count);

}

}

void showDigit(int num) {

switch (num) {

case 0: showChar(true,true,true,true,true,true,false); break;

case 1: showChar(false,true,true,false,false,false,false); break;

case 2: showChar(true,true,false,true,true,false,true); break;

case 3: showChar(true,true,true,true,false,false,true); break;

case 4: showChar(false,true,true,false,false,true,true); break;

case 5: showChar(true,false,true,true,false,true,true); break;

case 6: showChar(true,false,true,true,true,true,true); break;

case 7: showChar(true,true,true,false,false,false,false); break;

case 8: showChar(true,true,true,true,true,true,true); break;

case 9: showChar(true,true,true,true,false,true,true); break;

default: showChar(false,false,false,false,false,false,false); break;

}

}

void showChar(boolean aS, boolean bS, boolean cS, boolean dS, boolean eS, boolean fS, boolean gS) {

arduino.digitalWrite(a, aS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(b, bS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(c, cS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(d, dS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(e, eS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(f, fS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(g, gS ? Arduino.LOW : Arduino.HIGH);

arduino.digitalWrite(dp, Arduino.HIGH);

segA = aS;

segB = bS;

segC = cS;

segD = dS;

segE = eS;

segF = fS;

segG = gS;

}

# data[].attributes.headline
งานกลุ่มปองกิจถังขยะ เวอร์ชั่น 1

# data[].attributes.subject
งานกลุ่มปองกิจถังขยะ เวอร์ชั่น 1

# data[].attributes.body
#define BLYNK_TEMPLATE_ID "TMPL6GywV4_bp"
#define BLYNK_TEMPLATE_NAME "Anon 2"
#define BLYNK_AUTH_TOKEN "lcQ-maobGNGm_gUbRbmvZC0g8XOVcQ0i"

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define TRIG 13
#define ECHO 12
#define LED_FULL 25  // ไฟระดับ 25%
#define LED2 26      // ไฟระดับ 50%
#define LED3 27      // ไฟระดับ 90%
#define BUZZER_PIN 15

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define TRASH_X 40
#define TRASH_Y 20
#define TRASH_W 48
#define TRASH_H 35

bool beep50Triggered = false;
bool beep90Triggered = false;

unsigned long previousMillis = 0;
const long updateInterval = 200;

// 🔧 ใส่ WiFi ของคุณตรงนี้
char ssid[] = "electronic24";
char pass[] = "1234567890";

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(LED_FULL, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  WiFi.begin(ssid, pass);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("OLED failed"));
    while (1);
  }

  display.clearDisplay();
  drawTrashCan(0);
  display.display();
}

void loop() {
  Blynk.run();  // ทำให้ Blynk ทำงานเสมอ

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= updateInterval) {
    previousMillis = currentMillis;

    // อ่านระยะจากเซนเซอร์
    digitalWrite(TRIG, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG, LOW);
    long duration = pulseIn(ECHO, HIGH);
    float distance = duration * 0.034 / 2;

    float emptyDistance = 28.0;
    float fullDistance = 5.0;
    float level = constrain((emptyDistance - distance) / (emptyDistance - fullDistance), 0.0, 1.0);
    int percent = level * 100;

    // ส่งค่าไปยัง Blynk (Virtual Pin V0)
    Blynk.virtualWrite(V1, percent);
   Blynk.virtualWrite(V5, (percent >= 25) ? 255 : 0);  // LED V2 = 25%
    Blynk.virtualWrite(V6, (percent >= 50) ? 255 : 0);  // LED V3 = 50%
    Blynk.virtualWrite(V7, (percent >= 90) ? 255 : 0);  // LED V4 = 90%

    // แจ้งเตือนระดับต่าง ๆ
    if (percent >= 90 && !beep90Triggered) {
      blinkAndSound(3, LED3);
      beep90Triggered = true;
      beep50Triggered = true;
    } else if (percent >= 50 && !beep50Triggered) {
      blinkAndSound(2, LED2);
      beep50Triggered = true;
    }

    // ไฟสถานะระดับ 25%
    digitalWrite(LED_FULL, (percent >= 25) ? HIGH : LOW);

    // ไฟติดค้างแบบ real-time
    digitalWrite(LED2, (percent >= 50) ? HIGH : LOW);
    digitalWrite(LED3, (percent >= 90) ? HIGH : LOW);

    // รีเซ็ตถ้าขยะต่ำกว่า 50%
    if (percent < 50) {
      beep50Triggered = false;
      beep90Triggered = false;
    }

    // แสดงผลใน Serial และ OLED
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.print(" cm | Trash Level: ");
    Serial.print(percent);
    Serial.println("%");

    display.clearDisplay();
    drawTrashCan(level);
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.print("Trash: ");
    display.print(percent);
    display.println("%");
    display.display();
  }
}

void blinkAndSound(int times, int ledPin) {
  for (int i = 0; i < times; i++) {
    digitalWrite(BUZZER_PIN, HIGH);
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(BUZZER_PIN, LOW);
    digitalWrite(ledPin, LOW);
    delay(150);
  }
}

void drawTrashCan(float fillLevel) {
  display.drawRoundRect(TRASH_X, TRASH_Y, TRASH_W, TRASH_H, 5, SSD1306_WHITE);

  display.fillRoundRect(TRASH_X - 6, TRASH_Y - 8, TRASH_W + 12, 10, 4, SSD1306_WHITE);
  display.fillRect(TRASH_X + 18, TRASH_Y - 14, 12, 6, SSD1306_WHITE);

  display.drawLine(TRASH_X + 12, TRASH_Y + 2, TRASH_X + 12, TRASH_Y + TRASH_H - 2, SSD1306_WHITE);
  display.drawLine(TRASH_X + 24, TRASH_Y + 2, TRASH_X + 24, TRASH_Y + TRASH_H - 2, SSD1306_WHITE);
  display.drawLine(TRASH_X + 36, TRASH_Y + 2, TRASH_X + 36, TRASH_Y + TRASH_H - 2, SSD1306_WHITE);

  int fillHeight = fillLevel * (TRASH_H - 2);
  int fillY = TRASH_Y + TRASH_H - 1 - fillHeight;
  display.fillRect(TRASH_X + 1, fillY, TRASH_W - 2, fillHeight, SSD1306_WHITE);
}