help required.

Hi guys,

I have this code written to control various led's for a lighting project that will go inside a 1/6 scale model motorcycle.

as it currently stands it has 6 outputs,

1 - dipped beam
2 - high beam
3 - left indicators
4 - right indicators
5 - rear light
6 - rear brake

The problem with that is that the headlight and tail light both need 2 leds in them.

So i was wondering if someone can alter the code so there is only 1 led for high beam and dipped beam and 1 led for rear light and brake light, the idea being that the rear light and dipped beam are 50% brightness and brake light and high beam take those 2 leds up to 100% brightness with no fade time just instant increase/decrease in brightness where needed?

heres the code i have already, it includes the fader library but isn't used in the current setup,

// Motorcycle Prototype Mk1

#include <LedFader.h>
#include <LedFlasher.h>

// pin assignments PMW 3,5,6,9,10,11
const byte DippedPin = 1;          // 
const byte HighPin = 2;            // 
const byte RightIndicatorPin = 3;  //
const byte LeftIndicatorPin = 4;   // 
const byte RearLightPin = 5;       // 
const byte RearBrakePin = 6;       // 


// Flashers                     pin                  off-time  on-time       on?
LedFlasher LeftIndicator    (LeftIndicatorPin,        500,       500,     false);
LedFlasher RightIndicator   (RightIndicatorPin,       500,       500,     false);
LedFlasher RearBrake        (RearBrakePin,            700,       2000,     false);

// states for the state machine
typedef enum
  {
  initialState,
  wantDippedBeam,
  wantHighBeam,  
  wantHighBeamOff,
  wantLeftIndicator,            
  wantRightIndicator,
  wantLeftIndicatorOff,
  wantRightIndicatorOff,
  wantRearLight,
  wantRearBrake,
  wantRearBrake2,
  wantRearBrakeOff,
  wantRearBrakeOff2,  
  } states;

// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;

void setup ()
  {
  pinMode (DippedPin, OUTPUT);
  pinMode (HighPin, OUTPUT);
  pinMode (RearLightPin, OUTPUT);
    
  // set up faders, flashers  
  LeftIndicator.begin ();
  RightIndicator.begin ();
  RearBrake.begin ();
  }  // end of setup
        
void doStateChange ()
  {
  lastStateChange = millis ();    // when we last changed states
  timeInThisState = 1000;         // default one second between states

  switch (state)
   {
    case initialState:
         state = wantDippedBeam;
         break;
         
    case wantDippedBeam:
         digitalWrite (DippedPin, HIGH);
         state = wantRearLight;
         break;
         
    case wantRearLight:
         digitalWrite (RearLightPin, HIGH);
         state = wantRearBrake;
         timeInThisState = 20000;
         break;
//left Indicators/brake Lights
    case wantRearBrake:
         RearBrake.on();
         state = wantLeftIndicator;
         break;
         
    case wantLeftIndicator:
         LeftIndicator.on();
         state = wantRearBrakeOff;
         timeInThisState = 5000;
         break;
         
    case wantRearBrakeOff:
         RearBrake.off();
         state = wantLeftIndicatorOff;
         break;
    
    case wantLeftIndicatorOff:
         LeftIndicator.off();
         state = wantHighBeam;
         timeInThisState = 20000;
         break;
         
    case wantHighBeam:
         digitalWrite (HighPin, HIGH);
         state = wantRearBrake2;
         break;
         
    case wantRearBrake2:
         RearBrake.on();
         state = wantRightIndicator;
         break;
         
    case wantRightIndicator:
         RightIndicator.on();
         state = wantRearBrakeOff2;
         timeInThisState = 5000;
         break;
         
    case wantRearBrakeOff2:
         RearBrake.off();
         state = wantRightIndicatorOff;
         break;
         
    case wantRightIndicatorOff:
         RightIndicator.off();
         state = wantHighBeamOff;
         timeInThisState = 20000;
         break;
         
    case wantHighBeamOff:
         digitalWrite (HighPin, LOW);
         state = wantDippedBeam;
         break;
                
    }  // end of switch on state
  }  // end of doStateChange


void loop ()
  {
   if (millis () - lastStateChange >= timeInThisState)
     doStateChange ();
   // update faders, flashers
   RearBrake.update ();
   LeftIndicator.update ();
   RightIndicator.update ();
   
  }  // end of loop

