Welcome to the forum   
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post in this topic Go to first unread post in this topic
Offline carlostau  
#1 Posted : 11 July 2020 22:30:37(UTC)
carlostau

Argentina   
Joined: 11/07/2020(UTC)
Posts: 19
Location: Buenos Aires, Castelar
Hello.
I am not sure if this is the right place to post this. Feel free to move it if you consider it is not.
I have been tinkering with this to see if I can create a suitable replacement of the 7072 control box for analog tracks.
Basically the operation is as follows:
1 Button with a Green led and 1 button with a red led.
Upon pressing a button. The matching led will remain lit and the other led will turn off.
Simultaneously, a relay connected to the marklin trafo circuit sends a 500ms pulse (this can be adjusted) to the solenoid of whatever is connected there
I sketched this on the arduino simulator (Circuits) that Tinkercad has and it works very well.
You will need 2 relays for each signal or turnout.

Here is the link in case you want to see it in action. Please disregard the wire mess :)
https://www.tinkercad.co...kndqK71sgfuHwmyVheA6HuzU

In case you want to give it a try, here is the code
// MARKLIN CONTROL BOX FOR ANALOG SYSTEMS //


int pinButton = 8; //Red Button
int LED = 2; //Red LED
int pinButton2 = 12; //Green Button
int LED2 = 10; //Green LED
int RelayRed = 3; // Red RELAY
int RelayGreen = 4; //Green RELAY
void setup() {
pinMode(pinButton, INPUT); //set the button pin as INPUT
pinMode(LED, OUTPUT);
pinMode(pinButton2, INPUT);
pinMode(RelayRed = 3, OUTPUT);
pinMode(RelayGreen = 4, OUTPUT);
}

void loop() {
int stateButton = digitalRead(pinButton);//read state red button
int stateButton2 = digitalRead(pinButton2);//read the state green button

// EVALUATES CURRENT BUTTON PRESS CONDITION
if(stateButton == 1) { //if Red Button is pressed
digitalWrite(LED, HIGH);//turns on Red LED
digitalWrite(LED2, LOW);// turns OFF Green LED
digitalWrite(RelayRed, HIGH);// sends PULSE to relay
delay(500);// 500ms pulse
digitalWrite(RelayRed, LOW);// relay OFF
}
else if (stateButton2 ==1)//if Green Button is pressed
{
digitalWrite(LED2, HIGH);//turns on Green LED
digitalWrite(LED, LOW);//turns OFF Red LED
digitalWrite(RelayGreen, HIGH);// sends PULSE to relay
delay(500);// 500ms pulse
digitalWrite(RelayGreen, LOW);// relay OFF
}






}
thanks 5 users liked this useful post by carlostau
Offline clapcott  
#2 Posted : 12 July 2020 03:19:59(UTC)
clapcott

New Zealand   
Joined: 12/12/2005(UTC)
Posts: 2,433
Location: Wellington, New_Zealand
Originally Posted by: carlostau Go to Quoted Post
// MARKLIN CONTROL BOX FOR ANALOG SYSTEMS /

Thanks for taking the time to contribute.

If I may, the real issue with any DIY is the interface.
You allude to relays, however this is a bit glib as the electronics associated with protecting the chip(arduino) are pretty important.
If you are deploying one of the Arduino "off the shelf" relay shields this does need to be noted, otherwise the electronics around your choice of relay and any protective electronics should be referenced/described

regarding some programming disciplines
IMHO ..
- using usage of LED and LED2 is untidy, consider LED1 and LED2
- mixing "1" / "2" with "Red" / "Green" terminology might be improved with consistency - i.e. always use 1/2 OR Grn/Red.
- 500ms pulse is functional and possibly needed for a M-Track generation solenoid, 200/250 is fine for C-Track. Have you considered using another pin to offer the variation to load at startup, rather than hardcodeing the value in the code.

- rather than "just waiting" 500ms with the dalay, one idea is to use the time to introduce a fade in/out of the LEDs

