I have this code that works sortof fine. But when I add it to a scheduler it doesn't work similarily.
I found a scheduler on github:
I need a scheduler, becuse I need to send a sync clock to a modular synth. The sync clock is a +5 voltage pulse of a duration of 11-25 ms. And I need the interval between these pulses to be an average of intervals between detections from a sensor.
I have a HC-SR04 that I have set to detect when ever a stick passes in front of it. I then make this into timer, so it measures the time between stick passings. I then get the average of the last ten passings.
This average is to be the interval between pulses sent to the synth.
To avoid the pulse interval timing not be affected by the pulse length timing and the timing needed by the sensor to stabalize I chose to venture into this scheduler business.
When the timer code runs by it self, it works fine:
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int distance = 0;
const int threshDist = 4;
int detection = 0;
unsigned long interval = 0;
const int ledPin = 13;
int pulselength = 25;
unsigned long previousMillis = 0; //last detection
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int averageInterval = 0; // the average
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
distance = uS / US_ROUNDTRIP_CM;
if (distance < threshDist) {
interval = millis() - previousMillis;
Serial.print("Interval = ");
Serial.print(interval);
previousMillis = millis();
total = total - readings[readIndex];
readings[readIndex] = interval;
total = total + readings[readIndex];
readIndex = readIndex +1;
if (readIndex >= numReadings) {
readIndex = 0;
}
averageInterval = total / numReadings;
Serial.print(" avg = ");
Serial.println(averageInterval);
delay(50);
}
}
But when I add the scheduler and the pulse sending, it seems like the sensor doesn't behave right. It sometimes works ok, but not always.
#include "TaskScheduler.h"
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int distance = 0;
const int threshDist = 4;
int detection = 0;
unsigned long interval = 0;
const int ledPin = 13;
long pulseLength = 25;
unsigned long previousMillis = 0; //last detection
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int averageInterval = 100; // the average
boolean g_led1State=1;
void setup() {
pinMode(ledPin, OUTPUT);
Sch.init(); // Initialize task scheduler
/*
* use Sch.addTask(task, start_time, period, priority) to add tasks
* task - tasks to be scheduled
* start_time - when the task starts (ms)
* period - repeat period of the task (ms)
* priority - 1: mormal priority, 0: high priority
*/
Sch.addTask(timer,0,50,1); // add a task
Sch.addTask(pulse,0,50,1);
Sch.start(); // Start the task scheduler
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
Sch.dispatchTasks();
}
void timer() {
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
distance = uS / US_ROUNDTRIP_CM;
if (distance < threshDist) {
interval = millis() - previousMillis;
Serial.print("Interval = ");
Serial.print(interval);
previousMillis = millis();
total = total - readings[readIndex];
readings[readIndex] = interval;
total = total + readings[readIndex];
readIndex = readIndex +1;
if (readIndex >= numReadings) {
readIndex = 0;
}
averageInterval = total / numReadings;
Serial.print(" avg = ");
Serial.println(averageInterval);
}
}
void pulse()
{
digitalWrite(ledPin, HIGH);
delay(pulseLength);
digitalWrite(ledPin, LOW);
delay(averageInterval - pulseLength);
}
I'm not very experienced, so I might supply you with not the right or useful information. But hopefully, you get an idea. If not, please let me know how to elaborate, and I'll do my very best.
thx