Hi All, i am having an issue with some code,
it is simplay putting out a square wave of 35 pulses with one missing one,
The missing one triggers the single cam shaft pulse.
The code works fine, but i dont seem to get any difference in speed below 10,000uS in gap between pulses.
I need to run from 800RPM to 8000RPM, with 36 events per revolution, thats a speed of 4800Hz
Am i runnign out of Processor capabilty to do this?
Code below
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float TPS1 = 0.0;
float TPS2 = 0.0;
int sensorValue = 0;
const int crankPin = 13; // the number of the LED pin //crank pulse
const int camPin = 6; // the number of the LED pin // cam pulse
int ledState = HIGH; // ledState used to set the LED
int ledState2 = LOW; // ledState used to set the LED
unsigned long previousMicros = 0;
unsigned long interval = 5000;
unsigned long previousTime = 0;
int i = 0;
void setup() {
Serial.begin(9600);
pinMode(crankPin, OUTPUT); // Sets Pin 13 as Digital Output
pinMode(camPin, OUTPUT); // sets Pin 6 as digital Output
lcd.begin(20,4);
}
void loop()
{
sensorValue = analogRead(A0);
TPS1 = (sensorValue*5.0)/1024.0;
Serial.println(TPS1,2);
sensorValue = analogRead(A1);
TPS2 = (sensorValue*5.0)/1024.0;
Serial.println(TPS2,2);
lcd.setCursor(0,0);
lcd.print ("TPS1");
lcd.setCursor(0,1);
lcd.print ("TPS2");
lcd.setCursor(5,0);
lcd.print(TPS1,2);
lcd.setCursor(5,1);
lcd.print(TPS2,2);
lcd.setCursor(9,0);
lcd.print ("V ");
lcd.setCursor(9,1);
lcd.print ("V");
lcd.setCursor(0,2);
lcd.print ("CRANK ");
lcd.setCursor(0,3);
lcd.print ("CAM");
unsigned long currentMicros = micros(); // Makes currentMicros = to microseconds since CPU start
if(currentMicros - previousMicros > interval)
{
previousMicros = currentMicros;
i = i++;
if (i < 72 )
{
ledState = !ledState;
digitalWrite(crankPin, ledState);
}
if (i == 72 )
{
ledState2 = !ledState2;
digitalWrite(camPin, ledState2);
}
if (i == 73 )
ledState2 = !ledState2;
digitalWrite(camPin, ledState2);
if (i == 73)
{
i = 1;
}
}
}