Pages: [1]   Go Down
Author Topic: Ultrasonic EZ-1 and LED control based on distance  (Read 488 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
* 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);
   }
 
}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26630
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int sensorPin = 5;
Code:
if (sensorPin > 50000)
Seems unlikely, doesn't it?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50883
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does that make sense?
No. What does sensorPin represent?

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

Code:
  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.

Logged

Pages: [1]   Go Up
Jump to: