Reed switches and Boolean

Good afternoon all,

The coding below is what i have created from all the help i've recieved on here and a little bit from the internet, please note i have removed all of the integers and pinModes as I'm confident these work. The part I am struggling with is right at the bottom within the voids() and the void loop.

The project consists of oo gauge locomotive on short track shunting small carts around. It goes forwards and stops. but wont move on the next program to send it back the other way and stop. I was hoping that when the loop runs again it just goes back to the beginning going forward stop and then back again and stop etc.

boolean flag;

void setup() {

  digitalWrite(motor1Pin1,LOW);
  flag = true;//set flag when motor1Pin1 is LOW
  digitalWrite(motor1Pin2, HIGH);
}

void moveForward() {
    
  analogWrite(enablePin, 120); // locomotive crawls forward in what set by the variable resistor.
  digitalWrite(motor1Pin1, LOW); // enable both motorpins to drive fowards
  digitalWrite(motor1Pin2, HIGH); // this runs until the below becomes true.
}

void forwardStop(){
  
  if (digitalRead(reedSwitch1) == HIGH && flag == true) { // once the reed switch is magnetised and the flag becomes true 
                                                          //both L293 pins are switched to low. The locomotive waits untill the void
                                                         //loop() delays expires and it then moves backwards 
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  flag = false; // flag of boolean is reset.
  }
} 

void moveBackward() {
   
  analogWrite(enablePin, 120); // locomotive crawls backwards in what set by the variable resistor.
  digitalWrite(motor1Pin1, LOW); // enable both motorpins to drive backwards.
  digitalWrite(motor1Pin2, HIGH); // this runs until the below becomes true.
}

void backwardStop(){
  if (digitalRead(reedSwitch2) == HIGH && flag == false) { // once the reed switch is magnetised and the flag becomes true 
                                                           //both L293 pins are switched to low. The locomotive waits untill the void
                                                           //loop() delays expires and it then moves forwards. 
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  flag = false; // flag of boolean is reset.
  }
}

void loop() {

  moveForward(); //forward program runs for as long as it needs to get the locomotive to the end where reed sensor is and sit there for a few millisecond before returning backwards.
  delay(60);
   
  forwardStop();
  delay(40);

   moveBackward();  //backwards program runs for as long as it needs to get the locomotive to the end where reed sensor is and sit there for a few millisecond before returning backwards.
   delay(60);
   
   backwardStop();
   delay(40);

   // program then repeats forward, backwards, forward etc....
   
}

You have

  digitalWrite(motor1Pin1, LOW); // enable both motorpins to drive fowards
  digitalWrite(motor1Pin2, HIGH); // this runs until the below becomes true.

and

  digitalWrite(motor1Pin1, LOW); // enable both motorpins to drive backwards.
  digitalWrite(motor1Pin2, HIGH); // this runs until the below becomes true.

Do the same commands really make the motor move forward and backward or did you copy/paste the functions and forget to change the commands ?

Thank you. Yes one should be high and the other low and vice versa for the other.

Any thoughts for the rest of it?

The part I am struggling with is right at the bottom within the voids()

There are no voids in your program. There are functions whose return type is void.

   // program then repeats forward, backwards, forward etc....

I'm going to go out on a limb here, and suggest that this is where your problem is.

The code is never waiting until the train reaches the end of the track. If the train is not at the end of the track when forwardStop() or backwardStop() is called, it will just return to the loop() function and reverse directions.

Here's the scenario (I'll leave out the delays since they don't really do anything):

  • Call moveForward() - this starts the train moving (contrary to the comment, it doesn't wait for the train to read the end of the track).
  • Call forwardStop() - at this point, the train has just started, so the function returns without stopping the train.
  • Call moveReverse() - the train has barely started, but is immediately reversed.
  • Call reverseStop() - at this point, the train may be at the end (since it barely had a chance to move), so the function may stop the train.
  • Start over.

This is the basic flow of the code, which isn't what you want. It also doesn't match what you said is actually happening. That can be explained by the error is the motor pins that UKHeliBob pointed out (basically, causing the train only goes in one direction).

When troubleshooting this type of problem, it is very helpful to manually "walk through" the code to see what it is doing. If that doesn't help, you can also print values to the monitor at key points to see how the code is actually behaving.

--Doug

Before you even start programming, you should sit down with a pencil and paper and write down in precise detail what you need to happen, like this:

  1. start train
  2. keep reading both end markers until train has reached the other end
  3. when it has, stop the train.
  4. wait a while
  5. set reverse direction
  6. loop to 1

Now your programming becomes almost obvious.

NB, delay(40) means delay for 40/1000ths of a second.
Try delay(4000) = 4 seconds to see some movement of the train.