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