Hi all,
I'm really new to Arduino code and am having trouble with my current project.
I want to use a Parallax Ping sensor to control how bright or dim an LED becomes. The closer that a person approaches, the brighter the light. The farther away, the dimmer the light.
I found a simplified Ping code on the Arduino site, but don't know how to modify it for my project. Any suggestions?
Here is the code I have so far:
//working Ping project
unsigned long echo = 0;
int ultraSoundSignal = 7; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(ultraSoundSignal,OUTPUT);
pinMode(11, OUTPUT); //sets up LED as output
}
unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
return ultrasoundValue;
}
void loop()
{
int x = 0;
x = ping();
Serial.println(x);
delay(250); //delay 1/4 seconds.
if (ping() > 84)
{
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
{
//sets the value (range from 0 to 255);
analogWrite(11, fadeValue);
//wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
else
{
for(int fadeValue = 255 ; fadeValue>= 0; fadeValue -=1)
{
//sets the value (range from 0 to 255):
analogWrite(11, fadeValue);
//wait for 30 milliseconds to see dimming effect
delay(30);
}
}
}