DIY Arduino radio control for garden train
Re: DIY Arduino radio control for garden train
Installation of Arduino Nano:
There is a hole at the back of the boiler into the cabin. I increase a little bit the dimensions to have a goos access to the USB port on the Ardunio. Thus I can connect the Arduino to my computer without disassemble the loco.
The Arduino fits in the back of the boiler
The last part was to fit all the wires into the loco... it is not easy but possible :
There is a hole at the back of the boiler into the cabin. I increase a little bit the dimensions to have a goos access to the USB port on the Ardunio. Thus I can connect the Arduino to my computer without disassemble the loco.
The Arduino fits in the back of the boiler
The last part was to fit all the wires into the loco... it is not easy but possible :
Re: DIY Arduino radio control for garden train
That's an impressive conversion. Look forward to seeing it running. And thank you from me for the link to the wiring diagram software.
Phil
Sporadic Garden Railer who's inconsistencies know no bounds
My Line - https://gardenrails.org/forum/viewtopic ... 41&t=11077
Sporadic Garden Railer who's inconsistencies know no bounds
My Line - https://gardenrails.org/forum/viewtopic ... 41&t=11077
Re: DIY Arduino radio control for garden train
Clever idea for mounting the Nano with its usb port accessible. It will save you a lot of time and effort when tinkering with the programming.
Rik
Rik
Re: DIY Arduino radio control for garden train
Excellent. Any room for a steam loco soundcard?
Phil
Sporadic Garden Railer who's inconsistencies know no bounds
My Line - https://gardenrails.org/forum/viewtopic ... 41&t=11077
Sporadic Garden Railer who's inconsistencies know no bounds
My Line - https://gardenrails.org/forum/viewtopic ... 41&t=11077
Re: DIY Arduino radio control for garden train
To finish concerning the receiver here is the code :
I hope the comments in the code are enough to understand it.
Code: Select all
/*
Code for locomotive RUSTY
*/
#include "SPI.h"
#include "NRFLite.h"
const static uint8_t RADIO_ID = 10; // receiver identification
const static uint8_t PIN_RADIO_CE = 9; // connexion pin of nrf24l01
const static uint8_t PIN_RADIO_CSN = 10;
NRFLite _radio;
int POT_Value = 0; // received bariable for the potentiometer vaule
int ARU = 0; // emergency stop variable
int in1 = 4; // motor rotation direction
int in2 = 3; // motor rotation direction
int ENA = 5; // pwm motor
int in3 = 6; // entry for the light
int in4 = 7; // entry for the light
int LIGHT_Value = 0; // variable for the light
float analogVolt = 0; // vairable for voltage reading on pin A2 ==> 0.....1023
//packet structure including the received variables
struct RadioPacket {
int POT_Value;
int ARU;
int LIGHT_Value;
};
RadioPacket _radioData; // variable associated to the structure
void setup() {
pinMode(4, OUTPUT); // pin 4 output for in1
pinMode(3, OUTPUT); // pin 3 output for in2
pinMode(5, OUTPUT); // pin 5 output for ENA PWM motor
pinMode(6, OUTPUT); // pin 6 output for in3 lamp
pinMode(7, OUTPUT); // pin 7 output for in4 lamp
pinMode(A2, INPUT); // pin A2 voltage reading
Serial.begin(9600); // serial port for debug
if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) {
Serial.println("Cannot communicate with radio");
while (1)
;
}
}
void loop() {
while (_radio.hasData()) // awaiting character reading
{
_radio.readData(&_radioData); // reading variable radioData including the structure with 3 variables POT_Value ARU and LIGHT Value
ARU = _radioData.ARU; // writing of the value received into the declared variable
POT_Value = _radioData.POT_Value;
LIGHT_Value = _radioData.LIGHT_Value;
Serial.print("Eclairage : ");
Serial.println(LIGHT_Value);
// **** if battery voltage is under 10.8v then stop of lights and engine to save battery, otherwise continue the programm
// R1=10Kohm, R2=27Kohm ratio 0.270 max voltage of 4 batteries 16.8v*0.270=4.54v max at pin A2
// (4.54x1023)/5=929
// for the max batterie voltage we will read 929 on pin A2
// to limit undervoltage at 2.7v per element : 10.8v mini to read on pin A2
// à 10.8x0.270=2.916v
// (2.916x1023)/5=596 to limit voltage et 2.7v per element the minium read value will be 596
analogVolt = analogRead(A2); // read pin A2 variable analogVolt
Serial.println("Mesure batterie: ");
Serial.print(analogVolt);
if ((ARU == 1) || (analogVolt <= 596)) // if emergency stop OR battery voltage <10.8v
{
digitalWrite(in1, LOW); // breake to stop motor
digitalWrite(in2, LOW);
digitalWrite(in3, LOW); //light off
digitalWrite(in4, LOW);
Serial.println("Etat arret urgence :");
Serial.print(ARU);
Serial.println("Si batterie <10.8v soit <596 alors la loco stop");
Serial.println("Etat batterie =");
Serial.print(analogVolt);
}
// **** if no emergency stop or battery undervoltage ****
else {
Serial.println("Etat arret urgence : OFF");
if (POT_Value < 450) {
int mappedval = map(POT_Value, 450, 0, 80, 255); //mapping of potentiometer value to the output of L298N. If pote value is between 450 and 0 the it correspond to an output from 80 to 255. This is done to apply the power to the motore . I don't map 0 to 0 otherwise the motor will not have enough power to start and ther will be a long period of time when turning potentiometer the loco will not start
Serial.print("Reception Avant : ");
Serial.println(POT_Value); // affichage de la POT_Value du potentiometre
Serial.println(mappedval); // affichage valeur mappé pour trouver a quel valeur moteur démarre
digitalWrite(in1, HIGH); // rotation direction of motor
digitalWrite(in2, LOW);
analogWrite(ENA, mappedval); // sending of the mapped PWM value for the motor speed moteur pin 5
delay(30);
}
else if (POT_Value > 570) {
int mappedval = map(POT_Value, 570, 1024, 80, 255); // same as previously in th e other way
Serial.print("Reception Arrière : ");
Serial.println(POT_Value); // affichage de la POT_Value du potentiometre
Serial.println(mappedval); // affichage valeur mappé pour trouver a quel valeur moteur démarre
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ENA, mappedval); // commande vitesse moteur pin 5
delay(30);
}
else if (450 > POT_Value < 570) {
Serial.print("Reception Stop : ");
Serial.println(POT_Value); // affichage de la POT_Value du potentiometre
digitalWrite(in1, LOW); // if pot value btw 450 and 570 no motor power to have a centered 0 on the potentiometer
digitalWrite(in2, LOW);
}
//********* LAMP command ************
if (LIGHT_Value == 1) {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
} else if (LIGHT_Value == 0) {
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
}
}
}
Who is online
Users browsing this forum: No registered users and 2 guests