Single cylinder Arduino CDI code works, but does need a little tweeking. Can you help please?

Thread Starter

Galumpo77

Joined Jan 19, 2013
5
// This Arduino Uno code for a single cylinder bike, it reads an input pulse and outputs a SCR pulse with a delay determined by RPM.
// I found it online and I bench tested it with a dual beam scope and it looks like it will work well enough for me, but for one issue.
// Q: I'd like to mute off the SCR pulse on pin 0 below 1500rpm (enabling me to use an available later fixed 10 deg. BTDC SCR pulse for better stability).
// Is there simple mod to this code that would do this? See // following 'attach interrupt'. Cheers!

Mod: added Code tags to your program text.

C-like:
#include <math.h>


#include <avr/power.h>


int trig = 2;


int led = 3;


int SCR = 0;


int deg_retard = 0;


long spark_time = 0;


long micro_wait;


unsigned long micro_per_deg;


//rpm stuff


volatile byte trigger_count;


unsigned long rpm;


unsigned long timeold;


void setup()


{


if (F_CPU == 16000000) clock_prescale_set(clock_div_1);


pinMode(trig, INPUT);


pinMode(SCR, OUTPUT);


pinMode(led, OUTPUT);


attachInterrupt( 0, pulse, FALLING);


}


void loop()


{


if (trigger_count > 0) {


micro_per_deg = (micros()-timeold)/180;


timeold = micros();


trigger_count = 0;


if (micro_per_deg >= 110){deg_retard = 55;} // retards 55 deg. below 1500 rpm

// I want to mute off the output SCR pulse below 1500rpm

// I thought deleting this line might do it, I tried that, it all still verifies but still doesn't mute it.


if (micro_per_deg < 110 && micro_per_deg > 55){deg_retard = 45;} // retards 45 deg. between about 1500 and 3000 rpm


if (micro_per_deg < 55 ){deg_retard = 35;} // retards about 35 deg. above 3000 rpm




micro_wait = deg_retard*micro_per_deg;


}


}


void pulse()


{


trigger_count++;


fire();


}


void fire()


{


delayMicroseconds(micro_wait);


digitalWrite(SCR,HIGH); //sends transistor high


delayMicroseconds(350); // spark duration 350uS


digitalWrite(SCR,LOW); //Transistor goes low


}
 
Last edited by a moderator:
Top