#define DEBUG 0#include <Servo.h>enum SpoorState {leeg, sensor1komend, sensor2komend, sensor1gaand, sensor2gaand };//// Class for a set of two (red) leds which alternate(flicker) 50 times per// minute when on//class DuoLed{ private: int led1Pin; int led2Pin; bool ledOn = true; int interval = 600; // *** 50 keer per minuut bool lightOn = false; bool phase = false; unsigned long lastTime; public: // // Create a new instance, connect it to the specified // digital pins and put it in the specified state (true = on, false = off) // DuoLed(int aPin1, int aPin2, bool state) { led1Pin = aPin1; led2Pin = aPin2; ledOn = state; lightOn = false; phase = false; pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); lastTime = millis(); } // // Set the state of the leds (true = on, false = off) // void setOn(bool state) {#if (DEBUG) if (ledOn != state) { Serial.print("[DuoLed::setOn]"); if (state) Serial.println(" aan"); else Serial.println(" uit"); }#endif ledOn = state; } // // If the state is on, put power on the pins of the leds, using timers // to make the alternating flicker work. // If the led is off, remove the power from the pins of the leds. // void heartBeat() { if (ledOn) // Are the leds activated { lightOn = true; unsigned long currTime = millis(); if ((currTime - lastTime) >= interval) // Has the interval passed? { lastTime = currTime; // Yes, start new interval if (!phase) // The leds alternate, are we in phase1 (led1 on, led2 off) {#if (DEBUG) Serial.println("[DuoLed::heartBeat] led1 aan, led2 uit");#endif digitalWrite(led1Pin, HIGH); // Yes, led1 on digitalWrite(led2Pin, LOW); // led2 off phase = true; // When the interval expires, switch the leds } else // We are in phase2 (led1 off, led2 on) {#if (DEBUG) Serial.println("[DuoLed::heartBeat] led1 uit, led2 aan");#endif digitalWrite(led1Pin, LOW); // Led1 off digitalWrite(led2Pin, HIGH); // Led2 on phase = false; // When the interval expires, switc the leds } } } else // The leds are not active, switch of both leds { if (lightOn) {#if (DEBUG) Serial.println("[DuoLed::heartBeat] led1 uit, led2 uit");#endif digitalWrite(led1Pin, LOW); digitalWrite(led2Pin, LOW); lightOn = false; } } }};/* A wrapper class for controlling RC servo's*/class ArmServo{ private:#define REFRESH_TIME 30000 Servo *myServo; unsigned long timeOfLastStep; unsigned long lastTimeActive; unsigned int target; unsigned int current; unsigned int highPosition; // Arm position high unsigned int lowPosition; // Arm position low unsigned int servoWaitTime; // Wait time between 2 steps int servoPin; bool active = false; void attachServo() // Intialize the servo {#if (DEBUG) Serial.print("[ArmServo::attachServo] Servo pin: "); Serial.println(this->servoPin);#endif //DEBUG this->myServo->attach(this->servoPin); this->active = true; this->myServo->write(this->current); // Restore last known position } void detachServo() {#if (DEBUG) Serial.print("[ArmServo::detachServo] Servo pin: "); Serial.println(this->servoPin);#endif //DEBUG this->myServo->detach(); this->active = false; } public: ArmServo(int newServoPin, int newWaitTime, int newHighPosition, int newLowPosition) { this->target = 0; this->current = 0; this->highPosition = 0; // Arm position high this->lowPosition = 0; // Arm position low this->servoWaitTime = 40; // Wait time between 2 steps this->active = false; this->myServo = new Servo(); this->servoPin = newServoPin; this->servoWaitTime = newWaitTime; this->highPosition = newHighPosition; this->lowPosition = newLowPosition; this->target = newHighPosition; // We asume that the arms are initial in their this->current = newHighPosition; // upper position. this->current = 0; this->active = false; } void laag() {#if (DEBUG) Serial.print("[ArmServo::laag] arm going to low position: "); Serial.println(lowPosition); Serial.flush();#endif //DEBUG target = lowPosition; } void hoog() {#if (DEBUG) Serial.print("[Armservo::hoog] arm going to high position: "); Serial.println(highPosition); Serial.flush();#endif //DEBUG target = highPosition; } bool stepServo() { // // When the arm's position isn't at the target, we have to move the arm // if (this->target != this->current) { // // Is the servo active? if not attach it to it's pin so it can start moving // if (!this->active) {#if (DEBUG) Serial.println("{ArmServo::stepServo] attach"); Serial.flush();#endif // DEBUG this->attachServo(); } lastTimeActive = millis(); if ((lastTimeActive - this->timeOfLastStep) >= this->servoWaitTime) { // // The servo is moving to it's new location. //#if (DEBUG) Serial.print("[ArmServo::stepServo] target: "); Serial.print(this->target); Serial.print(", current: "); Serial.print(this->current);#endif //DEBUG if (this->target > this->current) { this->current++; } else { this->current--; }#if (DEBUG) Serial.print(", going to: "); Serial.println(this->current); Serial.flush();#endif // DEBUG this->myServo->write(this->current); this->timeOfLastStep = lastTimeActive; } return true; // The arm is still moving } else { if ((millis() - lastTimeActive) >= REFRESH_TIME) { // // Is the servo active? if not attach it to it's pin so it can start moving // if (!this->active) {#if (DEBUG) Serial.println("{ArmServo::stepServo] attach"); Serial.flush();#endif // DEBUG this->attachServo(); }#if (DEBUG) Serial.print(", refreshing to: "); Serial.println(this->current); Serial.flush();#endif // DEBUG this->myServo->write(this->current); this->lastTimeActive = millis(); } // // The arm reached it's position, so we can now detach the servo (if it was attached) // if (this->active) { this->detachServo(); } return false; // The arm has stopped moving } }};enum AkiStaat {aan, uit};//// A simple class that represents a complete AHOB with all its leds and two Servo's// for moving the arms//// TBD, the sound of the bells//class AHOB{ private: DuoLed *redLights; ArmServo *arm1; ArmServo *arm2; AkiStaat state = uit; public: // // Create an instance of the AHOB, using the specified Led and ArmServo objects // AHOB(DuoLed *aRedLed, ArmServo *aServo1, ArmServo *aServo2) { redLights = aRedLed; arm1 = aServo1; arm2 = aServo2; state = uit; // // AHOB is veilig, dus roodlicht uit en armen omhoog // redLights->setOn(false); arm1->hoog(); arm2->hoog(); } // // Activate the connected leds and arm servo's. TBD: the sound // void heartBeat() { redLights->heartBeat(); arm1->stepServo(); arm2->stepServo(); } void set(AkiStaat aState) {#if (DEBUG) if (state != aState) { Serial.print("[AKI::set]"); if (aState == uit) Serial.println(" uit"); else Serial.println(" aan"); }#endif state = aState; if (state == uit) { // // AHOB is veilig, dus roodlicht uit en armen omhoog // redLights->setOn(false); arm1->hoog(); arm2->hoog(); } else { // // AHOB is onvelig, dus roodlicht aan en arme omlaag // redLights->setOn(true); arm1->laag(); arm2->laag(); } }};//// Some sensors use a digital signal and keep it high when not active and drop the signal// when the sensor trips, use in that case the "laag" setting. Others use a digital signal// and are low when not active and make the signal high when the sensor trips, use in that case// the "hoog" setting.// if the sensor doesn't generate a digital signal, you have to use a pullup resistor (or INPUT_PULLUP)// and in that case specify hoog.//// The Flying Fish proximity detectors, when used in reflective mode, need the "laag" setting, // when their sensors are removed and positioned opposit each other, they need the "hoog" setting//enum SensorTrigger {laag, hoog};//// A simple class which represents the state of a single sensor//class Sensor{ private: int sensorPin; SensorTrigger type = hoog; bool state = false; // true = on, false = off unsigned long lastTime = 0; // Timer to ignore short interrupts#define JITTER_TIME 1000 // wait a second before the sensor goes off public: // // Create a new instance // Sensor(int aPin) { sensorPin = aPin; pinMode(sensorPin, INPUT_PULLUP); } // // Create a new instance // Sensor(int aPin, SensorTrigger aType) { sensorPin = aPin; type = aType; pinMode(sensorPin, INPUT_PULLUP); } // // Return the current logical state of the sensor // bool getState() { return state; } // // Monitor the physical sensor and update the state accordingly // void heartBeat() { if (digitalRead(sensorPin) == HIGH) { if (type == laag) // For sensors type laag an high signal means they are in the off state { if (state != false) // Process this signal only when the current state is ON { if (lastTime == 0) { lastTime = millis(); // start Interval } else { unsigned long currTime = millis(); if ((currTime - lastTime) >= JITTER_TIME) {#if (DEBUG) Serial.println("[Sensor::heartBeat] sensor(laag) goes off");#endif state = false; // sensor goes OFF lastTime = 0; // Reset interval timer } } } } else {#if (DEBUG) if (state != true) { Serial.println("[Sensor::heartBeat] sensor(hoog) goes on"); }#endif state = true; // Sensor goes ON lastTime = 0; // Reset interval timer } } else // sensorPin = LOW { if (type == laag) {#if (DEBUG) if (state != true) { Serial.println("[Sensor::heartBeat] sensor(laag) goes on"); }#endif state = true; // Sensor goes ON lastTime = 0; // Reset interval timer; } else { if (state != false) // Process this signal only when the current state is ON { if (lastTime == 0) { lastTime = millis(); // start Interval } else { unsigned long currTime = millis(); if ((currTime - lastTime) >= JITTER_TIME) {#if (DEBUG) Serial.println("[Sensor::heartBeat] sensor(hoog) goes off");#endif state = false; // sensor goes OFF lastTime = 0; // Reset interval timer } } } } } }};//// A finite state machine that guards a occupance of a single track//class SpoorControl{ private: Sensor *sensor1; Sensor *sensor2; SpoorState state = leeg; // // Based on the state of sensor 1 and the current state of the FSM, a new state // is calculated. // void handleSensor1(bool trigger) { if (trigger) // Sensor 1 is ON { if (state == leeg) // If the state is "leeg" it means that a train enters the section {#if (DEBUG) Serial.println("[SpoorControl::handleSensor1] state is sensor1komend");#endif state = sensor1komend; // so set it to state komend } else if (state == sensor2komend) // if the other sensor tripped first {#if (DEBUG) Serial.println("[SpoorControl::handleSensor1] state is sensor1gaand");#endif state = sensor1gaand; // The train is starting to leave the section } } else { // Sensor went OFF if (state == sensor1gaand) // If the train started to leave via sensor 1 {#if (DEBUG) Serial.println("[SpoorControl::handleSensor1] state is leeg");#endif state = leeg; // the section is "leeg" } } } // // Based on the state of sensor 2 and the current state of the FSM, a new state // is calculated. // void handleSensor2(bool trigger) { if (trigger) // Sensor 2 is ON { if (state == leeg) // Is the section "leeg"? {#if (DEBUG) Serial.println("[SpoorControl::handleSensor2] state is sensor2komend");#endif state = sensor2komend; // A Train entered the section via sensor 2 } else if (state == sensor1komend) // No a train entered the section via the other sensor {#if (DEBUG) Serial.println("[SpoorControl::handleSensor2] state is sensor2gaand");#endif state = sensor2gaand; // Start the leaving process } } else { if (state == sensor2gaand) // A train is in the process of leaving {#if (DEBUG) Serial.println("[SpoorControl::handleSensor2] state is leeg");#endif state = leeg; // Section is empty again } } } public: // // Create an instance with two sensors // SpoorControl(Sensor *aSensor1, Sensor *aSensor2) { sensor1 = aSensor1; sensor2 = aSensor2; } // // Get give the sensors a kick and check their state so we can update our own // state. // void heartBeat() { sensor1->heartBeat(); sensor2->heartBeat(); handleSensor1(sensor1->getState()); handleSensor2(sensor2->getState()); } // // Return the current state of the section // SpoorState getState() { return state; }};//// Create 2 FSM controling two track sections//#define SENSOR1_PIN A0 // Analoge pin A0#define SENSOR2_PIN A1 // Analoge pin A1#define SENSOR3_PIN A2 // Analoge pin A2#define SENSOR4_PIN A3 // Analoge pin A3SpoorControl spoor1(new Sensor(SENSOR1_PIN, laag), new Sensor(SENSOR2_PIN, laag)); // Sensor works reflectiveSpoorControl spoor2(new Sensor(SENSOR3_PIN, laag), new Sensor(SENSOR4_PIN, laag));#define LED1_PIN A5 // Analoge pin A5#define LED2_PIN A6 // Analoge pin A6DuoLed roodlicht(LED1_PIN, LED2_PIN, false);#define SERVO1_PIN 11 // PWM pin 11#define SERVO1_WACHTTIJD 40 // Wachttijd (in MS) tussen 2 servo stapjes, Hogere waarden = langzamer#define SERVO1_LAAGPOSITIE 45 // Positie arm in laag stand#define SERVO1_HOOGPOSITIE 100 // Positie arm in hoog stand#define SERVO2_PIN 12 // PWM pin 12#define SERVO2_WACHTTIJD 40 // Wachttijd (in MS) tussen 2 servo stapjes, Hogere waarden = langzamer#define SERVO2_LAAGPOSITIE 45 // Positie arm in laag stand#define SERVO2_HOOGPOSITIE 100 // Positie arm in hoog standArmServo arm_een(SERVO1_PIN, SERVO1_WACHTTIJD, SERVO1_LAAGPOSITIE, SERVO1_HOOGPOSITIE);ArmServo arm_twee(SERVO2_PIN, SERVO2_WACHTTIJD, SERVO2_LAAGPOSITIE, SERVO2_HOOGPOSITIE);//// Create an instance of the AHOB//AHOB ahob(&roodlicht, &arm_een, &arm_twee);void setup(){#if (DEBUG) //Serial.begin(9600); Serial.begin(115200); while (!Serial); Serial.println("Starting"); Serial.flush();#endif //DEBUG}void loop(){ spoor1.heartBeat(); // These object must monitor their sensors spoor2.heartBeat(); // so they can update their states if ((spoor1.getState() == leeg) && (spoor2.getState() == leeg)) { ahob.set(uit); // Stop sound and flashing red lights } else { ahob.set(aan); // Start flashing red lights and sound } ahob.heartBeat(); // Let the AHOB update it's connected bells and leds}
#define SERVO1_PIN 11 // PWM pin 11#define SERVO1_WACHTTIJD 40 // Wachttijd (in MS) tussen 2 servo stapjes, Hogere waarden = langzamer#define SERVO1_LAAGPOSITIE 45 // Positie arm in laag stand#define SERVO1_HOOGPOSITIE 100 // Positie arm in hoog stand#define SERVO2_PIN 12 // PWM pin 12#define SERVO2_WACHTTIJD 40 // Wachttijd (in MS) tussen 2 servo stapjes, Hogere waarden = langzamer#define SERVO2_LAAGPOSITIE 45 // Positie arm in laag stand#define SERVO2_HOOGPOSITIE 100 // Positie arm in hoog standArmServo arm_een(SERVO1_PIN, SERVO1_WACHTTIJD, SERVO1_LAAGPOSITIE, SERVO1_HOOGPOSITIE);ArmServo arm_twee(SERVO2_PIN, SERVO2_WACHTTIJD, SERVO2_LAAGPOSITIE, SERVO2_HOOGPOSITIE);
@de voeding.DVE Switching Adapter:Input:100/120Volt~50/60Hz, 3AOutput: +12V 1A.Is gejat van oude router die ik nog had liggen. Nu zit ie op de laptop aangesloten. Als ik verder ga heb uit veiligheidsoverwegingen van de laptop een 9Volt batterij met klip.
Lampje blijft rood branden en Arduino blijft continu doorgaan met servootje, hoe zet je dat tussentijds uit?'k heb maar ff koud de stekker eruit getrokken.
@MeinoDat ziet er veelbelovend uit. Heb je ook een schema met de aansluitingen voor de PIN' 's?
Koen Overigens sluit LEDS nooit zonder weerstand aan op een Arduino, ze gaan daar onherroepelijk stuk van. Dat klinkt misschien vreemd omdat een IO pin maar 100-200mA kan leveren, maar dat is voor de meeste LEDS gewoon teveel.
#define DEBUG 0#include <Servo.h>enum SpoorState {leeg, sensor1komend, sensor2komend, sensor1gaand, sensor2gaand };//// Class for a single (white) led which flickers 50 times per// minute when on//class SingleLed{ private: int ledPin; bool ledOn = false; public: // // Create a new instance of a (white) led, connect it to the specified // digital pin and put it in the specified state (true = on, false = off) // SingleLed(int aPin, bool state) { ledPin = aPin; ledOn = state; pinMode(ledPin, OUTPUT); } // // Set the state of the led (true = on, false = off) // void setOn(bool state) {#if (DEBUG) if (ledOn != state) { Serial.print("[SingleLed::setOn]"); if (state) Serial.println(" aan"); else Serial.println(" uit"); }#endif ledOn = state; } // // If the state is on, put power on the pin of the led, using timers // to make the flicker work. // If the led is off, remove the power from the pin of the led. // void heartBeat() { if (ledOn) // Do we need to light the led? {#if (DEBUG) Serial.println("[SingleLed::heartBeat] led aan");#endif digitalWrite(ledPin, HIGH); // No, so switch it on } else {#if (DEBUG) Serial.println("[SingleLed::heartBeat] led uit");#endif digitalWrite(ledPin, LOW); // Switch ir off } }};/* A wrapper class for controlling RC servo's*/class ArmServo{ private:#define REFRESH_TIME 30000 Servo *myServo; unsigned long timeOfLastStep; unsigned long lastTimeActive; unsigned int target; unsigned int current; unsigned int highPosition; // Arm position high unsigned int lowPosition; // Arm position low unsigned int servoWaitTime; // Wait time between 2 steps int servoPin; bool active = false; void attachServo() // Intialize the servo {#if (DEBUG) Serial.print("[ArmServo::attachServo] Servo pin: "); Serial.println(this->servoPin);#endif //DEBUG this->myServo->attach(this->servoPin); this->active = true; this->myServo->write(this->current); // Restore last known position } void detachServo() {#if (DEBUG) Serial.print("[ArmServo::detachServo] Servo pin: "); Serial.println(this->servoPin);#endif //DEBUG this->myServo->detach(); this->active = false; } public: ArmServo(int newServoPin, int newWaitTime, int newHighPosition, int newLowPosition) { this->target = 0; this->current = 0; this->highPosition = 0; // Arm position high this->lowPosition = 0; // Arm position low this->servoWaitTime = 40; // Wait time between 2 steps this->active = false; this->myServo = new Servo(); this->servoPin = newServoPin; this->servoWaitTime = newWaitTime; this->highPosition = newHighPosition; this->lowPosition = newLowPosition; this->target = newHighPosition; // We asume that the arms are initial in their this->current = newHighPosition; // upper position. this->current = 0; this->active = false; } void laag() {#if (DEBUG) Serial.print("[ArmServo::laag] arm going to low position: "); Serial.println(lowPosition); Serial.flush();#endif //DEBUG target = lowPosition; } void hoog() {#if (DEBUG) Serial.print("[Armservo::hoog] arm going to high position: "); Serial.println(highPosition); Serial.flush();#endif //DEBUG target = highPosition; } bool stepServo() { // // When the arm's position isn't at the target, we have to move the arm // if (this->target != this->current) { // // Is the servo active? if not attach it to it's pin so it can start moving // if (!this->active) {#if (DEBUG) Serial.println("{ArmServo::stepServo] attach"); Serial.flush();#endif // DEBUG this->attachServo(); } lastTimeActive = millis(); if ((lastTimeActive - this->timeOfLastStep) >= this->servoWaitTime) { // // The servo is moving to it's new location. //#if (DEBUG) Serial.print("[ArmServo::stepServo] target: "); Serial.print(this->target); Serial.print(", current: "); Serial.print(this->current);#endif //DEBUG if (this->target > this->current) { this->current++; } else { this->current--; }#if (DEBUG) Serial.print(", going to: "); Serial.println(this->current); Serial.flush();#endif // DEBUG this->myServo->write(this->current); this->timeOfLastStep = lastTimeActive; } return true; // The arm is still moving } else { if ((millis() - lastTimeActive) >= REFRESH_TIME) { // // Is the servo active? if not attach it to it's pin so it can start moving // if (!this->active) {#if (DEBUG) Serial.println("{ArmServo::stepServo] attach"); Serial.flush();#endif // DEBUG this->attachServo(); }#if (DEBUG) Serial.print(", refreshing to: "); Serial.println(this->current); Serial.flush();#endif // DEBUG this->myServo->write(this->current); this->lastTimeActive = millis(); } // // The arm reached it's position, so we can now detach the servo (if it was attached) // if (this->active) { this->detachServo(); } return false; // The arm has stopped moving } }};enum AkiStaat {aan, uit};//// A simple class that represents a complete AHOB with all its leds and two Servo's// for moving the arms//// TBD, the sound of the bells//class AHOB{ private: SingleLed *redLight; ArmServo *arm1; ArmServo *arm2; AkiStaat state = uit; public: // // Create an instance of the AHOB, using the specified Led and ArmServo objects // AHOB(SingleLed *aRedLed, ArmServo *aServo1, ArmServo *aServo2) { redLight = aRedLed; arm1 = aServo1; arm2 = aServo2; state = uit; // // AHOB is veilig, dus roodlicht uit en armen omhoog // redLight->setOn(false); arm1->hoog(); arm2->hoog(); } // // Activate the connected leds and arm servo's. TBD: the sound // void heartBeat() { redLight->heartBeat(); arm1->stepServo(); arm2->stepServo(); } void set(AkiStaat aState) {#if (DEBUG) if (state != aState) { Serial.print("[AKI::set]"); if (aState == uit) Serial.println(" uit"); else Serial.println(" aan"); }#endif state = aState; if (state == uit) { // // AHOB is veilig, dus roodlicht uit en armen omhoog // redLight->setOn(false); arm1->hoog(); arm2->hoog(); } else { // // AHOB is onvelig, dus roodlicht aan en arme omlaag // redLight->setOn(true); arm1->laag(); arm2->laag(); } }};//// Some sensors use a digital signal and keep it high when not active and drop the signal// when the sensor trips, use in that case the "laag" setting. Others use a digital signal// and are low when not active and make the signal high when the sensor trips, use in that case// the "hoog" setting.// if the sensor doesn't generate a digital signal, you have to use a pullup resistor (or INPUT_PULLUP)// and in that case specify hoog.//// The Flying Fish proximity detectors, when used in reflective mode, need the "laag" setting, // when their sensors are removed and positioned opposit each other, they need the "hoog" setting//enum SensorTrigger {laag, hoog};//// A simple class which represents the state of a single sensor//class Sensor{ private: int sensorPin; SensorTrigger type = hoog; bool state = false; // true = on, false = off unsigned long lastTime = 0; // Timer to ignore short interrupts#define JITTER_TIME 1000 // wait a second before the sensor goes off public: // // Create a new instance // Sensor(int aPin) { sensorPin = aPin; pinMode(sensorPin, INPUT_PULLUP); } // // Create a new instance // Sensor(int aPin, SensorTrigger aType) { sensorPin = aPin; type = aType; pinMode(sensorPin, INPUT_PULLUP); } // // Return the current logical state of the sensor // bool getState() { return state; } // // Monitor the physical sensor and update the state accordingly // void heartBeat() { if (digitalRead(sensorPin) == HIGH) { if (type == laag) // For sensors type laag an high signal means they are in the off state { if (state != false) // Process this signal only when the current state is ON { if (lastTime == 0) { lastTime = millis(); // start Interval } else { unsigned long currTime = millis(); if ((currTime - lastTime) >= JITTER_TIME) {#if (DEBUG) Serial.println("[Sensor::heartBeat] sensor(laag) goes off");#endif state = false; // sensor goes OFF lastTime = 0; // Reset interval timer } } } } else {#if (DEBUG) if (state != true) { Serial.println("[Sensor::heartBeat] sensor(hoog) goes on"); }#endif state = true; // Sensor goes ON lastTime = 0; // Reset interval timer } } else // sensorPin = LOW { if (type == laag) {#if (DEBUG) if (state != true) { Serial.println("[Sensor::heartBeat] sensor(laag) goes on"); }#endif state = true; // Sensor goes ON lastTime = 0; // Reset interval timer; } else { if (state != false) // Process this signal only when the current state is ON { if (lastTime == 0) { lastTime = millis(); // start Interval } else { unsigned long currTime = millis(); if ((currTime - lastTime) >= JITTER_TIME) {#if (DEBUG) Serial.println("[Sensor::heartBeat] sensor(hoog) goes off");#endif state = false; // sensor goes OFF lastTime = 0; // Reset interval timer } } } } } }};//// A finite state machine that guards a occupance of a single track//class SpoorControl{ private: Sensor *sensor1; Sensor *sensor2; SpoorState state = leeg; // // Based on the state of sensor 1 and the current state of the FSM, a new state // is calculated. // void handleSensor1(bool trigger) { if (trigger) // Sensor 1 is ON { if (state == leeg) // If the state is "leeg" it means that a train enters the section {#if (DEBUG) Serial.println("[SpoorControl::handleSensor1] state is sensor1komend");#endif state = sensor1komend; // so set it to state komend } else if (state == sensor2komend) // if the other sensor tripped first {#if (DEBUG) Serial.println("[SpoorControl::handleSensor1] state is sensor1gaand");#endif state = sensor1gaand; // The train is starting to leave the section } } else { // Sensor went OFF if (state == sensor1gaand) // If the train started to leave via sensor 1 {#if (DEBUG) Serial.println("[SpoorControl::handleSensor1] state is leeg");#endif state = leeg; // the section is "leeg" } } } // // Based on the state of sensor 2 and the current state of the FSM, a new state // is calculated. // void handleSensor2(bool trigger) { if (trigger) // Sensor 2 is ON { if (state == leeg) // Is the section "leeg"? {#if (DEBUG) Serial.println("[SpoorControl::handleSensor2] state is sensor2komend");#endif state = sensor2komend; // A Train entered the section via sensor 2 } else if (state == sensor1komend) // No a train entered the section via the other sensor {#if (DEBUG) Serial.println("[SpoorControl::handleSensor2] state is sensor2gaand");#endif state = sensor2gaand; // Start the leaving process } } else { if (state == sensor2gaand) // A train is in the process of leaving {#if (DEBUG) Serial.println("[SpoorControl::handleSensor2] state is leeg");#endif state = leeg; // Section is empty again } } } public: // // Create an instance with two sensors // SpoorControl(Sensor *aSensor1, Sensor *aSensor2) { sensor1 = aSensor1; sensor2 = aSensor2; } // // Get give the sensors a kick and check their state so we can update our own // state. // void heartBeat() { sensor1->heartBeat(); sensor2->heartBeat(); handleSensor1(sensor1->getState()); handleSensor2(sensor2->getState()); } // // Return the current state of the section // SpoorState getState() { return state; }};//// Create 2 FSM controling two track sections//#define SENSOR1_PIN A0 // Analoge pin A0#define SENSOR2_PIN A1 // Analoge pin A1#define SENSOR3_PIN A2 // Analoge pin A2#define SENSOR4_PIN A3 // Analoge pin A3SpoorControl spoor1(new Sensor(SENSOR1_PIN, laag), new Sensor(SENSOR2_PIN, laag)); // Sensor works reflectiveSpoorControl spoor2(new Sensor(SENSOR3_PIN, laag), new Sensor(SENSOR4_PIN, laag));#define LED1_PIN A5 // Analoge pin A5SingleLed roodlicht(LED1_PIN, false);#define SERVO1_PIN 11 // PWM pin 11#define SERVO1_WACHTTIJD 40 // Wachttijd (in MS) tussen 2 servo stapjes, Hogere waarden = langzamer#define SERVO1_LAAGPOSITIE 45 // Positie arm in laag stand#define SERVO1_HOOGPOSITIE 100 // Positie arm in hoog stand#define SERVO2_PIN 12 // PWM pin 12#define SERVO2_WACHTTIJD 40 // Wachttijd (in MS) tussen 2 servo stapjes, Hogere waarden = langzamer#define SERVO2_LAAGPOSITIE 45 // Positie arm in laag stand#define SERVO2_HOOGPOSITIE 100 // Positie arm in hoog standArmServo arm_een(SERVO1_PIN, SERVO1_WACHTTIJD, SERVO1_LAAGPOSITIE, SERVO1_HOOGPOSITIE);ArmServo arm_twee(SERVO2_PIN, SERVO2_WACHTTIJD, SERVO2_LAAGPOSITIE, SERVO2_HOOGPOSITIE);//// Create an instance of the AHOB//AHOB ahob(&roodlicht, &arm_een, &arm_twee);void setup(){#if (DEBUG) //Serial.begin(9600); Serial.begin(115200); while (!Serial); Serial.println("Starting"); Serial.flush();#endif //DEBUG}void loop(){ spoor1.heartBeat(); // These object must monitor their sensors spoor2.heartBeat(); // so they can update their states if ((spoor1.getState() == leeg) && (spoor2.getState() == leeg)) { ahob.set(uit); // Stop sound and flashing red lights } else { ahob.set(aan); // Start flashing red lights and sound } ahob.heartBeat(); // Let the AKI update it's connected bells and leds}