Pages: [1]   Go Down
Author Topic: Program reacts the wrong way  (Read 481 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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)

« Last Edit: November 16, 2012, 09:49:44 pm by Nick Gammon » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8665
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
  }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2199
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: