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!
-
I'm on my mobile, so I can't test anything. For me the link is not working in the app (you can copy the code in the question, formatting it as code, this is recommended as in the future the link may be broken). And "it's not working" is not the most accurate way of describing your problem. Does the code not compile? Does your actual physical setup not work? Post a schematic!idkfa– idkfa2018年03月19日 16:13:53 +00:00Commented Mar 19, 2018 at 16:13
2 Answers 2
You want 2 independant loops, one for the measurement and one for the dimming effect. The latter one must also be able to run when not measuring.
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(millis() - lastFade >= fadeRate) //other timer
{
lastFade = millis();
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;
}
}
*update! I fixed the code and got it to do what I want.
#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 fadeAmount1 = 10; // how many points to fade the LED by
int fadeAmount2 = 2; // 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 lastFade;
int fadeRate = 200;
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){
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount1;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount1 = -fadeAmount1;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
else{
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount2;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount2 = -fadeAmount2;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}