I have a project where I am going to use a model train electronic board to make a 'chuff' sound by using a reed switch on the driveshaft of our riding train to trigger a Mega 2560.
I have a magnet taped to the driveshaft and a reed switch mounted right next to it so that every turn of the tire the shaft rotates 4 times. The set up currently works, however I am getting a double-chuff and sometimes a missed chuff because of the code itself.
I think I need code that does something like read the input in the Arduino and send a pulse to the tsunami with some kind of logic that increases the pulse duration at a regular interval that increases and decreases smoothly but steadily. The current code has a Delay of 60ms (that I was told was needed for the tsunami to create the chuff output). I am thinking that at the end of the delay if the pulse comes in right then it chuffs but that might be out of sync and if it is that delay it misses the pulse and waits to long for the next one to sound proper.
Any ideas or suggestions?
Here is the code:
#include <Wire.h>
int outPin2=4; // Output pin to M+ on decoder
int inpPin3=3; // Input pin to reed switch
int prevMag=HIGH; // Variable reed switch condition
if (digitalRead(inpPin3) == LOW) { // If reed switch closed
if ((digitalRead(inpPin3) == LOW) && (prevMag == HIGH)) { // If reed switch now closed and previously open
digitalWrite(outPin2,HIGH); // Start signal to decoder
delay(60); // Required signal time to decoder
digitalWrite(outPin2,LOW); // End signal to decoder
prevMag = LOW; // Set variable to LOW
} // to eliminate multiple chuffs
delay(10);
if (digitalRead(inpPin3) == HIGH) { // If reed switch open
prevMag = HIGH; // Set variable HIGH
}
}
[quote="matthewmonge, post:1, topic:1307761, full:true"]
Hello Fellow Creatives,
I have a project where I am going to use a model train electronic board to make a 'chuff' sound by using a reed switch on the driveshaft of our riding train to trigger a Mega 2560.
The parts I am using are:
--- Soundtraxx Tsunami TSU-4400 (https://soundtraxx.com/products/tsunami2-digital-sound-decoders/tsu-4400/)
--- Elegoo R3 ATmega 2560
I have a magnet taped to the driveshaft and a reed switch mounted right next to it so that every turn of the tire the shaft rotates 4 times. The set up currently works, however I am getting a double-chuff and sometimes a missed chuff because of the code itself.
I think I need code that does something like read the input in the Arduino and send a pulse to the tsunami with some kind of logic that increases the pulse duration at a regular interval that increases and decreases smoothly but steadily. The current code has a Delay of 60ms (that I was told was needed for the tsunami to create the chuff output). I am thinking that at the end of the delay if the pulse comes in right then it chuffs but that might be out of sync and if it is that delay it misses the pulse and waits to long for the next one to sound proper.
Any ideas or suggestions?
Here is the code:
#include <Wire.h>
int outPin2=4; // Output pin to M+ on decoder
int inpPin3=3; // Input pin to reed switch
int prevMag=HIGH; // Variable reed switch condition
void setup() {
pinMode(inpPin3,INPUT_PULLUP);
pinMode(outPin2,OUTPUT);
} //// end of void setup
void loop() {
if (digitalRead(inpPin3) == LOW) { // If reed switch closed
if ((digitalRead(inpPin3) == LOW) && (prevMag == HIGH)) { // If reed switch now closed and previously open
digitalWrite(outPin2,HIGH); // Start signal to decoder
delay(60); // Required signal time to decoder
digitalWrite(outPin2,LOW); // End signal to decoder
prevMag = LOW; // Set variable to LOW
} // to eliminate multiple chuffs
delay(10);
if (digitalRead(inpPin3) == HIGH) { // If reed switch open
prevMag = HIGH; // Set variable HIGH
}
}
} //// end of void loop
[/quote]
M+ and M- on a DCC decoder are PWM outputs to drive the motor. Why are you grounding one side and trying to feed a 5V signal into the other? As soon as that decoder instructs the motor to turn there's going to be a 15-24V pulsing DC signal coming out of M+ and M-; and the Mega's not going to survive that. For that matter, the DCC decoder isn't going to react well to having the M- output grounded.
5V connects Function Common (V+)
GND connects Left Rail Pickup (LR)
VIN connects Right Rail Pickup (RR)
GND goes out to the reed switch
Pin 3 is input from reed switch
Pin 4 connects Motor+ (M+)
I was mistaken that M- is not connected to GND or anything at all
A model train guy designed the first sketch code and wired the connections. The way is was explained to me is it that the decoder listens to the motor EMF to cheater the pulse for the encoder. We recreate that pulse with the arduino.
This code will make trigger the encoder to work, but my problem is that as the speed of the input increases the chuff sound tends to miss a chuff or will double-chuff because of the delay. Is there a way to detect the inputs and create a more regular increasing or decreasing pulse that I can send to the encoder that will help smooth the delay. And even if I get it to a pulse rate that would be a maximum so it doesn't miss or double up on itself.