You need to enable javaScript to run this app.

SAMPAH LAGI OTOMATIS

  • Jum'at, 22 November 2024
  • Elektronika
  • Administrator
  • 0 komentar

KODING:

//kotak sampah otomatis
// https://www.youtube.com/@hendrykurniawan
#include <Servo.h>
#define ECHO_PIN 8
#define TRIG_PIN 9
#define  PIN_SERVO A0
Servo myservo;
float distance;

void setup()
{
  myservo.attach(PIN_SERVO);
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  myservo.write(0);
  delay(500);
}

void loop()
{
  //Tutup terbuka cepat, tunda beberapa saat, lalu tutup tertutup dengan lambat
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  int duration = pulseIn(ECHO_PIN, HIGH);
  float distance = duration * 0.034 / 2;

  Serial.print("Jarak: ");
  Serial.print(distance);
  Serial.println(" cm");
 
  if(distance<=30)
  {
    //servoSpeed(Servo servo, int sudut, uint8_t Speed)
    servoSpeed(myservo, 110, 255);  //100 derajat, speed full 255
    Serial.println("Kondisi Kotak Sampah Terbuka");
    delay(3000);
    servoSpeed(myservo, 0, 100);  //0 derajat, speeed lambat 100
    Serial.println("Kondisi Kotak Sampah Tertutup1");
    servoSpeed(myservo, 0, 0);  //stop
    Serial.println("standbye1");
    delay(500);
  }

  else
  {
    servoSpeed(myservo, 0, 0);  //Stop
    Serial.println("standbye2");
    delay(500);
  }
 
 
}

void servoSpeed(Servo servo, int sudut, uint8_t Speed)
{
  if(Speed == 0)
  {
    return;
  }
  int posisiSekarang = servo.read();
  for (int i = posisiSekarang; i != sudut; (posisiSekarang > sudut) ? i-- : i++)
  {
    servo.write(i);
    if (Speed > 174)
    {
      delayMicroseconds((256 - Speed) * 200);//0.12 detik/60 derajat
    }
    else
    {
      delay((uint16_t)(256 - Speed) * 0.2f);//0.12 detik/60 derajat
    }
  }
  servo.write(sudut);
}
 
DIAGRAM JSON :
{
  "version": 1,
  "author": "HENDRY KURNIAWAN",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 34.99, "left": -62.64, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo1", "top": 78.65, "left": 338.49, "attrs": {} },
    {
      "type": "wokwi-hc-sr04",
      "id": "ultrasonic1",
      "top": -143.2,
      "left": 330.38,
      "attrs": { "distance": "122" }
    }
  ],
  "connections": [
    [ "servo1:V+", "uno:5V", "red", [ "h-30.3", "v127.67", "h-12.87" ] ],
    [ "servo1:GND", "uno:GND.2", "black", [ "h-44.78", "v126.99", "h-158.68" ] ],
    [ "ultrasonic1:VCC", "uno:5V", "red", [ "v84.17", "h137.89", "v243.74", "h-442.37" ] ],
    [ "ultrasonic1:GND", "uno:GND.2", "black", [ "v69.62", "h124.4", "v266.29", "h-5.09" ] ],
    [ "servo1:PWM", "uno:A0", "green", [ "h-72.85", "v98.15", "h-118.58" ] ],
    [ "ultrasonic1:TRIG", "uno:9", "green", [ "v51.59", "h-307.32" ] ],
    [ "ultrasonic1:ECHO", "uno:8", "green", [ "v28.58", "h-307.21" ] ]
  ]
}
 
LIBRARIES :
Servo
HCSR04 ultrasonic sensor
Bagikan artikel ini:

Beri Komentar

Jajak Pendapat

PELAJARAN ELEKTRONIKA YANG PALING ANDA SUKAI ADALAH ?

Hasil
Banner