0

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!

ratchet freak
3,2671 gold badge13 silver badges12 bronze badges
asked Mar 19, 2018 at 16:07
1
  • 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! Commented Mar 19, 2018 at 16:13

2 Answers 2

1

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;
 }
}
answered Mar 19, 2018 at 16:17
-1

*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);
}
}
answered Mar 19, 2018 at 18:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.