tail lights and front blinkers

hi all,

i am developing a project which consist of one MEGA2560 which acts as a master. it r(currently) receives four inputs : blinkr (pin 15), blinkl (pin 16), hazard (pin 19) and park (pin 17) - all are internally pulled up, set as digital inputs.
i drew a state machine chart for all possible states and poured it into code, however i cannot seem to understand how i tie the inputs to the machine…

i’d appreciate any guidance or help…

// define the events
enum class Event : int {
    BLINKR_ON = 1,
    BLINKR_OFF = 2,
    BLINKL_ON = 3,
    BLINKL_OFF = 4,
    HAZARD_ON = 5,
    HAZARD_OFF = 6,
    PARK_ON = 7,
    PARK_OFF = 8
             
};

// Define the states
enum class State : int {
    idle = 1,
    blinkr = 2,
    blinkl = 3,
    hazard = 4,  
    hazard_br = 5,
    hazard_bl = 6,
    park = 7,
    park_br = 8,
    park_bl = 9,
    hazard_park = 10,
    hazard_park_br = 11,
    hazard_park_bl = 12
    
 };

Event rd;
State currentState = State::idle;

const int pin_blinkR = 15;   //set blink right LIGHT
int blinkR = 1;
const int pin_blinkL = 16;   //set blink left LIGHT
int blinkL = 1;
const int pin_park = 17;   //set blink left LIGHT
int Park = 1;
const int pin_hazard = 19;   //set hazard LIGHT
int hazardPin = 1;



State machine(State state, Event evt) {
   
    switch(state) {
        case State::idle:
            switch(evt) {
                case Event::BLINKR_ON: return State::blinkr;  
                case Event::HAZARD_ON: return State::hazard;
                case Event::BLINKL_ON: return State::blinkl;
                case Event::PARK_ON: return State::park;
                
                default: return state;
                        };
            break;
           
         case State::blinkr:
            switch(evt) {
                case Event::BLINKR_OFF: return State::idle;
                case Event::HAZARD_ON: return State::hazard_br;
                case Event::PARK_ON: return State::park_br;
                default: return state;
                        };
            break;

           case State::blinkl:
            switch(evt) {
                case Event::BLINKL_OFF: return State::idle;
                case Event::HAZARD_ON: return State::hazard_bl;
                case Event::PARK_ON: return State::park_bl;
                default: return state;
                        };
            break;

            
          case State::hazard:
            switch(evt) {
                case Event::HAZARD_OFF: return State::idle;
                case Event::BLINKR_ON: return State::hazard_br;
                case Event::BLINKL_ON: return State::hazard_bl;
                case Event::PARK_ON: return State::hazard_park;
                default: return state;
                        };
            break;

          case State::hazard_br:
            switch(evt) {
                case Event::BLINKR_OFF: return State::hazard;
                case Event::HAZARD_OFF: return State::blinkr;
                case Event::PARK_ON: return State::hazard_park_br;
                default: return state;
                        };
            break;
          
          case State::hazard_bl:
            switch(evt) {
                case Event::BLINKL_OFF: return State::hazard;
                case Event::HAZARD_OFF: return State::blinkl;
                case Event::PARK_ON: return State::hazard_park_bl;
                default: return state;
                        };
            break;

         case State::park:
            switch(evt) {
                case Event::HAZARD_ON: return State::hazard_park;
                case Event::BLINKR_ON: return State::park_br;
                case Event::BLINKL_ON: return State::park_bl;
                case Event::PARK_OFF: return State::idle;
                default: return state;
                        };
            break;

          case State::park_br:
            switch(evt) {
                case Event::BLINKR_OFF: return State::park;
                case Event::HAZARD_ON: return State::hazard_park_br;
                case Event::PARK_OFF: return State::blinkr;
                default: return state;
                        };
            break;

        case State::park_bl:
            switch(evt) {
                case Event::BLINKL_OFF: return State::park;
                case Event::HAZARD_ON: return State::hazard_park_bl;
                case Event::PARK_OFF: return State::blinkl;
                default: return state;
                        };
            break;
        
         case State::hazard_park:
            switch(evt) {
                case Event::HAZARD_OFF: return State::park;
                case Event::BLINKR_ON: return State::hazard_park_br;
                case Event::BLINKL_ON: return State::hazard_park_bl;
                case Event::PARK_OFF: return State::hazard;
                default: return state;
                        };
            break;
         
          case State::hazard_park_br:
            switch(evt) {
                case Event::HAZARD_OFF: return State::park_br;
                case Event::BLINKR_OFF: return State::hazard_park;
                case Event::PARK_OFF: return State::hazard_br;
                default: return state;
                        };
            break;
              
          case State::hazard_park_bl:
            switch(evt) {
                case Event::HAZARD_OFF: return State::park_bl;
                case Event::BLINKL_OFF: return State::hazard_park;
                case Event::PARK_OFF: return State::hazard_bl;
                default: return state;
                        };
            break;
          
    }
}


