I am making a smart bin which automatically sense (with the help of IR sensor ) and dispose the trash by moving with help of DC geared motor 12V 60 RPM and it also detects for obstacles with the help of ultrasonic sensor which is placed at the front of the bin. Once the bin is empty the motor has to spin in the reverse direction. My questions is that there is something wrong in my code and am not able to solve it.
The issue that I am having is that when I run the code the bin was perfectly working as checking whether the bin is empty or not at the start and check for obstacles. If both are clear it will start move and whenever it detects any obstacles, the bin will stop the motor and checks again if any obstacles present, else it will start move and dispose the trash from the bin. The problem is when I code to reverse the motor direction to bring back the bin to original state the relay in relay board making a trrrrr sound.
Project technical specifications:
- Arduino Uno
- 60 RPM 12V DC geared motor
- 4 channel relay board with 2 relay active for two motors
- Ultrasonic sensor
- Infrared module
- Plunger to open the door placed inside
My sketch:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(10, 11, 5, 4, 3, 2);
int motor_fwd = 7;
int motor_rev = 8;
int plung_rly = 6;
int irsensor = 9;
const int trigPin = 13;
const int echoPin = 12;
int mtr_time = 10;
int mtr_time_rev = 1;
void setup()
{
digitalWrite(motor_fwd, LOW);
digitalWrite(motor_rev, LOW);
digitalWrite(plung_rly, LOW);
digitalWrite(irsensor, LOW);
// initialize the relay1 pin as an output;
pinMode(motor_fwd, OUTPUT); // initialize the relay2 pin as an output;
pinMode(motor_rev, OUTPUT); // pinMode(irsensor, INPUT);
pinMode(plung_rly, OUTPUT);
}
void loop()
{
irsen_chk_low();
delay(500);
irsen_chk_high();
delay(500);
}
void irsen_chk_low()
{
if (digitalRead(irsensor) == LOW) {
lcd.begin(16, 2); // Print a message to the LCD.
lcd.print("bin Empty");
delay(500);
digitalWrite(motor_fwd, LOW);
if (mtr_time_rev >= 1) {
mtr_time_rev = mtr_time_rev - 1;
{
digitalWrite(motor_rev, HIGH);
delay(5000);
digitalWrite(motor_rev, LOW);
delay(5000);
int mtr_time = 10;
}
}
}
}
void irsen_chk_high()
{
if (digitalRead(irsensor) == HIGH) {
lcd.begin(16, 2); // Print a message to the LCD;
lcd.print("Bin is full");
obs_detection();
delay(500);
digitalWrite(motor_fwd, HIGH);
}
}
void obs_detection()
{
long duration, inches;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
lcd.begin(16, 2);
lcd.print(inches);
delay(100);
if (inches <= 10) {
digitalWrite(motor_fwd, LOW);
delay(200);
} else {
if (mtr_time >= 1) {
mtr_time = mtr_time - 1;
digitalWrite(motor_fwd, HIGH);
delay(200);
} else {
int mtr_time = 10;
}
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
mtr_time_rev = mtr_time_rev - 1;
can be shortened tomtr_time_rev--;
and 2. Why are the following lines put into a block?