Pages: [1]   Go Down
Author Topic: program help  (Read 585 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

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
 
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50458
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
const int MRELAY = 8;
No clue what this is for.

Code:
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.


Logged

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

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

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 808
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50458
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What's wrong with that?
The semicolon at the end?

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

Pages: [1]   Go Up
Jump to: