Automated brake system

Hello,

I am building a rollercoaster made of K’nex and I want to automate it.
So far my station is ready, now I’m working on the brake system.

This is what the brake must do:
The brake should always be closed, except when there is a train in the brake and there is no train in the station. Than it can open after 3 seconds. (there are 2 trains on the track, so the brake is to avoid collision)

I wrote a code with if and elses, but it doesn’t work. So I was thinking to redo everything and do it with a switch and cases.
I know the basics of switch, but never actually worked with it.

Could you guys help me a bit?

Here is the code I have so far.
The servo for the brake opens and closes super fast. I don’t get it.

// Brake

#include <Servo.h>

const int reedswitchBrakePin = 6;
const int servoBrakePin = 7;
const int reedswitchStationPin = 2; // same sensor as station
Servo servoBrake;

const int brakeOn  = 120;
const int brakeOff = 100;

boolean brakeCanOff = false;
boolean lastBrakeCanOff = false;

unsigned long currentMillis = 0;
unsigned long brakeMustOff  = 0;
unsigned long brakeMustOn   = 0;
int millisBeforeBrakeOff = 3000;
int millisBeforeBrakeOn  = 3000;

void setup() {
  pinMode(reedswitchStationPin, INPUT);
  pinMode(reedswitchBrakePin, INPUT);
  servoBrake.attach(servoBrakePin);
  servoBrake.write(brakeOn);
}

void loop() {
  currentMillis = millis();
  digitalRead(reedswitchStationPin);
  digitalRead(reedswitchBrakePin);
  brake();
  lastBrakeCanOff = brakeCanOff;
}

void brake() {

  if (reedswitchStationPin == LOW && reedswitchBrakePin == HIGH) {
    brakeCanOff = true;
  }
  
  if (brakeCanOff != lastBrakeCanOff) {
    if (brakeCanOff = true) {
      brakeMustOff = currentMillis;
    }
  }
  
  if (currentMillis - brakeMustOff >= millisBeforeBrakeOff) {
    servoBrake.write(brakeOff);
    brakeCanOff = false;

  }
  
  if (brakeCanOff != lastBrakeCanOff) {
    if (brakeCanOff = false) {
      brakeMustOn = currentMillis;
    }
  }
  
  if (currentMillis - brakeMustOn >= millisBeforeBrakeOn) {
    servoBrake.write(brakeOn);
  }
  
  if (reedswitchStationPin == HIGH) {
    servoBrake.write(brakeOn);
  }
}

Sorry for the mess

I wrote a code with if and elses, but it doesn't work. So I was thinking to redo everything and do it with a switch and cases.

A switch statement with a series of cases is the same as the if/else if/else structure. If the if/else if/else structure doesn't work, for some definition of work, changing to a switch statement is not going to apply any magic potion and make it "work".

  digitalRead(reedswitchStationPin);
  digitalRead(reedswitchBrakePin);

Like watching a sunset with a blindfold on. If you don't care about the returned value, why bother reading?

  if (reedswitchStationPin == LOW && reedswitchBrakePin == HIGH) {

The pin NUMBER is not LOW. The other pin NUMBER is not HIGH. Pin STATES might be HIGH or LOW. Pin NUMBERS rarely are, unless you are referring to the hardware serial pins.

    if (brakeCanOff = true) {

= is the assignment operator. == is the equality operator. Which should you be using?

Yeah I know I can’t just put it in a switch statement. But I said I wanted to redo everything anyway.

Thanks for the review, I did some major blunders in that program. Sorry for that :wink:

Here is the new code:

// Brake

#include <Servo.h>

const int reedswitchBrakePin = 6;
const int servoBrakePin = 7;
const int reedswitchStationPin = 2; // same sensor as station
Servo servoBrake;

int reedswitchStationState = 0;
int reedswitchBrakeState   = 0;


const int brakeOn  = 120;
const int brakeOff = 100;

boolean brakeCanOff = false;
boolean lastBrakeCanOff = false;

unsigned long currentMillis = 0;
unsigned long brakeMustOff  = 0;
unsigned long brakeMustOn   = 0;
int millisBeforeBrakeOff = 3000;
int millisBeforeBrakeOn  = 3000;

void setup() {
  pinMode(reedswitchStationPin, INPUT);
  pinMode(reedswitchBrakePin, INPUT);
  servoBrake.attach(servoBrakePin);
  servoBrake.write(brakeOn);
}

void loop() {
  currentMillis = millis();
  reedswitchStationState = digitalRead(reedswitchStationPin);
  reedswitchBrakeState = digitalRead(reedswitchBrakePin);
  brake();
  lastBrakeCanOff = brakeCanOff;
}

void brake() {

  if (reedswitchStationState == LOW && reedswitchBrakeState == HIGH) {
    brakeCanOff = true;
  }
  
  if (brakeCanOff != lastBrakeCanOff) {
    if (brakeCanOff == true) {
      brakeMustOff = currentMillis;
    }
  }
  
  if (currentMillis - brakeMustOff >= millisBeforeBrakeOff) {
    servoBrake.write(brakeOff);
    brakeCanOff = false;

  }
  
  if (brakeCanOff != lastBrakeCanOff) {
    if (brakeCanOff == false) {
      brakeMustOn = currentMillis;
    }
  }
  
  if (currentMillis - brakeMustOn >= millisBeforeBrakeOn) {
    servoBrake.write(brakeOn);
  }
  
  if (reedswitchStationState == HIGH) {
    servoBrake.write(brakeOn);
  }
}

Here is the new code:

And? It works? Or no?

I just tested it, and it doesn't work.

I can't really tell what's wrong. The servo seems to change allmost random.

The servo seems to change allmost random.

Can't be. There are no calls to random().

What do your Serial.print()s tell you is happening? (Yes, I know you don't have any. Why not?)