Hello, A great article and right up my alley. I use JMRI with CMRI ( computer model railway interface When a train comes to a crossing and hits a sensor, the crossbucks should begin to flash. The sensor is a boolean input to a PCA 9685, say it is on channel 0 on the first board. So the board is x40 channel 0. What would be the if statement to reference this sensor and begin the flashing. Later the train crosses the road and hits a second sensor. So this all should stop when the caboose passes that second sensor. How would you code this up? thanks Bob Schworm Cleveland OH
Your triggers needs to send a signal to the Arduino controlling the PCA9685, not the PCA9685 itself. That is controlled by the Arduino. So the code overview would be having a pin on the Arduino as an input from the first sensor. In you main loop you would do a digitalRead() on the sensor pin and if it went HIGH you would have a variable, let's call it runCrossing. So the pin goes HIGH and we set runCrossing to a value of 1. Then in your main loop, you have some code that goes either side of the line crossingLights(); it would read something like if( runCrossing == 1){ crossingLights(); } Then you would have your 2nd sensor and again it would trigger an Arduino pin sending it HIGH. This time your code would read something like if(digitalRead(secondSensorPin) > 0){ //HIGH runCrossing = 0; //stop the lights flashing //Then turn off all the crossing lights //So from the example coede station.setPWM(crossingLeftLED, 0, 4096);//turn LED off station.setPWM(crossing2LeftLED, 0, 4096);//turn LED off station.setPWM(crossing3LeftLED, 0, 4096);//turn LED off station.setPWM(crossingRightLED, 0, 4096);//turn LED off station.setPWM(crossing2RightLED, 0, 4096);//turn LED off station.setPWM(crossing3RightLED, 0, 4096);//turn LED off } You will need to declare the pins etc and the variables but that's about the code you would need.
Using 2 PCA 9685, do you know if you can use one for the LEDS, and another for the servo's, (for the crossing barriers), as they use different frequencies? in your code line 161 station.setPWMFreq(1600); Can you add, for example barriers.setPWMFreq(50); where barriers is the second PCA 9685 on 0x41 This way you can control barriers and LEDS from 2 wires from your Arduino.
@@digitaltown5442 Thanks for the quick response. I have found another PCA 9685, and am going to try it with 2 boards , different frequencies on either board. Trying to keep all LEDS on one board then all servos on another.Just need to solder it up and amend my sketch. Might be a while yet, as I'm not the quickest programmer. Thanks for the insight into millis() too.
Your videos remind me of my grandfather connecting LEDs to my trucks. Thank you for that memory.
Need to find out how I can incorporate this into my train control software. Great video, well done thank you!
Hello,
A great article and right up my alley.
I use JMRI with CMRI ( computer model railway interface
When a train comes to a crossing and hits a sensor, the crossbucks should begin to flash. The sensor is a boolean input to a PCA 9685, say it is on channel 0 on the first board.
So the board is x40 channel 0.
What would be the if statement to reference this sensor and begin the flashing.
Later the train crosses the road and hits a second sensor. So this all should stop when the caboose passes that second sensor. How would you code this up?
thanks
Bob Schworm
Cleveland OH
Your triggers needs to send a signal to the Arduino controlling the PCA9685, not the PCA9685 itself. That is controlled by the Arduino.
So the code overview would be having a pin on the Arduino as an input from the first sensor.
In you main loop you would do a digitalRead() on the sensor pin and if it went HIGH you would have a variable, let's call it runCrossing. So the pin goes HIGH and we set runCrossing to a value of 1.
Then in your main loop, you have some code that goes either side of the line crossingLights();
it would read something like
if( runCrossing == 1){
crossingLights();
}
Then you would have your 2nd sensor and again it would trigger an Arduino pin sending it HIGH.
This time your code would read something like
if(digitalRead(secondSensorPin) > 0){ //HIGH
runCrossing = 0; //stop the lights flashing
//Then turn off all the crossing lights
//So from the example coede
station.setPWM(crossingLeftLED, 0, 4096);//turn LED off
station.setPWM(crossing2LeftLED, 0, 4096);//turn LED off
station.setPWM(crossing3LeftLED, 0, 4096);//turn LED off
station.setPWM(crossingRightLED, 0, 4096);//turn LED off
station.setPWM(crossing2RightLED, 0, 4096);//turn LED off
station.setPWM(crossing3RightLED, 0, 4096);//turn LED off
}
You will need to declare the pins etc and the variables but that's about the code you would need.
Using 2 PCA 9685, do you know if you can use one for the LEDS, and another for the servo's, (for the crossing barriers), as they use different frequencies?
in your code line 161 station.setPWMFreq(1600);
Can you add, for example barriers.setPWMFreq(50); where barriers is the second PCA 9685 on 0x41
This way you can control barriers and LEDS from 2 wires from your Arduino.
It's no problem to have different frequencies for each board. So you should be able to control lights and servos off the same board.
@@digitaltown5442
Thanks for the quick response. I have found another PCA 9685, and am going to try it with 2 boards , different frequencies on either board. Trying to keep all LEDS on one board then all servos on another.Just need to solder it up and amend my sketch. Might be a while yet, as I'm not the quickest programmer.
Thanks for the insight into millis() too.
Very Good, Thanks