Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function runTrain()
- % The main function, this is what is called first. I use this function to initialize all of our variables and set up the arduino.
- delete(timerfindall);
- clear;
- close all;
- delete(instrfindall);
- clc;
- % Get the speed limits and set up the arduino for either sim or physical
- simulator = input('Run the train on a simulator? (0/1)\nsimulator = ');
- dSpeed = input('What is the speed limit in the departure phase?\ndSpeed = ');
- aSpeed = input('What is the speed limit in the approach phase?\naSpeed = ');
- if(simulator > 0)
- train = arduino_sim('COM3');
- else
- train = arduino('COM3');
- end
- % Set up the LEDs
- train.pinMode(14, 'output');
- train.pinMode(15, 'output');
- % Set up the gates
- train.servoAttach(1);
- % Global allows other functions to edit this variable. This is used to flash the lights.
- global flashState;
- flashState = 1;
- % Set up all the timing functions
- startTimer = timer('StartDelay', 4.5, 'Name', 'startTimer');
- startTimer.TimerFcn = {@startTrain, train, aSpeed};
- stopTimer = timer('StartDelay', 2, 'Name', 'stopTimer');
- stopTimer.TimerFcn = {@stopTrain, train};
- flashTimer = timer('Period', .5, 'TasksToExecute', 4, 'ExecutionMode', 'fixedRate', 'Name', 'flashTimer');
- flashTimer.TimerFcn = {@flashLights, train};
- flashTimer.StopFcn = {@turnOffLights, train};
- gateTimer = timer('StartDelay', .45, 'ExecutionMode', 'fixedRate', 'Period', 1.55, 'TasksToExecute', 2, 'Name', 'gateTimer');
- gateTimer.TimerFcn = {@toggleGate, train};
- % Start the train
- train.motorRun(1, 'forward');
- train.motorSpeed(1, dSpeed);
- % Move into the function that actually runs the train
- manageTrain(train, stopTimer, flashTimer, gateTimer, startTimer, dSpeed, aSpeed);
- end
- function manageTrain(train, stopTimer, flashTimer, gateTimer, startTimer, dSpeed, aSpeed)
- % Pause execution until the approach sensor is broken
- train.analogRead(2);
- while train.analogRead(2) < 200
- end
- % Once the sensor is broken, the while loop ends and this code runs. The train is slowed, and our timers are started.
- % All timers run at the same time.
- train.motorSpeed(1, aSpeed);
- % Lowers the gate after .45s, then raises it after 1.55s using the toggleGate() function
- start(gateTimer);
- % Alternates the lights every .5 seconds, 4 times = 2 total seconds using flashLights()
- % After the 2 seconds, makes sure both lights are off using turnOffLights()
- start(flashTimer);
- % Stops the train after 2 seconds using stopTrain()
- start(stopTimer);
- % Starts the train after 4.5 seconds using startTrain()
- start(startTimer);
- % Wait for the departure sensor to trip
- train.analogRead(3);
- while train.analogRead(3) < 200
- end
- % Return to normal speed
- train.motorSpeed(1, dSpeed);
- % Call itself to loop
- manageTrain(train, stopTimer, flashTimer, gateTimer, startTimer, dSpeed, aSpeed);
- end
- function flashLights(~, ~, train)
- global flashState;
- if flashState == 1
- train.digitalWrite(14, 1);
- train.digitalWrite(15, 0);
- else
- train.digitalWrite(14, 0);
- train.digitalWrite(15, 1);
- end
- flashState = ~flashState;
- end
- function turnOffLights(~, ~, train)
- train.digitalWrite(14, 0);
- train.digitalWrite(15, 0);
- end
- function stopTrain(~, ~, train)
- train.motorRun(1, 'release');
- end
- function toggleGate(~, ~, train)
- if train.servoRead(1) == 180;
- train.servoWrite(1, 90);
- else
- train.servoWrite(1, 180);
- end
- end
- function startTrain(~, ~, train, aSpeed)
- train.motorRun(1, 'forward');
- train.motorSpeed(1, aSpeed);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement