Go Down

Topic: Arduino and Railway Modelling (Read 6888 times) previous topic - next topic

johnbet

Thank you I will try and let you know.

johnbet

Hi

When compiling the sketch I am getting the following error message.
Arduino: 1.8.1 (Mac OS X), Board: "Arduino/Genuino Uno"

                                                                                                                              ^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno.

??



GrooveFlotilla

Is that all the error? Did you scroll back?
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

johnbet

Hi - i jumped too soon - just tried again more slowly and yes it works fine.

I assume I can adjust the rate of the fade?

If you could dome a flicker on as in a fluorescent light that would be fantastic.

Once again thank you very much for you patience.

Regards

John


PaulRB

#19
Mar 06, 2017, 05:51 pm Last Edit: Mar 06, 2017, 05:55 pm by PaulRB
I assume I can adjust the rate of the fade?
Hi John, glad it worked OK. Yes you can adjust the speed of the fade. That "10" figure controls it. The higher the number, the slower the fade.

I will have a think about a good way to simulate the flourescent light. Should it flicker for ever, like a faulty tube, or be on steady after a few seconds?

johnbet

Hi

Just a random flicker for two to fours seconds and then on steady.

PaulRB

OK, I put the random flicker on DCC F8:
Code: [Select]

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DCC Function Decoder
// Author: Ruud Boer - September 2015
// This sketch turns an Arduino into a DCC function decoder for F0 - F12
// Output pins used: 3-19 (14-19 = A0-A5)
// The DCC signal is optically separated and fed to pin 2 (=Interrupt 0).
// Optocoupler schematics: www.rudysmodelrailway.wordpress.com/software
// Many thanks to www.mynabay.com for publishing their DCC monitor and -decoder code.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: GOTO lines 15 - 28 to configure some data!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int decoderAddress = 1830; // This is the decoder address, change into the number you want.
#define F0_pin 13 // Define the output pin for every Function number in use
#define F1_pin 14 // Available pin numbers: 3 - 19
#define F2_pin 15
#define F3_pin 16
#define F4_pin 17
#define F5_pin 18
#define F6_pin 19
#define F7_pin 3
#define F8_pin 4
#define F9_pin 5
#define F10_pin 6
#define F11_pin 7
#define F12_pin 8
/* F13-F28 commented out
  #define F13_pin 0
  #define F14_pin 0
  #define F15_pin 0
  #define F16_pin 0
  #define F17_pin 0
  #define F18_pin 0
  #define F19_pin 0
  #define F20_pin 0
  #define F21_pin 0
  #define F22_pin 0
  #define F23_pin 0
  #define F24_pin 0
*/

#include <DCC_Decoder.h>                      // Comment for 123d Test !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#define kDCC_INTERRUPT 0

byte Func[4]; // 0=L F4-F1, 1=F9-F5, 2=F12-F9, 3=F20-F13, 4=F28-F21
byte instrByte1;
int Address;

boolean RawPacket_Handler(byte pktByteCount, byte* dccPacket) {
  Address = 0;
  if (!bitRead(dccPacket[0], 7)) { //bit7=0 -> Loc Decoder Short Address
    Address = dccPacket[0];
    instrByte1 = dccPacket[1];
  }
  else if (bitRead(dccPacket[0], 6)) { //bit7=1 AND bit6=1 -> Loc Decoder Long Address
    Address = 256 * (dccPacket[0] & B00000111) + dccPacket[1];
    instrByte1 = dccPacket[2];
  }

  if (Address == decoderAddress) {
    byte instructionType = instrByte1 >> 5;
    switch (instructionType) {
      case 4: // Loc Function L-4-3-2-1
        Func[0] = instrByte1 & B00011111;
        break;
      case 5: // Loc Function 8-7-6-5
        if (bitRead(instrByte1, 4)) {
          Func[1] = instrByte1 & B00001111;
        }
        else { // Loc Function 12-11-10-9
          Func[2] = instrByte1 & B00001111;
        }
        break;
        /* F13-F28 commented out
          case 6: // Future Expansions
          switch (instrByte1&B00011111) {
            case B00011110: // Loc Function F13-F20
              Func[3]=dccPacket[pktByteCount-1];
            break;
            case B00011111: // Loc Function F21-F28
              Func[4]=dccPacket[pktByteCount-1];
            break;
          }
          break;
        */
    }
    if (Func[0]&B00010000) digitalWrite(F0_pin, HIGH); else digitalWrite(F0_pin, LOW);
    if (Func[0]&B00000001) digitalWrite(F1_pin, HIGH); else digitalWrite(F1_pin, LOW);
    if (Func[0]&B00000010) digitalWrite(F2_pin, HIGH); else digitalWrite(F2_pin, LOW);
    if (Func[0]&B00000100) digitalWrite(F3_pin, HIGH); else digitalWrite(F3_pin, LOW);
    if (Func[0]&B00001000) digitalWrite(F4_pin, HIGH); else digitalWrite(F4_pin, LOW);
    if (Func[1]&B00000001) digitalWrite(F5_pin, HIGH); else digitalWrite(F5_pin, LOW);
    if (Func[1]&B00000010) digitalWrite(F6_pin, HIGH); else digitalWrite(F6_pin, LOW);
    //if (Func[1]&B00000100) digitalWrite(F7_pin,HIGH); else digitalWrite(F7_pin,LOW);
    //if (Func[1]&B00001000) digitalWrite(F8_pin, HIGH); else digitalWrite(F8_pin, LOW);
    if (Func[2]&B00000001) digitalWrite(F9_pin, HIGH); else digitalWrite(F9_pin, LOW);
    if (Func[2]&B00000010) digitalWrite(F10_pin, HIGH); else digitalWrite(F10_pin, LOW);
    if (Func[2]&B00000100) digitalWrite(F11_pin, HIGH); else digitalWrite(F11_pin, LOW);
    if (Func[2]&B00001000) digitalWrite(F12_pin, HIGH); else digitalWrite(F12_pin, LOW);

    /*//Print DCC packet bytes for testing purposes
      for (byte n=0; n<5; n++) {
      Serial.print(Func[n],BIN);
      Serial.print(" ");
      }
      Serial.println(" ");
    */
  }
}

