Can someone please help me with code? I have a 24 tooth trigger wheel. Every tooth is registered by hall sensor and I need that Arduino simulate 36 pulse output of that corresponding 24 pulse input.
Here is my code with delayMicroseconds, but I can`t use delayMicroseconds, because Arduino doesn't understand bigger than 16k micros delay.
const int hall = 2; // hall sensor
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int teethCounter = 0;
int hallState = 0;
int lasthallState = 0;
long cycles=0;
boolean cycle = false;
unsigned long microsStart = 0;
unsigned long microsStop = 0;
unsigned long usElapsed = 0;
unsigned long usElapsedUp = 0;
unsigned long usInterval;
void setup() {
// initialize the button pin as a input:
pinMode(hall, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
hallState = digitalRead(hall);
if(cycle==true){
microsStart=micros();
}
if(cycle==true){
usInterval = usElapsedUp/72;
for (int i=0; i <= 36; i++){
digitalWrite(13,HIGH);
delayMicroseconds(usInterval);
digitalWrite(13,LOW);
delayMicroseconds(usInterval);
cycle = false;
}
}
// compare the hallState to its previous state
if (hallState != lasthallState) {
// if the state has changed, increment the counter
if (hallState == HIGH) {
teethCounter++;
if(teethCounter==24){
cycle = true;
cycles++;
teethCounter=0;
usElapsedUp = usElapsed;
}
Serial.print("Tooth count: ");
Serial.print(teethCounter);
Serial.print(" Cycles: ");
Serial.print(cycles);
Serial.print(" Time: ");
Serial.print(usElapsedUp);
Serial.print(" Interval: ");
Serial.println(usInterval);
}
microsStop=micros();
usElapsed=microsStop-microsStart;
}
// save the current state as the last state,
//for next time through the loop
lasthallState = hallState;
}
How can I calculate and from where can I take trigger points?
but I can`t use delayMicroseconds, because Arduino doesn't understand bigger than 16k micros delay.
If you need to delay longer than a few microseconds, delayMicroseconds() is not the right function. delay() deals in milliseconds (which is what thousands of microseconds are).
PaulS:
If you need to delay longer than a few microseconds, delayMicroseconds() is not the right function. delay() deals in milliseconds (which is what thousands of microseconds are).
Because trigger wheel spins anywhere from 0 RPM up to 6000 RPM. And with my code where time is used it will not count realtime, there will be always 1 rpm error.
So, you need 3 pulses out for every 2 pulses from the Hall sensor. Similar to frequency conversion with a fractional PLL. And like a PLL, a lot depends on how much error and jitter (phase noise) you're willing to put up with.
The code below is untested, but it does compile. There is no jitter filtering and it doesn't check for 0 RPM. That's an exercise for the reader. But, give it a try. If you need more accuracy or less jitter, you'll have to add filtering and go to more sophisticated techniques such as timer interrupts and direct port access.