Voila .
#include <NmraDcc.h>
#include <Servo.h>
#include <PWMFreq.h>
#define This_Decoder_Address 3
struct CVPair
{
uint16_t CV;
uint8_t Value;
};
CVPair FactoryDefaultCVs [] =
{
// The CV Below defines the Short DCC Address
{CV_MULTIFUNCTION_PRIMARY_ADDRESS, This_Decoder_Address},
// These two CVs define the Long DCC Address
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, 0},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, This_Decoder_Address},
// ONLY uncomment 1 CV_29_CONFIG line below as approprate
// {CV_29_CONFIG, 0}, // Short Address 14 Speed Steps
{CV_29_CONFIG, CV29_F0_LOCATION}, // Short Address 28/128 Speed Steps
// {CV_29_CONFIG, CV29_EXT_ADDRESSING | CV29_F0_LOCATION}, // Long Address 28/128 Speed Steps
};
//xxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Début Essai Sens des feux
//xxxxxxxxxxxxxxxxxxxxxxxxxxxx
// const int FEUX = 13;
//xxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Fin Essai Sens des feux
//xxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Début Essai Eclairage cabine F0
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// const int ECLAIRAGE_CABINE = 13;
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Fin Essai Eclairage cabine
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Début Essai Servo pantographe
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Servo obj_panthographe;
unsigned int signal = 0; // permet de gérer le front montant-descendant de la commande de levée-descente des pantographes
unsigned int signalPrec = 0; // permet de gérer le front montant-descendant de la commande de levée-descente des pantographes
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Fin Essai Servo pantographe
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
NmraDcc Dcc ;
uint8_t FactoryDefaultCVIndex = 0;
// Uncomment this line below to force resetting the CVs back to Factory Defaults
// FactoryDefaultCVIndex = sizeof(FactoryDefaultCVs)/sizeof(CVPair);
void notifyCVResetFactoryDefault()
{
// Make FactoryDefaultCVIndex non-zero and equal to num CV's to be reset
// to flag to the loop() function that a reset to Factory Defaults needs to be done
FactoryDefaultCVIndex = sizeof(FactoryDefaultCVs)/sizeof(CVPair);
};
// Uncomment the #define below to print all Speed Packets
#define NOTIFY_DCC_SPEED
#ifdef NOTIFY_DCC_SPEED
void notifyDccSpeed( uint16_t Addr, DCC_ADDR_TYPE AddrType, uint8_t Speed, DCC_DIRECTION Dir, DCC_SPEED_STEPS SpeedSteps )
{
Serial.print("notifyDccSpeed: Addr: ");
Serial.print(Addr,DEC);
Serial.print( (AddrType == DCC_ADDR_SHORT) ? "-S" : "-L" );
Serial.print(" Speed: ");
Serial.print(Speed,DEC);
Serial.print(" Steps: ");
Serial.print(SpeedSteps,DEC);
Serial.print(" Dir: ");
Serial.println( (Dir == DCC_DIR_FWD) ? "Forward" : "Reverse" );
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Début Essai Sens des feux
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// pinMode( FEUX, OUTPUT );
// if( Dir == 1)
// digitalWrite( FEUX, HIGH );
// else
// digitalWrite( FEUX, LOW );
//xxxxxxxxxxxxxxxxxxxxxxxxxxx
// Fin Essai Sens des feux
//xxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Début Essai Gestion vitesse (en cours)
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//setPWMFrequency(PINS_3_11, PWM_125); // 4k, 1k, 500, 250, 125, 30
//int pin1Moteur1=12; //cmd 1 du moteur 1
//int pin2Moteur1=8; // cmd 2 du moteur 1
//int pinPWMoteur1=3;// PMM du moteur 1
//int pin1Moteur2=2;// cmd 1 du moteur 2
//int pin2Moteur2=4;// cmd 2 du moteur 2
//int pinPWMoteur2=11;// PWM du moteur 2
//
//pinMode( pin1Moteur1, OUTPUT );
//pinMode( pin2Moteur1, OUTPUT );
//pinMode( pinPWMoteur1, OUTPUT );
//pinMode( pin1Moteur2, OUTPUT );
//pinMode( pin2Moteur2, OUTPUT );
//pinMode( pinPWMoteur2, OUTPUT );
//
//int SpeedPrec
//
// if( (Speed)== 0) // Arret urgence
// {
// analogWrite(pinPWMoteur1,LOW);//Mettre moteur à 0
// analogWrite(pinPWMoteur2,LOW);
// digitalWrite(pin1Moteur1,LOW);
// digitalWrite(pin2Moteur1,LOW);
// digitalWrite(pin1Moteur2,LOW);
// digitalWrite(pin2Moteur2,LOW);
// }
// else
// if(Speed != SpeedPrec) // Détection changement vitesse (à partir du potentiomêtre MS2)
// {
// if( (Dir)== 1) // sens de marche avant
// {
// digitalWrite(pin1Moteur1,HIGH);
// digitalWrite(pin2Moteur1,LOW);
// digitalWrite(pin1Moteur2,HIGH);
// digitalWrite(pin2Moteur2,LOW);
// analogWrite(pinPWMoteur1,Speed);
// analogWrite(pinPWMoteur2,Speed);
//
// for (pos =SpeedPrec; pos <= Speed; pos++)
// }
// else
// if( (Dir)== 0) // sens de marche arrière
// {
//
// digitalWrite(pin1Moteur1,LOW);
// digitalWrite(pin2Moteur1,HIGH);
// digitalWrite(pin1Moteur2,LOW);
// digitalWrite(pin2Moteur2,HIGH);
// analogWrite(pinPWMoteur1,Speed);
// analogWrite(pinPWMoteur2,Speed);
// }
// SpeedPrec = Speed; // Détection changement vitesse
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Fin Essai Gestion vitesse (en cours)
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
};
#endif
// Uncomment the #define below to print all Function Packets
#define NOTIFY_DCC_FUNC
#ifdef NOTIFY_DCC_FUNC
void notifyDccFunc(uint16_t Addr, DCC_ADDR_TYPE AddrType, FN_GROUP FuncGrp, uint8_t FuncState)
{
Serial.print("notifyDccFunc: Addr: ");
Serial.print(Addr,DEC);
Serial.print( (AddrType == DCC_ADDR_SHORT) ? 'S' : 'L' );
Serial.print(" Function Group: ");
Serial.print(FuncGrp,DEC);
switch( FuncGrp )
{
#ifdef NMRA_DCC_ENABLE_14_SPEED_STEP_MODE
case FN_0:
Serial.print(" FN0: ");
Serial.println((FuncState & FN_BIT_00) ? "1 " : "0 ");
break;
#endif
case FN_0_4:
if(Dcc.getCV(CV_29_CONFIG) & CV29_F0_LOCATION) // Only process Function 0 in this packet if we're not in Speed Step 14 Mode
{
Serial.print(" FN 0: ");
Serial.print((FuncState & FN_BIT_00) ? "1 ": "0 ");
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Début Essai Eclairage cabine F0
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Serial.print("FuncState:");
// Serial.print(FuncState);
// Serial.print("FN_BIT_00:");
// Serial.print(FN_BIT_00);
//
// pinMode( ECLAIRAGE_CABINE, OUTPUT );
// if( (FuncState && FN_BIT_00) == 1)
// digitalWrite( ECLAIRAGE_CABINE, HIGH );
// else
// digitalWrite( ECLAIRAGE_CABINE, LOW );
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Fin Essai Eclairage cabine F0
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
Serial.print(" FN 1-4: ");
Serial.print((FuncState & FN_BIT_01) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_02) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_03) ? "1 ": "0 ");
Serial.println((FuncState & FN_BIT_04) ? "1 ": "0 ");
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Debut Essai Pantographe F01
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Serial.println("FuncState:");
Serial.println(FuncState);
Serial.println("FN_BIT_01:");
Serial.println(FN_BIT_01);
int pos;
signal = (FuncState & FN_BIT_01); // Détection front montant
Serial.println("signal:");
Serial.println(signal);
if(signal != signalPrec) // Détection front montant-descendant
{
obj_panthographe.attach(13);
Serial.println("FuncState1:");
Serial.println(FuncState & FN_BIT_01);
if( (signal)== 1) // Condition MS2-F01 = 1
{
for (pos =0; pos <= 255; pos++){ // goes from 0 degrees to 180 degrees in steps of 1 degree
obj_panthographe.write(pos); // tell servo to go to position in variable 'pos'
Serial.print("MONTEE PANTOGRAPHE:");
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
}
else // Condition MS2-F01 == 1
Serial.print("FuncState0:");
Serial.println(FuncState);
for (pos =255; pos >= 0; pos--){ // goes from 180 degrees to 0 degrees
obj_panthographe.write(pos); // tell servo to go to position in variable 'pos'
Serial.print("DESCENTE PANTOGRAPHE:");
Serial.println(pos);
delay(15); // waits 15ms for the servo to reach the position
}
// }
obj_panthographe.detach(13);
}
signalPrec = signal; // Détection front montant
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Fin Essai Pantographe F01
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
break;
case FN_5_8:
Serial.print(" FN 5-8: ");
Serial.print((FuncState & FN_BIT_05) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_06) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_07) ? "1 ": "0 ");
Serial.println((FuncState & FN_BIT_08) ? "1 ": "0 ");
break;
case FN_9_12:
Serial.print(" FN 9-12: ");
Serial.print((FuncState & FN_BIT_09) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_10) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_11) ? "1 ": "0 ");
Serial.println((FuncState & FN_BIT_12) ? "1 ": "0 ");
break;
case FN_13_20:
Serial.print(" FN 13-20: ");
Serial.print((FuncState & FN_BIT_13) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_14) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_15) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_16) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_17) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_18) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_19) ? "1 ": "0 ");
Serial.println((FuncState & FN_BIT_20) ? "1 ": "0 ");
break;
case FN_21_28:
Serial.print(" FN 21-28: ");
Serial.print((FuncState & FN_BIT_21) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_22) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_23) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_24) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_25) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_26) ? "1 ": "0 ");
Serial.print((FuncState & FN_BIT_27) ? "1 ": "0 ");
Serial.println((FuncState & FN_BIT_28) ? "1 ": "0 ");
break;
}
}
#endif
// Uncomment the #define below to print all DCC Packets
#define NOTIFY_DCC_MSG
#ifdef NOTIFY_DCC_MSG
void notifyDccMsg( DCC_MSG * Msg)
{
Serial.print("notifyDccMsg: ");
for(uint8_t i = 0; i < Msg->Size; i++)
{
Serial.print(Msg->Data[i], HEX);
Serial.write(' ');
}
Serial.println();
}
#endif
// This function is called by the NmraDcc library when a DCC ACK needs to be sent
// Calling this function should cause an increased 60ma current drain on the power supply for 6ms to ACK a CV Read
const int DccAckPin = 15 ;
void notifyCVAck(void)
{
Serial.println("notifyCVAck") ;
digitalWrite( DccAckPin, HIGH );
delay( 8 );
digitalWrite( DccAckPin, LOW );
}
void setup()
{
Serial.begin(115200);
Serial.println("NMRA Dcc Multifunction Decoder Demo 1");
// Configure the DCC CV Programing ACK pin for an output
pinMode( DccAckPin, OUTPUT );
digitalWrite( DccAckPin, LOW );
// Setup which External Interrupt, the Pin it's associated with that we're using and enable the Pull-Up
Dcc.pin(0, 2, 0);
// Call the main DCC Init function to enable the DCC Receiver
//Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER | CV29_OUTPUT_ADDRESS_MODE, 0 );
Dcc.init( MAN_ID_DIY, 10, FLAGS_MY_ADDRESS_ONLY, 0 );
// Uncomment to force CV Reset to Factory Defaults
notifyCVResetFactoryDefault();
}
void loop()
{
// You MUST call the NmraDcc.process() method frequently from the Arduino loop() function for correct library operation
Dcc.process();
if( FactoryDefaultCVIndex && Dcc.isSetCVReady())
{
FactoryDefaultCVIndex--; // Decrement first as initially it is the size of the array
Dcc.setCV( FactoryDefaultCVs[FactoryDefaultCVIndex].CV, FactoryDefaultCVs[FactoryDefaultCVIndex].Value);
}
}