Created
August 13, 2019 20:57
-
-
Save technerdchris/390524b6781f9ea5b8b6b4b45d40e98b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CDI tester - a pulse coil emulator | |
// (c) 2019 Chris Kelley : technerdchris@gmail.com | |
// License: SiYGR : Share if You Get Rich : If you use this and get rich, hook a brother up. | |
// see https://electronics.stackexchange.com/questions/452219/ | |
const char version[8]= {"v0.1"}; | |
const char today[15] = {"2019 AUG 9"}; | |
// LED connected to digital pin 13 | |
#define LED_PIN 13 | |
#define WHT_PIN 7 | |
#define RED_PIN 6 | |
#define POT_PIN 2 | |
// text buffer for Serial.println() | |
char text[80]; | |
const int SPIKE = 100; // how wide the + and - pulses are in uSec | |
int pot_adc; | |
int last_pot_adc; | |
// rpm will be integer divided by 50 | |
int rpm; | |
int dwell; | |
long wait; | |
#define RPM_MIN 950 | |
#define RPM_MAX 9000 | |
#define ANALOG_READ_DELAY 100 | |
void setup() { | |
Serial.begin(9600); // open the serial port at 9600 bps | |
Serial.println("---------------------------------"); | |
sprintf(text,"%s %s", version, today); | |
Serial.println(text); | |
pinMode(WHT_PIN, OUTPUT); | |
pinMode(RED_PIN, OUTPUT); | |
digitalWrite(WHT_PIN, HIGH); | |
digitalWrite(RED_PIN, HIGH); | |
pot_adc = analogRead(POT_PIN); | |
sprintf(text,"... pot ADC %u", pot_adc); | |
Serial.println(text); | |
rpm = map(pot_adc, 0, 1023, RPM_MIN, RPM_MAX); | |
setRpm(); | |
} | |
int idx = 1; | |
void loop() { | |
last_pot_adc = pot_adc; | |
pot_adc = analogRead(POT_PIN); // this takes 0.1 mSec or 100 uSec | |
int delta = pot_adc - last_pot_adc; | |
if( delta < 0 ) | |
delta = -delta; | |
if( delta > 2 ){ | |
sprintf(text,"... rpm changing detected\n last_pot_adc = %d\n pot_adc = %d", last_pot_adc, pot_adc); | |
Serial.println(text); | |
delay(200); | |
int temp = analogRead(POT_PIN); | |
while( abs(pot_adc - temp) > 25 ) { | |
sprintf(text,"... pot is changing ADC %d", temp); | |
Serial.println(text); | |
delay(200); | |
pot_adc = temp; | |
temp = analogRead(POT_PIN); | |
} | |
// pot_adc = analogRead(POT_PIN); | |
// convert range of 10 bit ADC to defined RPM range | |
rpm = map(pot_adc, 0, 1023, RPM_MIN, RPM_MAX); | |
setRpm(); | |
} | |
// high pulse | |
digitalWrite(RED_PIN, LOW); // delays 1-2 uSec | |
delayMicroseconds(SPIKE); | |
digitalWrite(RED_PIN, HIGH); | |
// dwell | |
delayMicroseconds(dwell); | |
// low pulse | |
digitalWrite(WHT_PIN, LOW); | |
delayMicroseconds(SPIKE); | |
digitalWrite(WHT_PIN, HIGH); | |
delayMicroseconds(wait); | |
idx++; | |
if( idx % 100 == 0 ){ | |
idx=0; | |
Serial.println("."); | |
} | |
} | |
// MAGIC NUMBER ALERT: the pickup is 26° of the 360° rotation: there will be 26/360 implemented. | |
// To calculate rpm to period and dwell, here's the human equation: | |
// RPM RPM -> Hz Hz -> period (uSec) period -> pulsewidth/dwell (uSec) | |
// hz = rpm/60 period = 1/rpm*10,000 dwell = period * 26 / 360 | |
// 1000 16.667 60.000 4333 | |
// ........ now start to optimize for 32 bit INT maths ............ | |
// RPM pd1 = RPM/100 pd2 = 60,000/pd1 period = pd2*10 dw1 = period / 360 dwell = dw1*26 | |
// 1000 10 6000 60000 167 4342 ERR = 8.8 uSec | |
// 7700 77 779 7790 22 572 ERR = 9.25 uSec | |
// ........ better optimize for 32 bit INT maths ............ | |
// RPM pd1 = RPM/50 pd2 = 12,000/pd1 period = pd2*100 dw1 = period / 36 dwell = (dw1*26)/10 | |
// 1000 20 600 60000 1667 4334 ERR = 0.80 uSec | |
// 5200 104 115 11500 319 829 ERR = -4.31 uSec | |
// 7700 154 78 7800 217 564 ERR = 1.25 uSec | |
// ........ best optimize for 32 bit INT maths ............ | |
// RPM pd1 = RPM/50 pd2 = 24,000/pd1 period = pd2*50 dw1 = period / 36 dwell = (dw1*26)/10 | |
// 1000 20 1200 60000 1667 4334 ERR = 0.80 uSec | |
// 5200 104 231 11550 321 835 ERR = 1.69 uSec | |
// 7700 154 156 7800 217 564 ERR = 1.25 uSec | |
void setRpm(){ | |
sprintf(text,"========== %s() ============\nrpm was set to %d", __FUNCTION__, rpm); | |
Serial.println(text); | |
// pd1 = RPM/50 pd2 = 24,000/pd1 period = pd2*50 dw1 = period / 36 dwell = (dw1*26)/10 | |
long period = rpm/50; | |
sprintf(text,"line %d:long period = rpm/50 = %ld", __LINE__, period); | |
Serial.println(text); | |
period = 24000/period; | |
sprintf(text,"line %d:period = 24000/period = %ld", __LINE__, period); | |
Serial.println(text); | |
period = period*50; | |
sprintf(text,"line %d:period = period*50 = %ld", __LINE__, period); | |
Serial.println(text); | |
dwell = period/36; | |
sprintf(text,"line %d:dwell = period/36 = %d", __LINE__, dwell); | |
Serial.println(text); | |
dwell = dwell*26; | |
sprintf(text,"line %d:dwell = dwell*26 = %d", __LINE__, dwell); | |
Serial.println(text); | |
dwell = dwell/10; | |
sprintf(text,"line %d:dwell = dwell/10 = %d", __LINE__, dwell); | |
Serial.println(text); | |
wait = period - dwell - ANALOG_READ_DELAY; | |
sprintf(text,"line %d: wait = period - dwell - ANALOG_READ_DELAY = %ld", __LINE__, wait); | |
Serial.println(text); | |
sprintf(text,"==== LEAVING %s() ====", __FUNCTION__); | |
Serial.println(text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment