... servo's ... rustig omhoog maar vallen als een speer naar beneden.
... als je mijn laatste schets geladen hebt
... 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; // Wait time between 2 steps 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; }
this->current = newHighPosition; this->current = 0;
... public: ArmServo(int newServoPin, int newWaitTime, int newHighPosition, int newLowPosition) { this->myServo = new Servo(); this->servoPin = newServoPin; this->servoWaitTime = newWaitTime; // Wait time between 2 steps this->highPosition = newHighPosition; this->lowPosition = newLowPosition; this->target = newHighPosition; // We asume that the arms are initial in their upper position. this->current = 0; this->active = false; }
Zal vanavond de groene jumper omzetten.
Haal de groene draad eens los aan de kant van de gele draden. Laat de andere kant maar in de GND zitten. Opstart:1) Zet de Arduino aan en wacht even tot alles tot rust is gekomen. Werking:2) Druk nu de groene draad in de A0 en wacht even tot alles weer tot rust is gekomen. 3) Haal nu de groene draad los, als het goed is gebeurt er niets.4) Stop de Groene draad in A1. Ook nu gebeurt er niets. 5) Verwijderen de groene draad, nu zullen de servo's gaan bewegen.Herhaal de stappen 2 t/m 5 net zo vaak als je wilt.In een van de twee standen zullen de LEDs branden/knipperen.
#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->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 upper position if (this->highPosition > this->lowPosition) this->current = newHighPosition - 1; // But force a small initial movement else this->current = newHighPosition + 1; // But force a small initial movement 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; } bool isLaag() { if (current == lowPosition) return true; else return false; } void hoog() {#if (DEBUG) Serial.print("[Armservo::hoog] arm going to high position: "); Serial.println(highPosition); Serial.flush();#endif //DEBUG target = highPosition; } bool isHoog() { if (current == highPosition) return true; else return false; } 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() { if (arm1->isHoog() && arm2->isHoog()) { redLight->setOn(false); // Als de armen omhoog staan mag het rode licht niet branden } 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 open, armen omhoog. Het rode licht stopt pas als de armen omhoog staan // arm1->hoog(); arm2->hoog(); } else { // // AHOB is dicht, 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}
... dat stukje code is niet goed.
this->active = false;
#include <Servo.h>bool DEBUG = false; // zodra alles werkt, anders trueconst int SENSOR1_PIN = A0; // Analoge pin voor lichtsluis 1const int SENSOR2_PIN = A1; // Analoge pin voor lichtsluis 2const int SENSOR3_PIN = A2; // Analoge pin voor lichtsluis 3const int SENSOR4_PIN = A3; // Analoge pin voor lichtsluis 4const int LED_RED_PIN = A5; // Analoge pin voor rode ledsconst int LED_WHITE_PIN = A6; // Analoge pin voor witte ledsconst int BEL_PIN = A7; // Analoge pin voor belconst int SERVO1_PIN = 11; // PWM pin voor servo 1const int SERVO1_WACHTTIJD = 40; // Wachttijd (in MS) tussen 2 servo stapjes, hogere waarden = langzamerconst int SERVO1_LAAGPOSITIE = 45; // Positie arm in laag stand (0..180 graden servo)const int SERVO1_HOOGPOSITIE = 100; // Positie arm in hoog stand (0..180 graden servo)const int SERVO2_PIN = 12; // PWM pin voor servo 2const int SERVO2_WACHTTIJD = 40; // Wachttijd (in MS) tussen 2 servo stapjes, hogere waarden = langzamerconst int SERVO2_LAAGPOSITIE = 45; // Positie arm in laag standconst int SERVO2_HOOGPOSITIE = 100; // Positie arm in hoog standenum SpoorState {leeg, sensor1komend, sensor2komend, sensor1gaand, sensor2gaand };enum AkiStaat {aan, uit};enum SensorTrigger {laag, hoog};//// ---------------------------------------------------------------------------------//// Class for a single red led which flickers 50 times per// minute when on//class SingleRedLed{ private: int ledPin; bool ledOn = false; public: // // Create a new instance of a red led, connect it to the specified // digital pin and put it in the specified state (true = on, false = off) // SingleRedLed(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("[SingleRedLed::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("[SingleRedLed::heartBeat] rode led aan");#endif digitalWrite(ledPin, HIGH); // No, so switch it on } else {#if (DEBUG) Serial.println("[SingleRedLed::heartBeat] led uit");#endif digitalWrite(ledPin, LOW); // Switch it off } }};// --------------------------------class SingleWhiteLed{ 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) // SingleWhiteLed(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("[SingleWhiteLed::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("[SingleWhiteLed::heartBeat] witte led aan");#endif digitalWrite(ledPin, HIGH); // No, so switch it on } else {#if (DEBUG) Serial.println("[SingleWhiteLed::heartBeat] witte led uit");#endif digitalWrite(ledPin, LOW); // Switch it off } }};// ----------------------------------------------------------------------/* A wrapper class for controlling RC servos*/class ArmServo{ private: const int 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->active = false; this->myServo = new Servo(); this->servoPin = newServoPin; this->servoWaitTime = newWaitTime; // Wait time between 2 steps this->highPosition = newHighPosition; this->lowPosition = newLowPosition; this->target = newHighPosition; // We asume that the arms are initial in their if (this->highPosition > this->lowPosition) this->current = newHighPosition - 1; // But force a small initial movement else this->current = newHighPosition + 1; // But force a small initial movement 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; } bool isLaag() { if (current == lowPosition) return true; else return false; } void hoog() {#if (DEBUG) Serial.print("[Armservo::hoog] arm going to high position: "); Serial.println(highPosition); Serial.flush();#endif //DEBUG target = highPosition; } bool isHoog() { if (current == highPosition) return true; else return false; } bool stepServo() { // // When the arm's position is not at the target, we have to move the arm // if (this->target != this->current) { // // Is the servo active? if not attach it to its 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 its 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 its 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 its position, so we can now detach the servo (if it was attached) // if (this->active) { this->detachServo(); } return false; // The arm has stopped moving } }};//