Older Stroboscope Code

This is the original code that I had used to overcome the frequency limitations. It ended up flashing brightly at the transition between sections on some transitions.


/*
Stroboscope
good for about 9.62 Hz to 500 Hz (577rpm to 30000rpm)
the range can be expanded in either direction, but resolution will be sacrificed
the microsecond based portion of the code will not work for frequencies below 31 Hz

Reads an analog input pin, uses this value as a time delay for a stroboscope.
Also sends frequency info back via serial.

The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LEDs connected from digital pins 3,5,6,9,10,and 11 to ground with proper resistor
to act as strobe

created 2/20/2011
by Steve Krave

*/

//set constants
// Analog input pin that the potentiometer is attached to
const int analogInPin = A0;

// Variables etc...
float sensorValue = 0; // value read from the pot
float hold = 1; // hold time between iterations (sort of)
float holdMicros = 1; // hold time between iterations (sort of)
float holdData = 0;
float frequency = 1; // stores frequency
float rpm = 1; // rpm
boolean fast = 1; //sets up intial micro or milli operator

//microsecond version
long microsPrev = 0; // set up value for timing
long microsCurrent = 0; // used for timing purposes
long microsSerial =0; //used for serial output timing

//millisecond version
long millisPrev = 0; // set up value for timing
long millisCurrent = 0; // used for timing purposes
long millisSerial =1; //used for serial output timing



void setup() {
// initialize serial communications at 115200 bps
// lower speeds can delay execution
Serial.begin(115200);

//initialize pins as outputs
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}

void loop() {
sensorValue = analogRead(analogInPin);

//basic crossover type function uses 256 as default
if(sensorValue <= 255) fast = strobeMicros(fast); if(sensorValue >= 256) fast = strobeMillis(fast);
}

boolean strobeMicros(boolean fast) {
Serial.println("Testmicro");
//add 2ms to hold time for code execution and then some to avoid overflow
holdMicros = sensorValue*100 + 2000;
microsCurrent = micros(); // collect current time
if (microsCurrent > microsPrev + holdMicros || fast == 0){ // set up timing loop
microsPrev = microsPrev + holdMicros; // set up millis for delay stuff
frequency = 1000000/holdMicros; // calculate frequency (used in delay timing)

//set all 6 LEDs high (they were already plugged in)
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);

//leave LEDs on long enough to see, adjusts "intensity"
delayMicroseconds(.01*holdMicros);

//set all LEDs low
digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);

if(millis() > millisSerial + 200){ //set up output
millisSerial=millis(); //reset timer
//do some math to find values
rpm = frequency * 60;

// print the results to the serial monitor:
Serial.print("frequency = " );
Serial.print(frequency);
Serial.print(" rpm = ");
Serial.println(rpm);
}
}
fast = 1; //sets to default into slow loop on switch
return fast;
}
boolean strobeMillis(boolean fast) {
Serial.println("Testmilli");
//add 2ms to hold time for code execution and then some to avoid overflow
hold = sensorValue/10 + 2;
millisCurrent = millis(); // collect current time
if (millisCurrent > millisPrev + hold || fast == 1){ //set up timing loop
millisPrev = millisPrev + hold; //set up millis for delay stuff
frequency = 1000/hold; //calculate frequency (used in delay timing)

//set all 6 LEDs high (they were already plugged in)
digitalWrite(3,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);

//leave LEDs on long enough to see, adjusts "intensity"
delayMicroseconds(10*hold);

//set all LEDs low
digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);

if(millis() > millisSerial + 200){ //set up output
millisSerial=millis(); //reset timer
//do some math to find values
rpm = frequency * 60;
// print the results to the serial monitor:
Serial.print("frequency = " );
Serial.print(frequency);
Serial.print(" rpm = ");
Serial.println(rpm);
}
}
fast = 0; //sets default default into fast loop on switch
return fast;
}