Arduino overweg
Arduino overweg
Ik had via het web https://starthardware.org/en/raylfx-lev ... h-arduino/ nagebouwd en alles werkt
behalve de servo,s,er is ook geen enkele foutmelding te zien met het flaschen naar het nano bordje.
hierbij de code.
#include <Servo.h>
/*
Rayl-FX Level Crossing Module
StartHardware.org/en
Permalink: https://starthardware.org/en/raylfx-lev ... h-arduino/
*/
/* ***** ***** Settings ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** */
int streetlightTimeout = 100; // switch-on time of the street lights (ms)
int streetlightFlickering = 1000; // the higher the value, the slower the LED flickers at pin 6
int crossBlinkSpeed = 1000; // blinking speed of the St. Andrew's cross
int servo1Min = 40; // servo 1 minimum (closed)
int servo2Min = 40; // servo 2 minimum (closed)
int servo1Max = 160; // servo 1 maximum (opened)
int servo2Max = 160; // servo 2 maximum (opened)
int boombarrierTimeouts[4] = {1000, 2000, 2000, 1000}; // boom barrier {blink, close, open, blink}
int boombarrierSpeed = 5; // 5 = fast, 25 = medium, 50 = slow
/* ***** ***** From here begins the program code, which does not need to be adjusted ***** ***** ***** ***** */
int streetlightPin1 = 3; // this pin connects to streetlights
int streetlightPin2 = 6; // this pin connects to a flickering streetlights
int crossBlinkPin1 = 5; // this pin connects to the LEDs of the St. Andrew's cross
int crossBlinkPin2 = 7; // this pin connects to the LEDs of the St. Andrew's cross
int boombarrierServoPin1 = 8; // this pin connects to a boom barrier servo
int boombarrierServoPin2 = 9; // this pin connects to a boom barrier servo
int sensorPin1 = 2; // this pin connects to a photoelectric sensor
int sensorPin2 = 4; // this pin connects to a photoelectric sensor
/* Memory Variables */
int streetlightBrightness = 0; // stores how bright the street lights shine
int myState = 0; // stores the current stage of the level crossing: 0 = clear, 1 = blink, 2 = close boom barrier, 3 = boom barrier closed, 4 = open boom barrier
boolean drivesby = false; // stores if there currently is a train driving by
/* Timer Variables */
long streetlightTimer = 0; // timer for the street lights
long myTimer = 0; // timer
long servoTimer = 0; // timer for the servos
/* Servo Objects */
Servo myServo1; // servo object 1
Servo myServo2; // servo Object 2
int myServoPosition[2] = {80, 80}; // stores current servo positions
/* Variables from the control module to determine the time of day */
boolean receive = false;
boolean receiveStarted = false;
int receiveTimeout = 10;
long receiveTimer = 0;
int receivedTime = 0;
int receivePulse = 0;
int lastReceivePulse = 0;
int receivePin = 18;
int myTime = 23;
#define PAYLOAD_SIZE 2 // mandatory for communication with master-module
int timeOfDay = 0; // stores timeOfDay of master-module (0 and 255)
byte nodePayload[PAYLOAD_SIZE]; // temporarily stores data of master-module
void setup() {
Serial.begin(115200); // starts the serial communication
pinMode(receivePin, INPUT); // receiving pin from the control module
/* Servo Variablen */
myServo1.attach(boombarrierServoPin1);
myServo2.attach(boombarrierServoPin2);
myServo1.write(servo1Min);
myServo2.write(servo2Min);
myServoPosition[0] = servo1Min;
myServoPosition[1] = servo2Min;
delay(500); // servos are moved to initial position
pinMode(crossBlinkPin1, OUTPUT);
pinMode(crossBlinkPin2, OUTPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
streetlightOff();
}
void loop() {
receiveFunction(); // execute instructions for reception
if (receiveStarted == false) { // if no data is currently being received:
if (myTime > 22) { // ***** late evening *****
streetlightOn(); // turn on street lights
} else if (myTime > 18) { // ***** evening *****
streetlightOn(); // turn on street lights
} else if (myTime > 12) { // ***** noon *****
streetlightOff(); // turn off street lights
} else if (myTime > 9) { // ***** forenoon *****
streetlightOff(); // turn off street lights
} else if (myTime > 7) { // ***** morning *****
streetlightOn(); // turn on street lights
} else { // ***** night *****
streetlightOn(); // turn on street lights
}
if ((digitalRead(sensorPin1) == 0) || (digitalRead(sensorPin2) == 0)) {
if (myState == 0) {
myState = 1;
myTimer = millis();
}
else if ((myState == 4) || (myState == 5)) myState = 2;
} else {
if (myState == 3) {
myState = 4;
myTimer = millis(); // reset timer
}
}
Serial.println(myTime);
switch (myState) {
case 0: // idle
crossOff();
break;
case 1: // blink
crossOn(); // St. Andrew's cross blinking
if (myTimer + boombarrierTimeouts[0] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 2; // go on to next state
}
break;
case 2: // close boom barrier
crossOn(); // St. Andrew's cross blinking
closeBoombarriers();
if (myTimer + boombarrierTimeouts[1] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 3; // go on to next state
}
break;
case 3: // boom barrier closed
crossOn(); // St. Andrew's cross blinking
break;
case 4: // open boom barrier
crossOn(); // St. Andrew's cross blinking
openBoombarriers();
if (myTimer + boombarrierTimeouts[2] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 5; // go on to next state
}
break;
case 5: // blink
crossOn(); // St. Andrew's cross blinking
if (myTimer + boombarrierTimeouts[3] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 0; // go on to next state
}
break;
}
}
}
void crossOn() {
if (millis() % crossBlinkSpeed * 2 < crossBlinkSpeed) {
digitalWrite(crossBlinkPin1, LOW);
digitalWrite(crossBlinkPin2, HIGH);
} else {
digitalWrite(crossBlinkPin1, HIGH);
digitalWrite(crossBlinkPin2, LOW);
}
}
void crossOff() {
digitalWrite(crossBlinkPin1, HIGH);
digitalWrite(crossBlinkPin2, HIGH);
}
void closeBoombarriers() {
if (servoTimer + boombarrierSpeed < millis()) {
if (myServoPosition[0] > servo1Min) myServoPosition[0]--;
myServo1.write(myServoPosition[0]);
if (myServoPosition[1] > servo2Min) myServoPosition[1]--;
myServo2.write(myServoPosition[1]);
servoTimer = millis();
}
}
void openBoombarriers() {
if (servoTimer + boombarrierSpeed < millis()) {
if (myServoPosition[0] < servo1Max) myServoPosition[0]++;
myServo1.write(myServoPosition[0]);
if (myServoPosition[1] < servo2Max) myServoPosition[1]++;
myServo2.write(myServoPosition[1]);
servoTimer = millis();
}
}
void streetlightOn() {
if (streetlightTimer + streetlightTimeout < millis()) {
streetlightBrightness++;
if (streetlightBrightness > 255) streetlightBrightness = 255;
streetlightTimer = millis();
analogWrite(streetlightPin1, 255 - streetlightBrightness);
}
if (random(streetlightFlickering) == 1) analogWrite(streetlightPin2, random(255) - streetlightBrightness);
}
void streetlightOff() {
streetlightBrightness = 0;
analogWrite(streetlightPin1, 255);
analogWrite(streetlightPin2, 255);
streetlightTimer = millis();
}
void receiveFunction() { // receives time of day from control-module
receivePulse = digitalRead(receivePin); // read out the receive pin
if ((receiveTimer + receiveTimeout < millis()) && (receiveStarted == true)) {
// on timeout and active reception
receiveStarted = false; // end active reception
myTime = receivedTime - 1; // store received time
receivedTime = 0; // reset the auxiliary variable for time reception
Serial.println(myTime); // serial output
}
// if a pulse is detected at the receive pin that was not there before
if ((receivePulse == 0) && (lastReceivePulse == 1)) {
receiveTimer = millis(); // restart timer
if (receiveStarted == false) receiveStarted = true; // start active reception, if not already done
receivedTime++; // there was a pulse, so increase the auxiliary variable to receive time
}
lastReceivePulse = receivePulse; // remember current state at pin for next pass
}
behalve de servo,s,er is ook geen enkele foutmelding te zien met het flaschen naar het nano bordje.
hierbij de code.
#include <Servo.h>
/*
Rayl-FX Level Crossing Module
StartHardware.org/en
Permalink: https://starthardware.org/en/raylfx-lev ... h-arduino/
*/
/* ***** ***** Settings ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** */
int streetlightTimeout = 100; // switch-on time of the street lights (ms)
int streetlightFlickering = 1000; // the higher the value, the slower the LED flickers at pin 6
int crossBlinkSpeed = 1000; // blinking speed of the St. Andrew's cross
int servo1Min = 40; // servo 1 minimum (closed)
int servo2Min = 40; // servo 2 minimum (closed)
int servo1Max = 160; // servo 1 maximum (opened)
int servo2Max = 160; // servo 2 maximum (opened)
int boombarrierTimeouts[4] = {1000, 2000, 2000, 1000}; // boom barrier {blink, close, open, blink}
int boombarrierSpeed = 5; // 5 = fast, 25 = medium, 50 = slow
/* ***** ***** From here begins the program code, which does not need to be adjusted ***** ***** ***** ***** */
int streetlightPin1 = 3; // this pin connects to streetlights
int streetlightPin2 = 6; // this pin connects to a flickering streetlights
int crossBlinkPin1 = 5; // this pin connects to the LEDs of the St. Andrew's cross
int crossBlinkPin2 = 7; // this pin connects to the LEDs of the St. Andrew's cross
int boombarrierServoPin1 = 8; // this pin connects to a boom barrier servo
int boombarrierServoPin2 = 9; // this pin connects to a boom barrier servo
int sensorPin1 = 2; // this pin connects to a photoelectric sensor
int sensorPin2 = 4; // this pin connects to a photoelectric sensor
/* Memory Variables */
int streetlightBrightness = 0; // stores how bright the street lights shine
int myState = 0; // stores the current stage of the level crossing: 0 = clear, 1 = blink, 2 = close boom barrier, 3 = boom barrier closed, 4 = open boom barrier
boolean drivesby = false; // stores if there currently is a train driving by
/* Timer Variables */
long streetlightTimer = 0; // timer for the street lights
long myTimer = 0; // timer
long servoTimer = 0; // timer for the servos
/* Servo Objects */
Servo myServo1; // servo object 1
Servo myServo2; // servo Object 2
int myServoPosition[2] = {80, 80}; // stores current servo positions
/* Variables from the control module to determine the time of day */
boolean receive = false;
boolean receiveStarted = false;
int receiveTimeout = 10;
long receiveTimer = 0;
int receivedTime = 0;
int receivePulse = 0;
int lastReceivePulse = 0;
int receivePin = 18;
int myTime = 23;
#define PAYLOAD_SIZE 2 // mandatory for communication with master-module
int timeOfDay = 0; // stores timeOfDay of master-module (0 and 255)
byte nodePayload[PAYLOAD_SIZE]; // temporarily stores data of master-module
void setup() {
Serial.begin(115200); // starts the serial communication
pinMode(receivePin, INPUT); // receiving pin from the control module
/* Servo Variablen */
myServo1.attach(boombarrierServoPin1);
myServo2.attach(boombarrierServoPin2);
myServo1.write(servo1Min);
myServo2.write(servo2Min);
myServoPosition[0] = servo1Min;
myServoPosition[1] = servo2Min;
delay(500); // servos are moved to initial position
pinMode(crossBlinkPin1, OUTPUT);
pinMode(crossBlinkPin2, OUTPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
streetlightOff();
}
void loop() {
receiveFunction(); // execute instructions for reception
if (receiveStarted == false) { // if no data is currently being received:
if (myTime > 22) { // ***** late evening *****
streetlightOn(); // turn on street lights
} else if (myTime > 18) { // ***** evening *****
streetlightOn(); // turn on street lights
} else if (myTime > 12) { // ***** noon *****
streetlightOff(); // turn off street lights
} else if (myTime > 9) { // ***** forenoon *****
streetlightOff(); // turn off street lights
} else if (myTime > 7) { // ***** morning *****
streetlightOn(); // turn on street lights
} else { // ***** night *****
streetlightOn(); // turn on street lights
}
if ((digitalRead(sensorPin1) == 0) || (digitalRead(sensorPin2) == 0)) {
if (myState == 0) {
myState = 1;
myTimer = millis();
}
else if ((myState == 4) || (myState == 5)) myState = 2;
} else {
if (myState == 3) {
myState = 4;
myTimer = millis(); // reset timer
}
}
Serial.println(myTime);
switch (myState) {
case 0: // idle
crossOff();
break;
case 1: // blink
crossOn(); // St. Andrew's cross blinking
if (myTimer + boombarrierTimeouts[0] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 2; // go on to next state
}
break;
case 2: // close boom barrier
crossOn(); // St. Andrew's cross blinking
closeBoombarriers();
if (myTimer + boombarrierTimeouts[1] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 3; // go on to next state
}
break;
case 3: // boom barrier closed
crossOn(); // St. Andrew's cross blinking
break;
case 4: // open boom barrier
crossOn(); // St. Andrew's cross blinking
openBoombarriers();
if (myTimer + boombarrierTimeouts[2] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 5; // go on to next state
}
break;
case 5: // blink
crossOn(); // St. Andrew's cross blinking
if (myTimer + boombarrierTimeouts[3] < millis()) { // when time expired
myTimer = millis(); // reset timer
myState = 0; // go on to next state
}
break;
}
}
}
void crossOn() {
if (millis() % crossBlinkSpeed * 2 < crossBlinkSpeed) {
digitalWrite(crossBlinkPin1, LOW);
digitalWrite(crossBlinkPin2, HIGH);
} else {
digitalWrite(crossBlinkPin1, HIGH);
digitalWrite(crossBlinkPin2, LOW);
}
}
void crossOff() {
digitalWrite(crossBlinkPin1, HIGH);
digitalWrite(crossBlinkPin2, HIGH);
}
void closeBoombarriers() {
if (servoTimer + boombarrierSpeed < millis()) {
if (myServoPosition[0] > servo1Min) myServoPosition[0]--;
myServo1.write(myServoPosition[0]);
if (myServoPosition[1] > servo2Min) myServoPosition[1]--;
myServo2.write(myServoPosition[1]);
servoTimer = millis();
}
}
void openBoombarriers() {
if (servoTimer + boombarrierSpeed < millis()) {
if (myServoPosition[0] < servo1Max) myServoPosition[0]++;
myServo1.write(myServoPosition[0]);
if (myServoPosition[1] < servo2Max) myServoPosition[1]++;
myServo2.write(myServoPosition[1]);
servoTimer = millis();
}
}
void streetlightOn() {
if (streetlightTimer + streetlightTimeout < millis()) {
streetlightBrightness++;
if (streetlightBrightness > 255) streetlightBrightness = 255;
streetlightTimer = millis();
analogWrite(streetlightPin1, 255 - streetlightBrightness);
}
if (random(streetlightFlickering) == 1) analogWrite(streetlightPin2, random(255) - streetlightBrightness);
}
void streetlightOff() {
streetlightBrightness = 0;
analogWrite(streetlightPin1, 255);
analogWrite(streetlightPin2, 255);
streetlightTimer = millis();
}
void receiveFunction() { // receives time of day from control-module
receivePulse = digitalRead(receivePin); // read out the receive pin
if ((receiveTimer + receiveTimeout < millis()) && (receiveStarted == true)) {
// on timeout and active reception
receiveStarted = false; // end active reception
myTime = receivedTime - 1; // store received time
receivedTime = 0; // reset the auxiliary variable for time reception
Serial.println(myTime); // serial output
}
// if a pulse is detected at the receive pin that was not there before
if ((receivePulse == 0) && (lastReceivePulse == 1)) {
receiveTimer = millis(); // restart timer
if (receiveStarted == false) receiveStarted = true; // start active reception, if not already done
receivedTime++; // there was a pulse, so increase the auxiliary variable to receive time
}
lastReceivePulse = receivePulse; // remember current state at pin for next pass
}
Re: Arduino overweg
Om het plaatje voor mij duidelijk te krijgen:
- Voed je de schakeling met een losse 5V voeding, of via de USB aansluiting van de Arduino?
- Je zegt dat alles verder werkt. Dus knipperlicht, etc. doet het allemaal?
- Heb je alle verbindingen gecontroleerd, bij voorkeur doorpiepen/nameten. Met name dupontstekkers staan bekend om hun onbetrouwbaarheid.
Re: Arduino overweg
De sketch zoals je die hebt gepost heb ik even nagebouwd op een breadboard en werkend gekregen.
Wat je nog kunt proberen om uit te zoeken:
- knipperlichten werken rood, om en om.
- verlichting werkt, waarvan 1 flikkert (simulatie defecte lamp)
- servo's werken, zodra D2 EN D4 beide HOOG zijn (simulatie van de opto sensoren)
Wat je nog kunt proberen om uit te zoeken:
- werken de lichtsluizen?
- heeft daglicht invloed op de werking?
Groet,
Rico (momfer)
Centrale: DCC-Ex | Arduino Mega + Motor shield
Besturing: EXRAIL | Traintastic | RocRail | Engine Driver | DecoderPro
Rails: Minitrix / Fleischmann Piccolo
En hier vind je de bouw van mijn kerstbaan en Starten met EXRAIL
Rico (momfer)
Centrale: DCC-Ex | Arduino Mega + Motor shield
Besturing: EXRAIL | Traintastic | RocRail | Engine Driver | DecoderPro
Rails: Minitrix / Fleischmann Piccolo
En hier vind je de bouw van mijn kerstbaan en Starten met EXRAIL
Re: Arduino overweg
Het is belangrijk dat je de servo's een eigen (goed ontkoppelde) 5V regulator geeft,
omdat de piekstromen veel hoger zijn dan de nano of uno aan kan.
Er loopt zelfs ook stroom als de servo's stil staan.
Wel even de 0 van beide voedingen doorverbinden.
My 2 cents (uit eigen ervaring)
Edit: digitale servo's zijn iets nauwkeuriger dan die 9g.
omdat de piekstromen veel hoger zijn dan de nano of uno aan kan.
Er loopt zelfs ook stroom als de servo's stil staan.
Wel even de 0 van beide voedingen doorverbinden.
My 2 cents (uit eigen ervaring)
Edit: digitale servo's zijn iets nauwkeuriger dan die 9g.
Groet van Frank
Re: Arduino overweg
@Rico,
Mooi dat je hem werkend hebt gekregen. Ik ben dus ook met een overweg bezig, zie mijn post met de vraag om code voor besturing via DCC i.p.v. lichtsluizen. Zie jij kans daarvoor?
Mooi dat je hem werkend hebt gekregen. Ik ben dus ook met een overweg bezig, zie mijn post met de vraag om code voor besturing via DCC i.p.v. lichtsluizen. Zie jij kans daarvoor?
Met vriendelijke groet, Jaap Kolenberg
Baan "Reichenbach e.o." Duits tijdperk 3, Intellibox-1, Digikeijs en Rosoft decoders, iTrain, Anyrail, Sprog2, Arduino
Foto's, filmpjes en bouwbeschrijvingen op https://www.kolenberg.eu
Baan "Reichenbach e.o." Duits tijdperk 3, Intellibox-1, Digikeijs en Rosoft decoders, iTrain, Anyrail, Sprog2, Arduino
Foto's, filmpjes en bouwbeschrijvingen op https://www.kolenberg.eu
Re: Arduino overweg
De lichtsluisen werken,en de andere verlichting ook.
ik gebruik een 5v voeding.
heb de servo,s nog wel niet doorgemeten.
ik ga eerst dan een printje bouwen met goede verbindingen,en dan nog eens testen.
ik had ook de timer gebouwd voor de verlichting,en deze werkt ook goed.
ik gebruik een 5v voeding.
heb de servo,s nog wel niet doorgemeten.
ik ga eerst dan een printje bouwen met goede verbindingen,en dan nog eens testen.
ik had ook de timer gebouwd voor de verlichting,en deze werkt ook goed.
Re: Arduino overweg
Probeer eens een heel eenvoudige servo sketch.
Met vriendelijke groet, Jaap Kolenberg
Baan "Reichenbach e.o." Duits tijdperk 3, Intellibox-1, Digikeijs en Rosoft decoders, iTrain, Anyrail, Sprog2, Arduino
Foto's, filmpjes en bouwbeschrijvingen op https://www.kolenberg.eu
Baan "Reichenbach e.o." Duits tijdperk 3, Intellibox-1, Digikeijs en Rosoft decoders, iTrain, Anyrail, Sprog2, Arduino
Foto's, filmpjes en bouwbeschrijvingen op https://www.kolenberg.eu
Re: Arduino overweg
Heb nu een servo sketch getest op een uno bordje en de servo werkt.
Het bord werkt nu,andere servo genomen, de fout zat in de servo.
Het bord werkt nu,andere servo genomen, de fout zat in de servo.