Go Down

Topic: Program reacts the wrong way (Read 589 times) previous topic - next topic

Grant1962

Nov 17, 2012, 02:49 am Last Edit: Nov 17, 2012, 03:49 am by Nick Gammon Reason: 1
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.

Code: [Select]
#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)


johnwasser

If you use meaningful names you quickly see that the comments for which pin does what disagrees with the comments that say what the code is trying to do:

Code: [Select]

#define OpenGate1 8// Drives a relay to which operates a soleniod to open gate1
#define OpenGate2 2// Drives a relay to which operates a soleniod to open gate2
#define MotorPower 4// This drives a soleniod to switch earth
#define MotorDirection 3// This relay is for forward/reverse of motor
#define MotorDirectionAlso 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(OpenGate1, OUTPUT);
  pinMode(OpenGate2, OUTPUT);
  pinMode(MotorPower, OUTPUT);
  pinMode(MotorDirection, OUTPUT);
  pinMode(MotorDirectionAlso, OUTPUT);
  pinMode(SWITCH1, INPUT);
  pinMode(REED1, INPUT);

}


void loop() {
  val = digitalRead(SWITCH1);// Read infrared sensor

  if (val == LOW) {
    delay(50);// Wait
    digitalWrite(OpenGate1, HIGH);// Open frist gate
    delay(2000);// Wait 2 seconds
    digitalWrite(OpenGate1, LOW);// Turn off first gate
    delay(2000);// Wait 2 seconds
    digitalWrite(MotorDirectionAlso, HIGH);// Open second gate
    delay(2000);// Wait 2 seconds
    digitalWrite(MotorDirectionAlso, LOW);// Turn off second gate
    delay(3000);// Wait 3 seconds
    digitalWrite(OpenGate2, HIGH);// Swicth earth relay to drive motor forward
    delay(10000);// wait 10 seconds
    digitalWrite(OpenGate2, LOW);// Turn off earth relay stop driving motor
    delay(3000);// wait 3 seconds
    digitalWrite(OpenGate2, HIGH);// Turn on eatrh relay
    digitalWrite(MotorDirection, HIGH);// swicth relay over to reverse motor
    digitalWrite(MotorPower, HIGH);// swicth relay over to reverse motor
    delay(4000);// wait 4 seconds
  }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

holmes4

You can't use delay() in a program like take a look at blink without delay and using FSM's (Finite State Machines).

Mark

Grant1962

Thanks Guy's for your reply's have been trying them, all has made no difference. May still doing it wrong but thanks.
Regards
Grant

PaulS

Quote
have been trying them, all has made no difference.

I've modified my code. It hasn't helped. Please guess what changes I've made and tell me what is still wrong.

Was that what you meant to say?

Grant1962

Ok I am new sorry for such simple mistake's.

Nick Gammon

OK, well read this: Read this before posting a programming question

It's reasonable to post your code if you are having problems with it, don't you think? New or not.
http://www.gammon.com.au/electronics

Go Up