void receiveEvent() {
 
   currentState = machine(currentState, rd);
  // Print value of incoming data
  //Serial.println(static_cast<int>(rd));
  //Serial.println(hazardPin);
  //Serial.println("-------");
  //Serial.println(static_cast<int>(currentState));

}


void setup() {

  Wire.begin();
  
  pinMode(pin_blinkR, INPUT_PULLUP);
  digitalWrite(pin_blinkR, HIGH); //set internal pull up resistor
  
  
  pinMode(pin_blinkL, INPUT_PULLUP);
  digitalWrite(pin_blinkL, HIGH); //set internal pull up resistor
  
  pinMode(pin_park, INPUT_PULLUP);
  digitalWrite(pin_park, HIGH); //set internal pull up resistor
  
  pinMode(pin_hazard, INPUT_PULLUP);
  digitalWrite(pin_hazard, HIGH); //set internal pull up resistor
  
currentState == State::idle;

  Serial.begin(9600);
}

void loop() {
blinkR = digitalRead(pin_blinkR);
blinkL = digitalRead(pin_blinkL);
Park = digitalRead(pin_park);
hazardPin = digitalRead(pin_hazard);
Serial.println(static_cast<int>(currentState));
 
if (currentState == State::idle) {  //idle = do nothig
    
     }

if (currentState == State::blinkr) {  //blink F+R right blinkers
    
    }

if (currentState == State::blinkl){  //blink F+R left blinkers
     
    }


if (currentState == State::hazard){  //blink F+R hazard mode
      
    }
  
if (currentState == State::hazard_br){  //disable blink right and remember it, then blink all hazard
     
    }


if (currentState == State::hazard_bl){  //disable blink left and remember it, then blink all hazard
    
    }


if (currentState == State::park){   //turn on F+R parking lights
    
    }

if (currentState == State::park_br){  //blink front right while park light is on, for rears send both blink right and turn on park light
    
    }

if (currentState == State::park_bl){  //blink front left while park light is on, for rears send both blink left and turn on park light
    
    }

if (currentState == State::hazard_park){    //for front blink hazard with park, for rears send both hazard and turn on park lights
    
    }

if (currentState == State::hazard_park_br){ //for front blink hazard with park, for rears send send both hazard and turn on park lights
    
    }

if (currentState == State::hazard_park_bl){ //for front blink hazard with park, for rears send send both hazard and turn on park lights
    
    }

}

Interesting question but it is missing a lot of information. From your description when you test this thing hopefully you have a big box of Mega's on hand, they will fry. Once you get that part figured out and you install it in a car, keep that box of remaining good Mega's handy. You need to study a bit of automotive electronics and understand load dump, double battery jump and reverse battery. If you really want this to work you need to design a hardware interface and do it with a schematic capture program, not the frizzy thing. You can get KiCad online free and it is good. Since you have some not complete code you need to work on that a bit. I see no setup() or loop() in your code, if it is there post it, if not add it then post it. Your IDE has a lot of great examples, I highly recommend you go through at least a dozen, build them, understand them, and make them work before starting on automotive interfacing. There is a lot of good information on line for this. I almost forgot check the laws regarding what you want to do, these points are classified as safety items.

gilshultz, thanks for your insight and comments!

you are correct - i apologize for my poor intro. a little bit on myself - i am electronics hardware and integration engineer, in the automotive field for over 10 years. my "hands-on" experience is vast but admittedly my c-programming skills are poor so i try getting some help with it here.

allow me to explain -
both front blinkers and rear tail lights are completely designed and hand built by me from scratch. they consist of Neopixel stripes. the front blinkers are essentially one individual stripe (19 RGBWW pixels) on each side which will produce blink function, parking lights function and hazard function. the rear tail lights consist of three individual stripes - one will do the blink and hazard, the second is for the parking light and break light and the third is for reverse light.
total of 4 corners. each corner have its own slave Nano to generate the visual animation of the stripes connected to it will also communicate with the master- the Mega, which according to its state machine, will send the correct command for the Nano to make .

the above setup (4 corners each with its own nano + Mega with all inputs) is already working on the bench with the blinkers and tail lights already built. i also built a test bench box consisting all inputs to the Mega (a total of about 30 digital i/o's and 7 analog inputs) which, discretely, is working correctly.

the state machine i posted here is a different approach i decided to take in order to make the Mega function correctly in the vehicle. before it, i took the "if" approach which made it all very complex and hard to debug.

the code i posted does have setup() and loop() and compiles with no errors.
however, as i mentioned in my previous post - i am missing the way to link the inputs to the state machine...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.