Crank Signal Generator, Cant get below 10000uS in delay

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;

}
}
}

Am i runnign out of Processor capabilty to do this?

No. There is a way to split a code in 2 category - time sensitive and everything else.
To stream 36 pulses, put a code inside ISR, TimerOne library could help on this. Everything else, could stay in main loop (slow HMI: reading buttons, lsd). What is receptor, can you send data with same speed 8000 ksps doesn't matter if there was no changes?

A few things will speed you up.

Change your baud rate from 9600 to 115200 which is the maximum rate the serial monitor will work at. Better yet if you can remove serial prints you will save the most time.

You are printing to the LCD on every pass through the loop, why not print only when the data changes and also only change only half the display at a time. You will save time by not printing to the LCD so often.

Here is a topic to get faster analogReads: http://arduino.cc/forum/index.php/topic,74895.0.html.

Somewhere among these downloads is a library that makes your digitalWrite faster. I think he called it fastWrite? Google Code Archive - Long-term storage for Google Code Project Hosting.

Also there is a pin toggle method that is one of his newest files on the same link as above. It is probably even better. Google Code Archive - Long-term storage for Google Code Project Hosting.

Hi Guys,
Sorry, maybe i have mislead you,

The issue i am having is not with the dsplay,
Only the last part of the code is doing the square wave output

I am using a scope to read the signal i am generating, i am happy with the display as it is.

This part of the code is what i am having issues with, the Constant long integer called "variable" is giving the gap between pulses in microseconds, changing the value to less that 10000 doesnt seem to speed up the frequency of the pulses.

thanks in advance,

i am new to the forum so apologies if my formatting isnt quite right yet

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;

}
}
}

You don't understand. Everything included in loop() executed in same order as it written. It means, you pulse generator have to Wait till processor finished everything else. Lcd is slow, serial is slow, and this is why pulse is also slow, because overall speed depends on all participants .

Sorry, completely understand now, i must have muisread your post,

thanks :slight_smile:

        i = i++;

i++ is ewuivalent to i i + 1. So, your code is equivalent to

i = i = i + 1;

Look silly written that way, doesn't it? Like yours does to those that comprehend the ++ operator.

        if (i == 73 )
        ledState2 = !ledState2;
         digitalWrite(camPin, ledState2);

If i is 73, toggle the pin. Then, regardless of the value of i, set the pin state. Is that what you want? Or, are you missing some braces?

      if (i < 72 )
      {
      }         
          if (i == 72 )
         
      {
      }
        if (i == 73 )
      if (i == 73)

For which values of i will multiple blocks be executed? Using if/else if/ else makes for faster code, since some comparisons can be skipped.