- The way you have coded the loop, should you hold the button down for longer than 500ms there will be a VERY QUICK relay OFF/ON. This may not be the best handling of the relay.
Sometimes pulsing is desirable, but the duty cycle is possibly better aimed at 50% rather than 99%. so including a finalising 200-500ms delay to prevent the VERY QUICK OFF/ON should be considered.
- One variation is to remove the "Off" command from the "Button On" tested condition and have a separate test for "Button Off".
Thus the relay would stay active as long as you hold the button down.
This is the kind of behavior that is sometimes required/desired for an uncoupling track.

- designing for 3 (or 4) aspects might be worthwhile to have a program project that may be used generically
- similarly, if there are pins available, provisioning for a both the pulse (as you have done) and another pin for persistent ON / OFF might be considered
Peter
thanks 2 users liked this useful post by clapcott
Offline DaleSchultz  
#3 Posted : 12 July 2020 03:38:32(UTC)
DaleSchultz

United States   
Joined: 10/02/2006(UTC)
Posts: 3,997
good posts from both so far...

I would add that if you use an ESP8266, in addition to that code, you can also get wifi access to the device in a very economical way.

I have done such a thing and can now control the lighting in my train room from software elsewhere on the network.
See https://cabin-layout.mixmox.com/2019/01/room-lighting-with-remotesign.html

I make my devices use a common interface language (RemoteSign) and have published the syntax as well as a Sequencer program that can be used to control them all.

Once you have the ability to switch turnouts and signals this way, over the network, you have the foundation of a layout digital control system!


Dale
Intellibox + own software, K-Track
My current layout: https://cabin-layout.mixmox.com
Arrival and Departure signs: https://remotesign.mixmox.com
thanks 1 user liked this useful post by DaleSchultz
Offline carlostau  
#4 Posted : 12 July 2020 04:50:39(UTC)
carlostau

Argentina   
Joined: 11/07/2020(UTC)
Posts: 19
Location: Buenos Aires, Castelar
Originally Posted by: clapcott Go to Quoted Post
Originally Posted by: carlostau Go to Quoted Post
// MARKLIN CONTROL BOX FOR ANALOG SYSTEMS /

Thanks for taking the time to contribute.
Originally Posted by: clapcott Go to Quoted Post

If I may, the real issue with any DIY is the interface.
You allude to relays, however this is a bit glib as the electronics associated with protecting the chip(arduino) are pretty important.
If you are deploying one of the Arduino "off the shelf" relay shields this does need to be noted, otherwise the electronics around your choice of relay and any protective electronics should be referenced/described

Regarding the relays. I allude to the common arduino relay modules which contain an npn transistor to work more efficiently with the circuitry (tinkercad does not have this module, so I had to use a common relay)

Originally Posted by: clapcott Go to Quoted Post

regarding some programming disciplines
IMHO ..
- using usage of LED and LED2 is untidy, consider LED1 and LED2


Of course it is untidy! this is not even a prototype.. this was a quick test to see how this worked on a virtual environment
Labeling the leds by color is the correct way. Futhermore, the proper way would be to label them as LedGreen01, LedRed01, LedGreen02, etc etc.

Originally Posted by: clapcott Go to Quoted Post

- mixing "1" / "2" with "Red" / "Green" terminology might be improved with consistency - i.e. always use 1/2 OR Grn/Red.
- 500ms pulse is functional and possibly needed for a M-Track generation solenoid, 200/250 is fine for C-Track.


This is aimed at M tracks, not C tracks.
Originally Posted by: clapcott Go to Quoted Post

Have you considered using another pin to offer the variation to load at startup, rather than hardcodeing the value in the code.

- rather than "just waiting" 500ms with the dalay, one idea is to use the time to introduce a fade in/out of the LEDs


- The way you have coded the loop, should you hold the button down for longer than 500ms there will be a VERY QUICK relay OFF/ON. This may not be the best handling of the relay.
Sometimes pulsing is desirable, but the duty cycle is possibly better aimed at 50% rather than 99%. so including a finalising 200-500ms delay to prevent the VERY QUICK OFF/ON should be considered.
- One variation is to remove the "Off" command from the "Button On" tested condition and have a separate test for "Button Off".
Thus the relay would stay active as long as you hold the button down.
This is the kind of behavior that is sometimes required/desired for an uncoupling track.

Yes that could work for uncoupling where you would release the button when appropriate.
Originally Posted by: clapcott Go to Quoted Post

