sábado, 22 de octubre de 2011

"Chrono Starts": Measure your 0 to 100 kph time



You can find an example using our FSM (Finite State Machine) library to create an Arduino program to measure the time to achieve 0 to 100 kph. The speed is read from the CAN bus and we are using the SetFilters() function, which uses the HW feature to only allows receive the messages with a specific ID (where the car speed is sent).

You can download the FSM library for Arduino022 from:

You need to copy it into the libraries folder.

Below, the state machine diagram of the "Chrono Starts" sketch:

(click to enlarge the diagram)

Arduino sketch:

 // -------------------------------------------------------------------------------------
// SECUduino: Chrono 0 to 100 kph
// By Igor Real
// 23/10/2011
// -------------------------------------------------------------------------------------

#include <FSM.h>
#include <LiquidCrystal.h>
#include <CAN.h>
#include "myStates.h"

#define delayShowData  7000
#define SpeedTarget    100

unsigned char myEvent;
char myByteRec;
unsigned long my0to100;
unsigned long t0;
unsigned long t_prev;
float Speed;
char bUpdateLCD;

LiquidCrystal LCD(13,12, 11, 10, 9, 8, 7);

void setup()
  func1();  //Print Initial screen
  uint16_t  Filters[6];
  uint16_t Masks[2];
  Filters[0]=0x351;  //Buffer0
  Filters[2]=0x351;  //Buffer1
  CAN.SetFilters(Filters,Masks);  //Only messages with ID=0x351 are allowed in both Rx buffers

void loop()

void ReadEvents(void)
  if (CAN.CheckNew() )
    LCD.print("Speed= ");
  if (Speed<=0){
  }else if (Speed>=SpeedTarget){
  }else if ( (Speed>0) && (Speed<SpeedTarget) ){

void func1(void)
  if (bUpdateLCD)
    LCD.print("  *- SECUduino -*");
    LCD.print("State 1");
void func2(void)
  LCD.print("State 2");

void func3(void)
  LCD.print("State 3");

void func4(void)
  LCD.print("State 4");

void func5(void)
  LCD.print("State 4");
  LCD.print(" s");

Code myStates.h:


#ifndef myStates_H
#define myStates_H

//Declaracion de las funciones
extern void func1(void);
extern void func2(void);
extern void func3(void);
extern void func4(void);
extern void func5(void);

//Declaracion del nombre de ESTADOS y de EVENTOS
#define STATE1   0x01
#define STATE2   0x02
#define STATE3          0x03
#define STATE4          0x04
#define STATE5          0x05

#define EV_Start      0x01
#define EV_100     0x02 
#define EV_Stop     0x03
#define EV_Wait     0x04

// Estructuras descriptivas de mi diagrama de flujo
const FSMClass::FSM_State_t FSM_State[] PROGMEM= {

const FSMClass::FSM_NextState_t FSM_NextState[] PROGMEM= {

//Macros para el cálculo de los tamaños de las estructuras
#define nStateFcn  sizeof(FSM_State)/sizeof(FSMClass::FSM_State_t)
#define nStateMachine  sizeof(FSM_NextState)/sizeof(FSMClass::FSM_NextState_t)


No hay comentarios:

Publicar un comentario en la entrada