Can not get code to read switches

Hi everyone
I have been working on this project for a while now but just cant get it working properly. Basically its a can crusher driven by a 12 volt motor driving a piston up and down a tube.There are two solenoids that feed the can into the tube.The piston moves down crushing the can and the returns to reload the next can.I have got it working with a delay function for the piston travel.But it keeps going out of timing.I have added to switch to try too switch of the relay’s when the piston hits them but to no luck.Hope somebody can help me. Thankyou

[quote]

[/quote// Can crusher:control program 2
// Progarm loaded 01/07/2013
// This is the lastest progem

#define MRELAY1 8 
#define MRELAY2 2 
#define MRELAY3 4
#define MRELAY4 3
#define MRELAY5 10
#define SWITCH1 12
#define SWITCH2 7
#define SWITCH3 6
int val = 0;
int val1 = 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(SWITCH2, INPUT);
  pinMode(SWITCH3, INPUT);
  
  
  
}


void loop() {
  val = digitalRead(SWITCH1); // ir switch for can hopper
  val1 = digitalRead(SWITCH2); // return cut off switch
  val2 = digitalRead(SWITCH3); // forward cut off switch
  delay(1000);
  
  if (val == LOW) {
    delay(1000);
    digitalWrite(MRELAY1, HIGH); // open soleniod for gate 1
    delay(2000);
    digitalWrite(MRELAY1, LOW);  //  close soleniod for gate 1
    delay(2000);
    digitalWrite(MRELAY5, HIGH); // open soleniod for gate 2
    delay(2000);
    digitalWrite(MRELAY5, LOW);  // close soleniod for gate 2
    delay(2000);
    digitalWrite(MRELAY2, HIGH);  // earth relay to drive forward
    delay(9500);
  if (val2 == HIGH)  // switch to stop driving forward
    digitalWrite(MRELAY2, LOW);
    delay(2000);
    digitalWrite(MRELAY2, HIGH);  // earth relay
    digitalWrite(MRELAY4, HIGH);  // reverse relay
    digitalWrite(MRELAY3, HIGH);  // reverse relay
    delay(9000);          
  if (val1 == HIGH) // switch to stop driving reverse
    digitalWrite(MRELAY3, LOW);
    digitalWrite(MRELAY4, LOW);
    digitalWrite(MRELAY2, LOW);
      
  }else{
  digitalWrite(MRELAY1, LOW);
  }

}
]

a lot would depend on how you wired the switches do you have external pullup/pulldown resistors?

But it keeps going out of timing.

What do you mean exactly?

No i have not.They are just toggle switches with 5 volt power to them.Going back to pin's 6 & 7.

  val = digitalRead(SWITCH1); // ir switch for can hopper
  val1 = digitalRead(SWITCH2); // return cut off switch
  val2 = digitalRead(SWITCH3); // forward cut off switch
  delay(1000);

  if (val == LOW) {
    delay(1000);
 ...
    delay(2000);
...
    delay(2000);
...
    delay(2000);
...
    delay(2000);
...
    delay(9500);
    if (val2 == HIGH)  // switch to stop driving forward
      digitalWrite(MRELAY2, LOW);
    delay(2000);
...
    delay(9000);          
    if (val1 == HIGH) // switch to stop driving reverse
      digitalWrite(MRELAY3, LOW);

You know that you read switch 3 but test its value many seconds (18.5 seconds) later?

And you read switch 2 but test its value even later (27.5 seconds)?

Do you really mean to do that? Why not test the switches directly after you read them?

Grant1962: No i have not.They are just toggle switches with 5 volt power to them.Going back to pin's 6 & 7.

http://www.gammon.com.au/switches

Each can crushes slightly different.So the piston does not all ways end up at the same place.Going forward or reverse.

No i don't think i tried to do that.Why not test the switches directly after you read them? Not sure what you mean by that.

You read a switch:

  val1 = digitalRead(SWITCH2); // return cut off switch

Almost half a minute later you see what value it had:

    if (val1 == HIGH) // switch to stop driving reverse

Doesn't it make more sense to read the switch at the time when you want to test its value?

That's like checking if it is raining today, and then putting your raincoat on tomorrow. The time lapse makes no sense.

Thank you for your help will have a play and see if i can get it to work will let you know how i go.

Hi Nick Have tried as your Switches tutorial send and added the resistor's so they Pull-Down. But still I can not get it working.I also changed the code trying to go each bit at a time.All if fine until i get to the digitalWrite (mrelay3,HIGH); without putting a delay after it, it will not work. Or pull the relay in to drive forward.As soon as i put the delay in it drives forward for the delay time and then stops before getting to the switch.Could you please help just can not work out what i am doing wrong. Regards Grant

int val = 0;
const byte switchPin2 = 7;
const byte switchPin3 = 6;
#define switch 8
#define mrelay1 10
#define mrelay2 11
#define mrelay3 9
#define mrelay4 12
#define mrelay5 13


void setup() {
 pinMode (switch,INPUT);
 pinMode (switchPin2,INPUT);
 pinMode (switchPin3,INPUT);
 pinMode (mrelay1,OUTPUT);
 pinMode (mrelay2,OUTPUT);
 pinMode (mrelay3,OUTPUT);
 pinMode (mrelay4,OUTPUT);
 pinMode (mrelay5,OUTPUT); 

}

void loop() {
  val = digitalRead(switch);
    delay(1000);
    
  if (val == LOW){ 
    delay(1000); 
    digitalWrite (mrelay1,HIGH);
    delay(2000);
    digitalWrite (mrelay1,LOW);
    delay(2000);
    digitalWrite (mrelay2,HIGH);
    delay(2000);
    digitalWrite (mrelay2,LOW);
    delay(2000);
    digitalWrite (mrelay3,HIGH);
    delay(3000);
    {
  if (digitalRead (switchPin2) == HIGH)
    delay(100);
    digitalWrite (mrelay3,LOW); 
    }
  
    }else
  digitalWrite(mrelay1,LOW);
  }

It looks like you have made a mess of the if/else tests in your code. Try putting all of the code to be executed when each if statement is true in braces as well as the code to be executed when the else is true. This is not strictly necessary when only one statement is to be executed but it helps remove confusion. For instance, this code

    {
      if (digitalRead (switchPin2) == HIGH)
        delay(100);
      digitalWrite (mrelay3,LOW); 
    }

will execute the delay if switchPin2 is HIGH but will always execute the digitalWrite. What are the outer braces doing there ?

In the IDE put the cursor to the right of any brace and the matching one of the pair will be indicated. Does each pair of braces surround the correct set of code ? This is much easier to see if each brace, both opening and closing, is put on its own line and the code is Auto Formatted to make pairs of braces line up.

Why not have it drive until the switch is hit or a timeout is reached or the piston motion has stopped due to force?

Of course that would mean learning to write multi-tasking, real time code. But that’s actually a =lot= easier than it sounds.

It might even be good to check the crusher for heat or excess current draw, or at least put a thermal fuse on the power line.

One thing. Put even a small dent in a can and it will crush much easier.