I changed the denominator of the equation to suit the motor and 13 seemed to work best. Currently only have the intake, exhaust, and spark working for the LEDs until I can machine pockets for the combustion and power strokes. The code works perfectly.
I will end up using a photo interrupter to switch the motor on and off with the 1950s Duncan parking meter. Once I am at that point, I may chime in again if I don't understand the coding.
const int VCOMP = 11;
const int VSPARK = 10;
const int VPOWER = 9;
const int VEXT = 8;
const int VINT = 12;
const int HCOMP = 6;
const int HSPARK = 5;
const int HPOWER = 4;
const int HEXT = 3;
const int HINT = 7;
const int RHALL = 2;
const int MOTOR = 13;
unsigned long offsetMs = 0;
unsigned long offsetMsLastCycle = 0;
float msPerDegree = 0;
long camshaftDegrees = 0;
long camshaftDegreesLast = 0 ;
long camshaftRotationCount = 0 ;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
digitalWrite(MOTOR, HIGH);
// if we are at zero degrees, record offset to millis() and calculate mS per degree.
if ( digitalRead( RHALL ) == LOW ) {
delay (500) ; // includes some button debounce to prevent very short cycles
camshaftRotationCount ++ ;
Serial.print( "Camshaft sensor detected. Number of rotations = " ) ;
Serial.println( camshaftRotationCount ) ;
offsetMs = millis() ;
msPerDegree = ( offsetMs - offsetMsLastCycle ) / 13 ;
Serial.print( "msPerDegree: " ) ;
Serial.println( msPerDegree ) ;
offsetMsLastCycle = offsetMs ; // save for next cycle
}
if ( camshaftRotationCount <= 1 ) {
if ( digitalRead( RHALL ) == HIGH ) {
Serial.println ( F("Engine speed not yet known") ) ;
}
return ;
}
//calculate current position of camshaft in degrees - fails if msPerDegree is zero.
float camshaftDegreesF = ( ( millis() - offsetMs ) / msPerDegree);
camshaftDegrees = camshaftDegreesF ;
// Serial.println(camshaftDegrees);
if (camshaftDegrees != camshaftDegreesLast ) {
Serial.println(camshaftDegrees);
switch ( camshaftDegrees ) {
case 2 : digitalWrite( HINT, HIGH) ; Serial.println( F( "HINT, HIGH" )) ; break ;
case 15 : digitalWrite( VEXT, HIGH ) ; Serial.println( F( "HPOWER, HIGH" )) ; break ;
case 60 : digitalWrite( HEXT, HIGH ) ; Serial.println( F( "HEXT, HIGH" )) ; break ;
case 61 : digitalWrite( HPOWER, LOW ) ; Serial.println( F( "HPOWER, LOW")); break;
case 90 : digitalWrite( VINT, LOW ); Serial.println( F( "VINT, LOW" )) ; break ;
case 91 : digitalWrite( VCOMP, HIGH ); Serial.println( F( "VCOMP, HIGH")); break;
case 240: digitalWrite( HINT, LOW ); Serial.println( F( "HINT, LOW" )) ; break ;
case 260: digitalWrite( VINT, HIGH ); Serial.println( F( "VINT, HIGH" )) ; break ;
case 290: digitalWrite( VEXT, LOW ); Serial.println( F( "VEXT, LOW" )) ; break ;
case 340: digitalWrite( HSPARK, HIGH ) ; Serial.println( F( "HSPARK, HIGH" )) ; break ;
case 380: digitalWrite( HSPARK, LOW ); Serial.println( F( "VPOWER, LOW" )) ; break ;
case 175: digitalWrite( HINT, LOW ); Serial.println( F( "HINT, HIGH" )) ; break ;
case 190: digitalWrite( HEXT, LOW ); Serial.println( F( "HEXT, LOW" )) ; break ;
case 191: digitalWrite( VEXT, HIGH); Serial.println( F( "VEXT, HIGH")); break;
case 192: digitalWrite( VPOWER, LOW); Serial.println( F( "VPOWER, LOW")); break;
case 540: digitalWrite( VINT, LOW ); Serial.println( F( "VINT, LOW" )) ; break ;
case 306: digitalWrite( HCOMP, HIGH); Serial.println( F( "HCOMP, HIGH")); break;
case 640: digitalWrite( VSPARK, HIGH ); Serial.println( F( "VSPARK, HIGH" )) ; break ;
case 680: digitalWrite( VSPARK, LOW ); Serial.println( F( "VSPARK, LOW" )) ; break ;
case 705: digitalWrite( HINT, HIGH ); Serial.println( F( "HINT, HIGH" )) ; break ;
}
camshaftDegreesLast = camshaftDegrees ;
}
}