I was wondering if anyone had any pointers or advice to make this project work. Essentially I would like to have the LED fade (meaning like fade on and off at a calm rate) at one rate when there is nothing close to the proximity sensor, and then to speed up the fade amount when there is something close to the sensor.
#include <NewPing.h> //include the library
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount; // how many points to fade the LED by
int triggerPin = 12; //pin conneccted to the Trig pin on the sensor
int echoPin = 11; //pin connected to the Echo pin on the sensor
int maxDistance = 200; //set the max distance for the sensor to read (helps with errors)
int distanceVal; //variable to hold the distance val
int sampleRate = 200; //how fast to sample the value
long lastReading; //used for the timer
NewPing proximity1(triggerPin, echoPin, maxDistance); //sets up the sensor object
void setup()
{
Serial.begin(9600); //start the serial port
pinMode(led, OUTPUT);
}
void loop() {
if(millis()-lastReading>=sampleRate) //this very simple statement is the timer,
{
distanceVal = proximity1.ping_cm(); //get the distance value in centimeters
lastReading = millis();
Serial.print("Distance Reading CM : "); //print the value to the Serial monitor
Serial.println(distanceVal);
if(distanceVal<20){
fadeAmount = 5;
}
else{
fadeAmount = 2;
}
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
https://github.com/sanaalla/Fade-LED-with-Proximity-sensor
Above is my code, which is not working. I am fairly new to Arduino and so would greatly appreciate some more seasoned wisdom!