I'm trying to make something like this: LEXUS LFA V10 1LRGUE (even fire) firing order AudioVisual demonstration 1 2 3 4 7 8 9 10 5 6 - YouTube, but I dont know how I would convert an RPM value (like 8000) to microseconds for delayMicroseconds(), how would I do this?
8000 RPM is 133.3 rotations per second.
Seconds per rotation is 1/133.3 = 0.0075 seconds
How would I convert that in code format? Also, I was talking about microseconds, not seconds.
There are 1 million microseconds in one second.
You can't use a delay for timing unless you know how long the rest of the code takes. Better to use 'micros()' for timing.
float RPM = 8000.0;
const unsigned Cylinders = 10;
unsigned CylinderIndex = 0;
// LED pins for cylinders in firing order:
// 1 2 3 4 7 8 9 10 5 6
const byte CylinderPins[Cylinders] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
void setup()
{
for (size_t i = 0; i < Cylinders; i++)
{
digitalWrite(CylinderPins[i], LOW);
pinMode(CylinderPins[i], OUTPUT);
}
}
void loop()
{
float microsecondsPerRevolution = 1000000.0 / (RPM * 60);
unsigned long microsecondsPerCylinder = microsecondsPerRevolution / Cylinders;
unsigned long currentMicros = micros();
static unsigned long lastCylinderTime = 0;
if (currentMicros - lastCylinderTime >= microsecondsPerCylinder)
{
// Edit: OOPS. I forgot this line. Again.
lastCylinderTime = currentMicros;
digitalWrite(CylinderPins[CylinderIndex], LOW);
CylinderIndex++;
if (CylinderIndex >= Cylinders)
CylinderIndex = 0;
digitalWrite(CylinderPins[CylinderIndex], HIGH);
}
}