range sensor to detect object passing by -> trigger 11 ms pulse

Hi there.

Struggeling through this new world of code, forgive me, for maybe asking questions with seemingly obvious answers :slight_smile:

So, a brief explanation of what I want to acheive:

I have a wheel, about 80 cm in diameter with 12 spokes. Each spoke is about 1 cm thick and they are round. It's not running very fast. Approx 8 second pr cycle. A quick calculation gave me roughly 667 ms between each spoke. I've attached a drawing off the contraption.

So I have ordered some Sharp IR sensors (GP2Y0A21YK0F) but until they arrive, I'm using my HC-SR04 ultrasonic range detector.
The led will be turned into a sync clock for synthesizers. They need about 11 ms of pulse duration, I'm told.

What I need help with, is to stabalize this. Is the Sharp IR sensor more stable? Thinking it doesn't need any ping'ing. Or is there maybe something in the code I can add/simplify?

I've added some averaging, to smooth the data coming from the sensor, but I can't average to much, because the spokes don't linger in front of the sensor.

// —————————————————————————
// Example NewPing library sketch that does a ping about 20 times per second.
// —————————————————————————

#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;
int detection = 0;
int detectionStatus = 0;
int lastDetection = 0;
const int ledPin = 13;

const int numReadings = 2;

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 distAverage = 0;            // the average



void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.

  for (int thisReading = 0; thisReading < numReadings; thisReading++)  {
    readings[thisReading] = 0;
  }
}

void loop() {
  
  delay(20); // Wait 20ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings, but there is another delay in the code so this adds up to 30
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  distance = uS / US_ROUNDTRIP_CM;

  total = total - readings[readIndex];
  readings[readIndex] = distance;
  total = total + readings[readIndex];
  readIndex = readIndex + 1;

  if (readIndex >= numReadings)  {
    readIndex = 0;
  }

  distAverage = total / numReadings;
  Serial.print(distance);
  Serial.print(" avg ");
  Serial.println(distAverage);
  
  if (distAverage < 4) {
  detection = 1;
  
  } else {
  detection = 0;
}




  detectionStatus = detection;

  if (detectionStatus != lastDetection) {
    if (detectionStatus == 1) {
      digitalWrite(ledPin, HIGH);
      delay (11); //pulse duration
      digitalWrite(ledPin, LOW);
      
    } 

  }
 lastDetection = detectionStatus;
}

Any help would be great :slight_smile:
thx