Help with function

Hi,
I wanna make, that relay will be opened, till limit switch are not pressed, and when I press limit switch relay immediately turns off. This part are part of my program, so I written a function witch wont work :smiley: Can anybody help?

//This function opens a relay until EndStop is pressed

void open_relay(int relay, int limsw){
pinMode(limsw, INPUT);
pinMode(relay, OUTPUT);

int limstate = digitalRead(limsw);
while (limstate == 0) {
digitalWrite(relay, 1);
}

digitalWrite(relay, 0);
}

Please post your complete code inside the code tags, so it will look like this :

//This function opens a relay until EndStop is pressed

void open_relay(int relay, int limsw){
pinMode(limsw, INPUT);
pinMode(relay, OUTPUT);

int limstate = digitalRead(limsw);
while (limstate == 0) {
  digitalWrite(relay, 1);
}

digitalWrite(relay, 0);
}

For now, some quick notes :

pinMode(limsw, INPUT);
pinMode(relay, OUTPUT);

This code must to be putted in setup() function, at the beginning of the program, usually you assign o role to a pin only once.

int limstate = digitalRead(limsw);
while (limstate == 0) {
  digitalWrite(relay, 1);
}

Here you declaring a int variable limstate and assign it the value of digitalRead(limsw) [BTW, why use a int(2 byte) when a bool or a byte it's enough?]. After the while loop check if limstate is equal to 0. If this is true you enter in a never ending loop, since nobody gonna change the value of limstate again. So maybe could be better something like this :

while (digitalRead(limsw) == 0) {
  digitalWrite(relay, 1);
}

Finally, you want your code to react immediatily at the limit switch action, but if you put it inside a user function how can you be sure that the function will be executing in the same moment? Maybe it' better if you move this code inside the loop() function.

Cheers, Ale.

(deleted)

//This function opens a relay until EndStop is pressed
void open_relay(int relay, int limsw){

pinMode(relay, OUTPUT);
Serial.println("open");
digitalWrite(relay, HIGH);
  wait_to_hit(limsw);
  Serial.println("closed");
  digitalWrite(relay, LOW);
}

//------------------------------------------------------------------------
//This function waits until EndStop hits
void wait_to_hit(int lim){
  pinMode(lim, INPUT);
  while(digitalRead(lim) < 1){
            Serial.println("wt"); 
            delay(1000);
        }
}

Okay, I have added another function, witch waits till limswitch is pressed, and still, I don’t know why it still works like crap.
I cannot add all this stuff in to loop, cause I need it written in functions :confused:

WOP WOP WOP, I fixed that stuff :smiley:

//This function opens a relay until EndStop is pressed
void open_relay(int relay, int limsw){

pinMode(relay, OUTPUT);
Serial.println("open");
digitalWrite(relay, LOW);
  wait_to_hit(limsw);
  Serial.println("closed");
  digitalWrite(relay, HIGH);
}

//------------------------------------------------------------------------
//This function waits until EndStop hits
void wait_to_hit(int lim){
  pinMode(lim, INPUT);
  while(digitalRead(lim) > 0){
            Serial.println("wt"); 
            delay(100);
        }
}

works like a charm

It's a shame that the code as posted is not complete.

Are you using a pullup resistor with the pin related to limsw ?

I'm using http://www.ebay.com/itm/6pcs-Mechanical-End-Stop-Endstop-Limit-Switch-Cable-for-CNC-3D-Printer-RAMPS-1-4-/201537186890?hash=item2eec8d6c4a:g:kicAAOSwvgdW3kBS

A couple of things: First, rarely do you need to use the pinMode() function more than once, and usually that is done in setup(). Second, before you post your code, use Ctrl-T in the IDE's source code window to format the code into a common C style. It makes it easier for us to read. Finally, there is an icon on the insert dialog box to be used when you insert a URL link in your post. That makes it easier and faster for the reader to branch to the URL.