0% found this document useful (0 votes)
2 views

Arduino Code Smart Bin

The document contains Arduino code for a smart bin project that utilizes a servo motor and ultrasonic sensor to measure distance. It sets up the necessary pins and reads the distance to control the servo's position based on the measured distance. The code includes a delay mechanism to adjust the servo's response time depending on the distance detected.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arduino Code Smart Bin

The document contains Arduino code for a smart bin project that utilizes a servo motor and ultrasonic sensor to measure distance. It sets up the necessary pins and reads the distance to control the servo's position based on the measured distance. The code includes a delay mechanism to adjust the servo's response time depending on the distance detected.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Arduino Code Smart Bin

- January 07, 2023

//**************************Written by Simple Circuits*****************************//

#include<Servo.h>

const int trigPin = A2;

const int echoPin = A1;

const int servoPin = A0;

// defines variables

double SetDelay, Input, Output, ServoOutput;

Servo myServo; //Initialize Servo.

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output


pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

myServo.attach(servoPin); //Attach Servo

Input = readPosition();

void loop() {

Input = readPosition();

ServoOutput=Input;

myServo.write(ServoOutput);

if(ServoOutput ==140)

SetDelay=3000;

else{

SetDelay=100;

delay(SetDelay);

float readPosition() {

delay(40);
long duration, var;

int distance;

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance = duration/(29*2);

// Prints the distance on the Serial Monitor

Serial.print("Distance: ");
Serial.println(distance);

if(distance <68) {

var=140;

else{

var=50;

//Returns distance value.

return var;

Comments

Popular posts from this blog

Arduino Code Car Parking System

- March 07, 2024

// Created by Simple Circuits #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C


lcd(0x27,16,2); #include <Servo.h> Servo myservo; int IR1 = 2; int IR2 = 3; int Slot = 4; //Total
number of parking Slots int flag1 = 0; int flag2 = 0; void setup() { Serial.begin(9600); lcd.init();
//initialize the lcd lcd.backlight(); //open the backlight pinMode(IR1, INPUT); pinMode(IR2,
INPUT); myservo.attach(4); myservo.write(100); lcd.setCursor (0,0); lcd.print(" ARDUINO ");
lcd.setCursor (0,1); lcd.print(" PARKING SYSTEM "); delay (2000); lcd.clear(); } void loop(){
if(digitalRead (IR1) == LOW && flag1==0){ if(Slot>0){flag1=1; if(flag2==0){myservo.write(0); Slot =
Slot-1;} }else{ lcd.setCursor (0,0); lcd.print(" SORRY :( "); lc...

Read more

Arduino Code

- February 16, 2024

//define Pins #include <Servo.h> Servo servo; int trigPin = 11; int echoPin = 12; // defines variables
long duration; int distance; void setup() { servo.attach(13); servo.write(180); delay(2000); //
Sets the trigPin as an Output pinMode(trigPin, OUTPUT); // Sets the echoPin as an Input
pinMode(echoPin, INPUT); } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW);
delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin,
HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound
wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance
distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: ");
Serial.println(distance); if ( distance <= 25 ) // Change Distance according to Ultrasonic Sensor
Placement { servo.write(180); delay(3000); ...
Read more

Circuit Diagram

- February 16, 2024

You might also like