What type of code to use

Could anybody please help.
I am trying to read a reed switch. But the code I am using is not quite right. Can anybody tell me what type of code I need to keep looking at the reed switch until it goes to a high state. So it doesn't look at the next part of the code until the condition is true.There is a bit of time between looking at the reed switch and the reed switch going to high state.
Regards
Grant

  while (reed_read()==LOW) continue;

will do it.

Thank you very much will try.
Regards
Grant

Grant1962:
... the code I am using is not quite right.

Perhaps if you posted this code?

Read this before posting a programming question

It's in the bottom part of the 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
  }

}

while (reed_read()==LOW) continue; will do it.

Sure, if you write the function correctly. Useless information if you don't provide the function, as is most of your drivel.

If you want to code to keep reading the state of the reed switch input until it turns HIGH, then I'd do it something like this:

while(digitalRead(REED1) == LOW)
{
    // do nothing
}
// by the time we reach here, the reed switch is HIGH

You're starting on the right foot by using #defines to name your input and output pins, but the names you've chosen are pretty poor. Just by comparing the code against your comments, it's obvious that the names don't give you any indication of what the value represents. If you gave them meaningful names, you wouldn't have needed to add all those comments telling you what the code does:

digitalWrite(MRELAY1, HIGH);// Open frist gate
digitalWrite(MRELAY5, HIGH);// Open second gate
digitalWrite(MRELAY2, HIGH);// Swicth earth relay to drive motor forward
digitalWrite(MRELAY4, HIGH);// swicth relay over to reverse motor
digitalWrite(MRELAY3, HIGH);// swicth relay over to reverse motor

Comments are a good idea and I'd encourage you to keep using them, but when you find yourself explaining what the code does (rather than why or how it does it) this is a clue that the code would benefit from being made clearer.

Comments are a good idea and I'd encourage you to keep using them, but when you find yourself explaining what the code does (rather than why or how it does it) this is a clue that the code would benefit from being made clearer.

+2

PeterH
Thank you Instead of putting people down you have helped me so much. I have been doing this for about one year but have not able to get my head around it. That's when I would ask for help.Your the the only person that has given help without treaty people like they are fool's.Thank you so much for your advice.
Regards
Grant