I am making a can crusher.Which has a chute on top of it that loads one can at a time.I have got a program working in it.But the problem is it is fine when there is one can in the chute.But when you put two cans in it does not work correctly.When there are two cans in chute the first can drops in and motor drives forward,then stops as required,Motor starts in reverse but before it get to the reed switch.The gate opens for the can in the chute.Loading the next can before the motor has fully returned.Also the motor does not stop at all in reverse direction.
#define MRELAY1 8// Drives a relay to which operates a soleniod to open gate1
#define MRELAY2 2// Drives a relay to which operates a soleniod to open gate2
#define MRELAY3 4// This drives a soleniod to switch earth
#define MRELAY4 3// This relay is for forward/reverse of motor
#define MRELAY5 10// This relay is for forward/reverse of motor
#define SWITCH1 12// This is a infrared sensor to detect when a can is in chute
#define REED1 7// This is a reed switch to stop motor at a distance when in reverse
int val = 0;
int val2 = 0;
void setup() {
pinMode(MRELAY1, OUTPUT);
pinMode(MRELAY2, OUTPUT);
pinMode(MRELAY3, OUTPUT);
pinMode(MRELAY4, OUTPUT);
pinMode(MRELAY5, OUTPUT);
pinMode(SWITCH1, INPUT);
pinMode(REED1, INPUT);
}
void loop() {
val = digitalRead(SWITCH1);// Read infrared sensor
if (val == LOW) {
delay(50);// Wait
digitalWrite(MRELAY1, HIGH);// Open frist gate
delay(2000);// Wait 2 seconds
digitalWrite(MRELAY1, LOW);// Turn off first gate
delay(2000);// Wait 2 seconds
digitalWrite(MRELAY5, HIGH);// Open second gate
delay(2000);// Wait 2 seconds
digitalWrite(MRELAY5, LOW);// Turn off second gate
delay(3000);// Wait 3 seconds
digitalWrite(MRELAY2, HIGH);// Swicth earth relay to drive motor forward
delay(10000);// wait 10 seconds
digitalWrite(MRELAY2, LOW);// Turn off earth relay stop driving motor
delay(3000);// wait 3 seconds
digitalWrite(MRELAY2, HIGH);// Turn on eatrh relay
digitalWrite(MRELAY4, HIGH);// swicth relay over to reverse motor
digitalWrite(MRELAY3, HIGH);// swicth relay over to reverse motor
delay(4000);// wait 4 seconds
}
val2 = digitalRead(REED1);// read reed switch to stop motor reversing at a set point
if (val2 == HIGH){
delay(500);// wait half a second
digitalWrite(MRELAY3, LOW);// Turn off earth relay stop the motor reversing
digitalWrite(MRELAY4, LOW);// Turn off relay for reverse
digitalWrite(MRELAY2, LOW);// turn off relay for reverse
}else{
digitalWrite(MRELAY1, LOW);// Do not activate first gate
}
}
Please any assistance would be greatly appreciated
Grant
Moderator edit: [code] ... [/code] tags moved to be around the code. (Nick Gammon)