I am building a project where I segregate different types of wastes (Metal, glass and plastic). I do this by using sensors to detect the type of the waste and then rotating the appropriate bin to collect the waste. There are three types of wastes and hence there are 3 bins; namely: Metal, Plastic and Glass. The problem I am facing is that the motors are not rotating. The detection part is fine; just the motors refuse to rotate. I rotated the motor with a simple code and hence I am sure the motor is fine. I am using an Adafruit Motor Shield to work with Arduino. Below is my code:
/* Basically, there is an incline on which the waste rolls down
before falling into a bin. In the code, m is Metal, g is Glass
and p is Plastic. I have assumed that the system always starts off
with the glass bin under the incline. Also, Pins 3 & 5 are used for
detecting glass, pin 4 is used to detect Metal and if none of the
sensors are actuated then the waste is plastic. There is an IR sensor
at the end of the incline to detect the arrival of waste. Therefore
when the IR sensor activates without any other sensor being activated,
the waste is plastic. The sensors follow inverse logic, so when
the sensors are actuated, they actually sens a LOW signal. LED connections
can be ignored. The Metal sensor is attached first followed by the sensors
for detecting glass.
*/
#include <AFMotor.h>
AF_DCMotor motor_plate(4);
int val;
char prev_state;
char current_state = 'g';
void setup() {
Serial.begin(9600);
motor_plate.setSpeed(100);
motor_plate.run(RELEASE);
pinMode(7,INPUT); //IR Sensor
pinMode(5, INPUT_PULLUP); //Proximity Sensor for glass detection
pinMode(3,INPUT_PULLUP); // Proximity Sensor for glass detection
pinMode(4,INPUT_PULLUP); // Proximity Sensor for metal detection
pinMode(10, OUTPUT); // LED COnnection for Glass
pinMode(12, OUTPUT); // LED COnnection for Metal
pinMode(11, OUTPUT); // LED COnnection for Plastic
}
void loop(){
Serial.println('Current State');
Serial.println(current_state);
Serial.println('Previous State=');
Serial.println(prev_state);
if (digitalRead(4) == HIGH){
digitalWrite(12,LOW);
if (digitalRead(5) == LOW || digitalRead(3) == LOW){
digitalWrite(10,HIGH);
delay(3000);
digitalWrite(10,LOW);
prev_state = current_state;
current_state = 'g';
if (prev_state == 'p'){
motor_plate.run(FORWARD);
delay(200);
motor_plate.run(RELEASE);
}
else if (prev_state == 'm'){
motor_plate.run(BACKWARD);
delay(200);
motor_plate.run(RELEASE);
}
else{
motor_plate.run(RELEASE);
}
}
else{
digitalWrite(10,LOW);
if (digitalRead(7) == HIGH){
digitalWrite(11,HIGH);
delay(3000);
digitalWrite(11,LOW);
prev_state = current_state;
current_state = 'p';
if (prev_state == 'm'){
motor_plate.run(FORWARD);
delay(200);
motor_plate.run(RELEASE);
}
else if (prev_state =='g'){
motor_plate.run(BACKWARD);
delay(200);
motor_plate.run(RELEASE);
}
else{
motor_plate.run(RELEASE);
}
}
}
}
else{
digitalWrite(12, HIGH);
delay(3000);
digitalWrite(12,LOW);
prev_state = current_state;
current_state = 'm';
if (prev_state == 'g'){
motor_plate.run(FORWARD);
delay(200);
motor_plate.run(RELEASE);
}
else if (prev_state == 'p'){
motor_plate.run(BACKWARD);
delay(200);
motor_plate.run(RELEASE);
}
else{
motor_plate.run(RELEASE);
}
}
}