- designing for 3 (or 4) aspects might be worthwhile to have a program project that may be used generically
- similarly, if there are pins available, provisioning for a both the pulse (as you have done) and another pin for persistent ON / OFF might be considered



Thanks for all the tips. Please feel free to edit the code to add the suggestions you mention. I was considering buying one of these 16 relay modules which should take care of most of the signals and turnouts on my layout which is very small.
Offline carlostau  
#5 Posted : 12 July 2020 04:53:29(UTC)
carlostau

Argentina   
Joined: 11/07/2020(UTC)
Posts: 19
Location: Buenos Aires, Castelar
Originally Posted by: DaleSchultz Go to Quoted Post
good posts from both so far...

I would add that if you use an ESP8266, in addition to that code, you can also get wifi access to the device in a very economical way.

I have done such a thing and can now control the lighting in my train room from software elsewhere on the network.
See https://cabin-layout.mixmox.com/2019/01/room-lighting-with-remotesign.html

I make my devices use a common interface language (RemoteSign) and have published the syntax as well as a Sequencer program that can be used to control them all.

Once you have the ability to switch turnouts and signals this way, over the network, you have the foundation of a layout digital control system!




Very nice indeed! Wifi would be a nice addition. I will make the tests and check the correct operation and maybe give it a shot.
thanks 1 user liked this useful post by carlostau
Offline carlostau  
#6 Posted : 14 July 2020 00:25:14(UTC)
carlostau

Argentina   
Joined: 11/07/2020(UTC)
Posts: 19
Location: Buenos Aires, Castelar
Quick update.
Upon preliminary tests. The program works very well and does what it is supposed to. I tested it with a switch track and a signal and both worked flawlessly.
I am using the arduino relay modules which have built in protection. Will post a video once I have it working.
Here's the updated code
// MARKLIN CONTROL BOX FOR ANALOG SYSTEMS //


const int RedButton01 = 8; //Red Button
const int RedLed01 = 2; //Red LED
const int GreenButton01 = 12; //Green Button
const int GreenLed01 = 10; //Green LED
int RelayRed01 = 3; // Red RELAY
int RelayGreen01 = 4; //Green RELAY
const int stateRedBt01 = 0; // intitialize button red
const int stateGreenBt01 = 0; // intitialize button green



void setup() {
pinMode(RedButton01, INPUT); //set the button pin as INPUT
pinMode(RedLed01, OUTPUT);
pinMode(GreenLed01, OUTPUT);
pinMode(GreenButton01, INPUT);
pinMode(RelayRed01 = 3, OUTPUT);
pinMode(RelayGreen01 = 4, OUTPUT);
digitalWrite(RedLed01, LOW);
digitalWrite(GreenLed01, LOW);
digitalWrite(RelayRed01, LOW);
digitalWrite(RelayGreen01, LOW);



}
void loop() {
int stateRedBt01 = digitalRead(RedButton01);//read state red button
int stateGreenBt01 = digitalRead(GreenButton01);//read the state green button

// EVALUATES CURRENT BUTTON PRESS CONDITION
if(stateRedBt01== HIGH ) { //if Red Button is pressed
digitalWrite(RedLed01, HIGH);//turns on Red LED
digitalWrite(GreenLed01, LOW);// turns OFF Green LED
digitalWrite(RelayRed01, HIGH);// sends PULSE to relay
delay(50);// 50ms pulse
digitalWrite(RelayRed01, LOW);// relay OFF
}
if (stateGreenBt01 == HIGH)//if Green Button is pressed
{
digitalWrite(RedLed01, LOW);//turns OFF Red LED
digitalWrite(GreenLed01, HIGH);//turns on Green LED
digitalWrite(RelayGreen01, HIGH);// sends PULSE to relay
delay(50);// 50ms pulse
digitalWrite(RelayGreen01, LOW);// relay OFF
}


}
Thanks
thanks 1 user liked this useful post by carlostau
Offline DaleSchultz  
#7 Posted : 14 July 2020 18:34:50(UTC)
DaleSchultz

