HC-SR04 Ultrasonic Sensor to control Solenoid push rate

had this posted in the wrong topic, hopefully someone can shed some light on this project?

I am currently requiring some help with a project involving a HC-SR04 Ultrasonic Sensor to control a Solenoid so that the rate at which the solenoid is pushing against an object can be controlled by a hand movement in front of the ultrasonic sensor, ranging from about 0 (being the fastest rate) - 100cm (being the slowest rate)

So far I have gotten my code to turn an LED on when the reading in the serial monitor is between 2 and 50
hopefully you can make some sense of it:

int ledPin = 4;
int dist; //how far the object is away from the module(cm)
int val = 0;
#define trig 11
#define echo 12

void setup() {
  Serial.begin (9600);
  pinMode(echo, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(ledPin, OUTPUT);


void loop() {
  int duration, distance;
  digitalWrite(trig, HIGH);
  digitalWrite(trig, LOW);
  //receive the trigger signal echo, and calculate cm to the object
  val= pulseIn(echo, HIGH);
  dist= val/58; //converts raw to cm, valueSensor/74/2 = inches
  if (dist >= 50 || dist <= 2)
    digitalWrite(ledPin, LOW);
  else {
 digitalWrite(ledPin, HIGH);

all I am wondering now is how the heck I can create a slow - fast blink rate of the LED determined by the 0-200 serial reading I am getting (if i get the LED to blink fast to slow, I can then use my ULN2003AN chip to control a solenoid to turn on and off in the same motion the LED does)

Thanks, would love some help! I hope that is enough information! :smiley:

Have a look at the blink-without-delay example that is included with the IDE.

working with this code now, I can read out the values of the ultrasonic sensor as cm

/* Ping))) Sensor
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe

   This example code is in the public domain.


// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;
int ledPin = 13;

void setup() {
  // initialize serial communication:

void loop()
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);

long microsecondsToCentimeters(long microseconds)
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;

now I am wondering how i can get it to read only between 0 - 100cm , and then convert that to the blink of an LED... its strange I can only get it to work with a potentiometer, but when I try to use the Ultrasonic sensor it will not read it the same as an analog read...... I dont really understand what is going on with the blink-delay tutorial.... and how I can relate it to this as it is not an analog read..