The project here is to make what some call a 'chuff unit'. Basically a smoke generator that sits inside a model train and gives out puffs of smoke that are synchronised with the exhaust beats of the locomotive. Chuff Chuff Chuff!
I have a separate sound card and motor controller - both not driven by the Arduino (Seeed Xaio).
The loco is powered by a 3S Lithium pack. 5v is supplied to the Arduino side via a regulator.
The Arduino is used to check two things: that there is power going to the motor (it shouldn't be chuffing when stationary) and in order to time the chuffs correctly, it detects an input from a Hall effect sensor which is trigged by 4 magnets that revolve on an axle. Therefore there will be 4 chuffs per revolution of the wheel, which is correct for a two cylinder locomotive. The sound card also produces audible chuffs via the same trigger.
The smoke is generated by a piezo electric disc, driven by its own 5v board. The piezo requires a reasonable voltage and a sine wave somewhere in the region of 1.2mhz (I'm probably wrong here). The piezo disc atomises water and produces a vapour.
I'm simply going to switch the power to this board using a mosfet breakout.
Anyway, the project...it works, but I'm getting some strange behaviour. As the motor voltage increases, so does the output to the mosfet/chuff unit. It's not binary High/Low, instead starts at 3v with the motor at 5% and works it way up to 5v as the motor reaches 100%
The motor input is via a voltage divider - I also need to find out a way for this to work when the polarity is reversed.
Also, I have to set the onboard LED in reverse for it to light correctly, Low=On.
I'll attach a full wiring diagram when I have time to make one. But in the meantime I was wondering if anyone would mind skimming through the code to see if I've done anything obviously wrong.
Hoping to get the creases ironed out and upload a detailed build as it's a relative cheap project and probably quite helpful for other modellers too.
const int threshold = 800; // Motor Voltage Threshold
int MotorVoltagePin = A5; // Pin for monitoring motor voltage input
const int hallSensorPin = 3; // Hall effect sensor
const int ledPin = LED_BUILTIN;
const int ChuffTrig = 0; // Output to chuff unit on PIN0
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // LED for visual monitoring
pinMode(hallSensorPin, INPUT); // Hall effect sensor, LOW when magnet detected
pinMode(ChuffTrig, OUTPUT); // Output trigger to drive chuff unit
pinMode(MotorVoltagePin, INPUT); // Detect power to motor
}
void loop()
{
int MotorVoltage = analogRead(MotorVoltagePin); // find voltage from motor input
if (MotorVoltage > threshold && digitalRead(3) == LOW) //if motor voltage meets threshold & hall is triggered
{
digitalWrite(ChuffTrig, HIGH); // do a chuff
digitalWrite(ledPin, LOW); // light the LED
}
// when hall is not active
else
{
digitalWrite(ChuffTrig, LOW); // don't do a chuff
digitalWrite(ledPin, HIGH); // switch LED off
}
}