Folks,
I'm currently involved in an ev conversion project and wrote some code (well mostly copied!) to simulate a 60-2 crankshaft position sensor. Basically the sketch should output 58 pulses , skip two , another 58 and so on. A pot on A0 provides a reference for pulse width and hence rpm. Could someone please have a look at the sketch and let me know if I have the pulse counts correct as i don't have a dso to verify the output?
/**
crank signal simulator
*/
#define PULSE_PIN 10
void setup() {
pinMode(PULSE_PIN, OUTPUT);
}
/**
Simulate the high of a tooth on a
reluctor wheel
*/
void triggerHigh(int duration) {
digitalWrite(PULSE_PIN, HIGH);
delayMicroseconds(duration);
digitalWrite(PULSE_PIN, LOW);
}
/**
Simulate the reference marker on a
reluctor wheel
*/
void triggerReference(int duration) {
// pin should be low already
delayMicroseconds(duration);
delayMicroseconds(duration); // two delays for two missing pulses.
}
/**
Simulates a 58 tooth reluctor wheel
with a 2 tooth reference
*/
void loop(){
int val = analogRead(0);
val = map(val, 0, 1023, 100, 3500);
for(int i = 0; i <= 58; i++) {
triggerHigh(val);
delayMicroseconds(val);
}
triggerReference(val);
delayMicroseconds(val);
}
many thanks. For anyone interested in the project please see http://www.e39ev.com/