The rotary encoder may not be the best way to cycle the lights. It did help me visualize and understand some of the coding needed to make the sequence of lights turn on and off. Below is the code that I modified to sequence the leds.
I am only using 48 out of the 255 counts to simulate one rotation of the camshaft (2 rotations of the crank). The goal is to hopefully only need one rotation and not have duplicate coding just to take up the full range values for the encoder. How would I decrease the 0-255 range of the encoder into one turn? 256/48=5.3333??
/* Rotary encoder read example */
#define ENC_A 8
#define ENC_B 9
#define ENC_PORT PINB
int intake = 2; //intake led
int comp = 3; //compression led
int spark = 4; //spark led flashing
int ext = 5; // exhaust led
void setup()
{
/* Setup encoder pins as inputs */
pinMode(ENC_A, INPUT);
digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT);
digitalWrite(ENC_B, HIGH);
Serial.begin (115200);
Serial.println("Start");
//led outputs
pinMode(intake, OUTPUT);
pinMode(comp, OUTPUT);
pinMode(spark, OUTPUT);
pinMode(ext, OUTPUT);
}
void loop()
{
static uint8_t counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
/**/
tmpdata = read_encoder();
if( tmpdata ) {
Serial.print("Counter value: ");
Serial.println(counter, DEC);
counter += tmpdata;
if(counter == 0){
digitalWrite(intake, HIGH); // intake led turn on
}
if(counter == 11){
digitalWrite(intake, LOW); //intake led turn off
}
if(counter == 12){
digitalWrite(comp, HIGH);// compression led turn on
}
if(counter == 23){
digitalWrite(comp, LOW); // compression led turn off
}
if(counter == 24){
digitalWrite(spark, HIGH); // spark led flash on
}
if(counter == 35){
digitalWrite(spark, LOW); // spark led off
}
if(counter == 36){
digitalWrite(ext, HIGH); // exhaust led turn on
}
if(counter == 48){
digitalWrite(ext, LOW); // exhaust led turn off
}
}
}
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
static uint8_t old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}
Could the same code work using a hall effect sensor? The ducati has a hall effect sensor positioned directly at the counter gear spinning at 1/2 crank speed. The sensor has the following data:
Technical Data
Supply voltage.....................................4.5 > 24 Vdc
Nominal sensing distance............................1.5 mm
Max be increased in proportion to pick up size
Current (typ) .................................................1 0 mA
Current (max) ...............................................20 mA
Enviroment sealing................................... IP67
Weight............................................ ..............30 g
Temperature range ........................ -30 > + 130 °C
Tightening torque........................................... 6 Nm
Output............................................ ...........PNP
Output sink voltage ............................0.4v Max
Trigger type........................................... ferrous
Electrical connections
Pin 1 Gnd
Pin 2 signal
Pin 3 VBB