Go Down

Topic: Ultrasonic EZ-1 and LED control based on distance (Read 501 times) previous topic - next topic

anderscrib

I am so close on this projects thank to forum input-especially CrossRoads! I am trying to have the LED blink faster when an object gets closer, then if no object is in a certain range, the LED should "throb" like and Apple computer using PWM.
The first part works ok then I tried to add the "throb" code using an "if"statement. Everything compiled fine but the throb part doesn't work..
As a relative newbie, I am looking for some insight on this code
Code: [Select]
/*
* EZ rangefinder Distance Sensor
* prints distance and changes LED flash rate
* depending on distance from sensor
* if sensor range is greater than 50000 on serial port then LED "throbs" like apple computer
*/

const int sensorPin = 5;
const int ledPin = 6; //pin connected to LED

long value = 0;
int cm = 0;
int inches = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  value = pulseIn(sensorPin, HIGH);
  cm = value / 58;   // pulse width is 58 microseconds per cm
  inches  = value / 147;  // pulse is 147 microseconds per inch
  Serial.print(cm);
  Serial.print(',');
  Serial.print(inches);
 
  digitalWrite(ledPin,HIGH);
  delay(cm * 5);  //each centimeter adds 10 milliseconds delay
  digitalWrite(ledPin, LOW);
  delay( cm * 5);
  delay(20);
 
  if (sensorPin > 50000)
     for(int i = 0; i<360; i++){
     //convert 0-360 angle to radian (needed for sin function)
     float rad = DEG_TO_RAD * i;

    //calculate sin of angle as number between 0 and 255
     int sinOut = constrain((sin(rad) * 128) + 128, 0, 255);

    analogWrite(ledPin, sinOut);

    delay(15);
   }

}


AWOL

Code: [Select]
const int sensorPin = 5;
Code: [Select]
if (sensorPin > 50000)
Seems unlikely, doesn't it?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

anderscrib

Ah yes.
I believe I need to serial.read that number..Does that make sense?

PaulS

Quote
Does that make sense?

No. What does sensorPin represent?

Code: [Select]
  value = pulseIn(sensorPin, HIGH);
Looks a lot like a pin that a sensor is attached to.

Code: [Select]
  cm = value / 58;   // pulse width is 58 microseconds per cm
  inches  = value / 147;  // pulse is 147 microseconds per inch
  Serial.print(cm);
  Serial.print(',');
  Serial.print(inches);

How come you don't compute feet, yards, furlongs, and angstroms, too? Only one of these values makes sense in your unit of measure system.


Go Up