program help

Hi I'am new to here so hope I post it properly.Not trying to upset anybody.My project is a can crusher.It is driven by a 180w 12volt motor.But having problems with the program.Have tried everything and spent many a late night trying to get it right.Hopping somebody else might be able to see what I am doing wrong.It is probably the most simple thing. Basically it does work except when the motor is reversing the gate's open again before the motor finishes returning all the way.This allows the next can to load before the motor is fully returned.

define MRELAY1 8//This is the first gate relay

define MRELAY2 2//This is for second gate relay

define MRELAY3 4//This operates a relay switching earth for the motor

define MRELAY4 3//To switch motor direction reverse

define MRELAY5 10//To switch motor direction reverse

define REED1 7//This is to stop the motor in the reverse direction

const int InfraredSensorPin = 11;//This senses a can in the chute const int MRELAY = 8; int val2 = 0;

void setup() { pinMode(MRELAY1, OUTPUT); pinMode(MRELAY2, OUTPUT); pinMode(MRELAY3, OUTPUT); pinMode(MRELAY4, OUTPUT); pinMode(MRELAY5, OUTPUT); pinMode(InfraredSensorPin, INPUT); pinMode(REED1, INPUT);

}

void loop() { if(digitalRead(InfraredSensorPin) == LOW); { delay(3000); digitalWrite(MRELAY1, HIGH); delay(2000); digitalWrite(MRELAY1, LOW); delay(2000); digitalWrite(MRELAY5, HIGH); delay(2000); digitalWrite(MRELAY5, LOW); delay(3000); digitalWrite(MRELAY2, HIGH); delay(10000); digitalWrite(MRELAY2, LOW); delay(3000); digitalWrite(MRELAY2, HIGH); digitalWrite(MRELAY4, HIGH); digitalWrite(MRELAY3, HIGH); delay(4000); }

val2 = digitalRead(REED1);

if (val2 == HIGH){ delay(500); digitalWrite(MRELAY3, LOW); digitalWrite(MRELAY4, LOW); digitalWrite(MRELAY2, LOW);

}else{ digitalWrite(MRELAY1, LOW); }

} Thank you any help you my be able to give. Regards Grant

Basically it does work except when the motor is reversing the gate’s open again before the motor finishes returning all the way.

Names like MRELAY1, MRELAY2, MRELAY3, MRELAY4, and MRELAY5 do not help when writing/debugging code. Names like gateRelay1, gateRelay2, groundRelay, forwardRelay, and reverseRelay mean that I don’t need to continually look back at the comments to see that does what.

const int MRELAY = 8;

No clue what this is for.

int val2 = 0;

There’s no val1, so why is there a val2? Why is it global?

if(digitalRead(InfraredSensorPin) == LOW);
Rarely a good idea.

Thanks for advice will take your advice and change the naming of relay's etc Regards Grant

PaulS: if(digitalRead(InfraredSensorPin) == LOW); Rarely a good idea.

What's wrong with that?

Sure, you and I would no doubt write it as if(!digitalRead(InfraredSensorPin)) {...}, but "if(...==LOW)" is easier to understand.

What's wrong with that?

The semicolon at the end?

Sure, you and I would no doubt write it as if(!digitalRead(InfraredSensorPin)) {...}, but "if(...==LOW)" is easier to understand.

You, maybe. Me? Never.