2

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;
}
10
  • You'll need to tell us more than "something is wrong with my code" - what specifically doesn't work the way you expected it to? What have you tried yourself? How is your project connected to your Arduino? It would also be very helpful if you would format your code by highlighting it and clicking the "{}" code-formatting symbol. It's pretty hard to read right now. Commented Apr 8, 2016 at 12:17
  • 1
    Hint: Check statement "int mtr_time = 10 ;" in blocks. How does that work according to C/C++? Commented Apr 8, 2016 at 13:04
  • 1
    okay if i remove that code, does the motor start spin in reverse direction ?? Commented Apr 8, 2016 at 13:09
  • 1
    I've reformatted your code to make it more readable. Two notes: 1. mtr_time_rev = mtr_time_rev - 1; can be shortened to mtr_time_rev--; and 2. Why are the following lines put into a block? Commented Apr 8, 2016 at 22:27
  • 1
    guys thanks for your suggestions, i have added for loop for reversing the motor upto a value :) Commented Apr 19, 2016 at 9:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.