void fadeF7() {
  static unsigned long lastFadeTimeF7 = 0;
  static int fadeLevelF7 = 0;

  if (millis() - lastFadeTimeF7 > 10) {
   
    lastFadeTimeF7 = millis();
   
    if (Func[1]&B00000100) {
      if (fadeLevelF7 < 255) analogWrite(F7_pin, ++fadeLevelF7);
      }
    else {
      if (fadeLevelF7 > 0) analogWrite(F7_pin, --fadeLevelF7);
    }
  }
}

void flickerF8() {

  static unsigned long startTimeF8 = 0;
  static unsigned long lastFlickerF8 = 0;
  static int stateF8 = HIGH;

  if (Func[1]&B00001000) {
    if (stateF8 == LOW) {
      startTimeF8 = millis();
      stateF8 = HIGH;
    }
    else {
      if (millis() - startTimeF8 > 3000) {
        digitalWrite(F7_pin, HIGH);
      }
      else {
        if (millis() - lastFlickerF8 > 25) {
          lastFlickerF8 = millis();
          if (random(2) == 0) digitalWrite(F7_pin, HIGH); else digitalWrite(F7_pin, LOW);
        }
      }
    }
  }
  else {
    digitalWrite(F7_pin, LOW);
    stateF8 = LOW;
  }
}

void setup() {
  Serial.begin(38400);
  DCC.SetRawPacketHandler(RawPacket_Handler); // Comment for 123d Test !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  DCC.SetupMonitor( kDCC_INTERRUPT );         // Comment for 123d Test !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  for (byte n = 3; n < 20; n++) pinMode(n, OUTPUT);
}

void loop() {
  DCC.loop();   // Comment for 123d Test !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  fadeF7();
  flickerF8();
}

johnbet

Thanks - will give it a try.

Regards

John

johnbet

Hi the Flicker works but on Pin 3 with F8

The fade doesn't work at all now - but that may be as you intended.

Could I make the fade work on Pin 3 and the flicker on pin 4?

Regards

John

PaulRB

#24
Mar 07, 2017, 05:51 pm Last Edit: Mar 07, 2017, 05:53 pm by PaulRB
Oops, my bad.

In flickerF7() I put:
Code: [Select]
 else {
    digitalWrite(F7_pin, LOW);
    stateF8 = LOW;
  }

That should be F8_pin not F7_pin.

I want you to read and understand the code changes I am making. If you had been there is a chance you would have spotted my mistake. Ask me any questions you have about how the changes I made work.

johnbet

Hi

I did wonder why there was reference to F7 in the Flicker Code - I will edit the code and try again.

Thank you?

johnbet

I have now amended the code as follows
void fadeF7() {
  static unsigned long lastFadeTimeF7 = 0;
  static int fadeLevelF7 = 0;

  if (millis() - lastFadeTimeF7 > 10) {
   
    lastFadeTimeF7 = millis();
   
    if (Func[1]&B00000100) {
      if (fadeLevelF7 < 255) analogWrite(F7_pin, ++fadeLevelF7);
      }
    else {
      if (fadeLevelF7 > 0) analogWrite(F7_pin, --fadeLevelF7);
    }
  }
}

void flickerF8() {

  static unsigned long startTimeF8 = 0;
  static unsigned long lastFlickerF8 = 0;
  static int stateF8 = HIGH;

  if (Func[1]&B00001000) {
    if (stateF8 == LOW) {
      startTimeF8 = millis();
      stateF8 = HIGH;
    }
    else {
      if (millis() - startTimeF8 > 3000) {
        digitalWrite(F8_pin, HIGH);
      }
      else {
        if (millis() - lastFlickerF8 > 25) {
          lastFlickerF8 = millis();
          if (random(2) == 0) digitalWrite(F8_pin, HIGH); else digitalWrite(F8_pin, LOW);
        }
      }
    }
  }
  else {
    digitalWrite(F8_pin, LOW);
    stateF8 = LOW;
  }
}

Will now give it a try.

Thanks again for your help.

PaulRB

#27
Mar 08, 2017, 08:05 pm Last Edit: Mar 08, 2017, 08:15 pm by PaulRB
Did you forget how to use code tags?

What else did you change, apart from that correction I suggested?

johnbet

I changed any reference to F7 to F8 in the flicker routine.

I have to apologise I don't know what code tags are?

Regards

John

GrooveFlotilla

Code tags are explained in the "How to use this forum - please read" sticky, the first thread at the top of this section of the forum
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

Go Up