United States   
Joined: 10/02/2006(UTC)
Posts: 3,997
I think you will need some debounce for the push buttons.
When it goes from say, off to on, what typically happens is off off off on off on off on on on on
Debounce code will check that the new state has been that way for some ms before deciding that is really is a new state.
Dale
Intellibox + own software, K-Track
My current layout: https://cabin-layout.mixmox.com
Arrival and Departure signs: https://remotesign.mixmox.com
thanks 1 user liked this useful post by DaleSchultz
Offline carlostau  
#8 Posted : 15 July 2020 01:12:37(UTC)
carlostau

Argentina   
Joined: 11/07/2020(UTC)
Posts: 19
Location: Buenos Aires, Castelar
Originally Posted by: DaleSchultz Go to Quoted Post
I think you will need some debounce for the push buttons.
When it goes from say, off to on, what typically happens is off off off on off on off on on on on
Debounce code will check that the new state has been that way for some ms before deciding that is really is a new state.


Thanks for the feedback.
Yes, I though so too, but it seems that does not affect it.
I must have done about 100 tests with 0 issues on signals, uncoupling and switch tracks.
The way it is coded allows you to push the button, which gives a 50ms "open" relay condition, but if you keep it pushed, the relay will remain open which is useful for uncoupling.
I received today a 16 relay board for arduino which should take care of 8 devices.
So far, so good.
Thanks again for your insights.
Offline DaleSchultz  
#9 Posted : 15 July 2020 02:40:01(UTC)
DaleSchultz

United States   
Joined: 10/02/2006(UTC)
Posts: 3,997
sounds good, nothing like a real test! However, you may not be detecting (by hearing) the bounce and it may be cycling the relay rapidly without you hearing it. You may also happen to be using excellent switches.

An easy way to see if you are getting bounce would be drop in some Serial.println() statements and see how many you get each time you press the buttons.

I keep an array of my input sensors, and the time of last use. I handle the debounce like this:

Code:
// sensor stuff
void checkSensors(){ // check each sensor from 1 to SensorCount
	int state;
	const byte debounce = 90; // ms
	for (byte i=1;i<=SensorCount; ++i) {
		pinMode(SensorPin[i],INPUT_PULLUP); // do this in case another command set it to OUTPUT....
		state = digitalRead(SensorPin[i]);
		
		if (state != SensorState[i] &&  millis() - SensorLastTime[i] > debounce) {  // it changed for debounce period
			SensorLastTime[i] = millis();
			
			if (state==HIGH) { 
				//some stuff
			}else{
				//other stuff
			}
			
			SensorState[i] = state; //keep the new value
			
		} // changed
	} // for each sensor
}
Dale
Intellibox + own software, K-Track
My current layout: https://cabin-layout.mixmox.com
Arrival and Departure signs: https://remotesign.mixmox.com
thanks 2 users liked this useful post by DaleSchultz
Offline carlostau  
#10 Posted : 15 July 2020 03:25:32(UTC)
carlostau

Argentina   
Joined: 11/07/2020(UTC)
Posts: 19
Location: Buenos Aires, Castelar
Originally Posted by: DaleSchultz Go to Quoted Post
sounds good, nothing like a real test! However, you may not be detecting (by hearing) the bounce and it may be cycling the relay rapidly without you hearing it. You may also happen to be using excellent switches.

An easy way to see if you are getting bounce would be drop in some Serial.println() statements and see how many you get each time you press the buttons.

I keep an array of my input sensors, and the time of last use. I handle the debounce like this:

Code:
// sensor stuff
void checkSensors(){ // check each sensor from 1 to SensorCount
	int state;
	const byte debounce = 90; // ms
	for (byte i=1;i<=SensorCount; ++i) {
		pinMode(SensorPin[i],INPUT_PULLUP); // do this in case another command set it to OUTPUT....
		state = digitalRead(SensorPin[i]);
		
		if (state != SensorState[i] &&  millis() - SensorLastTime[i] > debounce) {  // it changed for debounce period
			SensorLastTime[i] = millis();
			
			if (state==HIGH) { 
				//some stuff
			}else{
				//other stuff
			}
			
			SensorState[i] = state; //keep the new value
			
		} // changed
	} // for each sensor
}


Thanks Dale. I will try to implement it on my program
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

| Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.391 seconds.