If someone can make the changes needed that would be outstanding.

I'm not sure, where you code is braking, but take a look at this changes.

Basically, you use only 1 pin for the front light and another one for the rear light.

And when you turn them on, you use analogWrite(FrontPin, 125); //Use 255 for HIGH and 0 for LOW

Now just a question, so I know, why you define the pins using "const byte"?

And on SetUp, why only 3 pins are set as outputs, and not the 5 of them?

Thanks :wink:

// Motorcycle Prototype Mk1

#include <LedFader.h>
#include <LedFlasher.h>

// pin assignments PMW 3,5,6,9,10,11
const byte DippedPin = 1;          // NOT USED
const byte FrontPin = 9;            // PIN 9 or any with PWM
const byte RightIndicatorPin = 3;  //
const byte LeftIndicatorPin = 4;   // 
const byte RearPin = 10;       // PIN 10 or any with PWM
const byte RearBrakePin = 6;       // NOT USED


// Flashers                     pin                  off-time  on-time       on?
LedFlasher LeftIndicator    (LeftIndicatorPin,        500,       500,     false);
LedFlasher RightIndicator   (RightIndicatorPin,       500,       500,     false);
LedFlasher RearBrake        (RearBrakePin,            700,       2000,     false);

// states for the state machine
typedef enum
  {
  initialState,
  wantDippedBeam,
  wantHighBeam,  
  wantHighBeamOff,
  wantLeftIndicator,            
  wantRightIndicator,
  wantLeftIndicatorOff,
  wantRightIndicatorOff,
  wantRearLight,
  wantRearBrake,
  wantRearBrake2,
  wantRearBrakeOff,
  wantRearBrakeOff2,  
  } states;

// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;

void setup ()
  {
  pinMode (FrontPin, OUTPUT);
  pinMode (RearPin, OUTPUT);
    
  // set up faders, flashers  
  LeftIndicator.begin ();
  RightIndicator.begin ();
  RearBrake.begin ();
  }  // end of setup
        
void doStateChange ()
  {
  lastStateChange = millis ();    // when we last changed states
  timeInThisState = 1000;         // default one second between states

  switch (state)
   {
    case initialState:
         state = wantDippedBeam;
         break;
         
    case wantDippedBeam:
         analogWrite (FrontPin, 125);
         state = wantRearLight;
         break;
         
    case wantRearLight:
         analogWrite (RearPin, 125);
         state = wantRearBrake;
         timeInThisState = 20000;
         break;
//left Indicators/brake Lights
    case wantRearBrake:
         RearBrake.on();
         state = wantLeftIndicator;
         break;
         
    case wantLeftIndicator:
         LeftIndicator.on();
         state = wantRearBrakeOff;
         timeInThisState = 5000;
         break;
         
    case wantRearBrakeOff:
         RearBrake.off();
         state = wantLeftIndicatorOff;
         break;
    
    case wantLeftIndicatorOff:
         LeftIndicator.off();
         state = wantHighBeam;
         timeInThisState = 20000;
         break;
         
    case wantHighBeam:
         analogWrite (FrontPin, 255);
         state = wantRearBrake2;
         break;
         
    case wantRearBrake2:
         RearBrake.on();
         state = wantRightIndicator;
         break;
         
    case wantRightIndicator:
         RightIndicator.on();
         state = wantRearBrakeOff2;
         timeInThisState = 5000;
         break;
         
    case wantRearBrakeOff2:
         RearBrake.off();
         state = wantRightIndicatorOff;
         break;
         
    case wantRightIndicatorOff:
         RightIndicator.off();
         state = wantHighBeamOff;
         timeInThisState = 20000;
         break;
         
    case wantHighBeamOff:
         analogWrite (FrontPin, 0);
         state = wantDippedBeam;
         break;
                
    }  // end of switch on state
  }  // end of doStateChange


void loop ()
  {
   if (millis () - lastStateChange >= timeInThisState)
     doStateChange ();
   // update faders, flashers
   RearBrake.update ();
   LeftIndicator.update ();
   RightIndicator.update ();
   
  }  // end of loop

well the code i posted worked exactly as it was written to, so not really "broken" so to speak.

I'm not entirely sure on the setup stage, mostly due to it being a major variation of another sketch i have.

I'm trying to keep the sketch small as i (in kb) only have 8kb to work with so this should do the job.

thanks for the help :smiley: