I am making an IR sensor to read either 1 or 0, every 1 second.
When it is 0, I would like to run a fading LED light that runs indefinitely until the sensor goes to 1 again
Fading LED is done by PWM (0+255-255-0), 5 seconds for each 'cycle'
Else turn the LED off.
Whole process go on loop()
My code worked but it is always resetting itself before fading cycle is completed. How can I make sure the fading cycle goes on indefinitely when the sensor reads 0, but continue reading the sensor every second?
On simpler terms, do not start over the fading LED cycle every time sensor reads 0.
const int LED = 9;
int Brightness = 0;
int increase = 5;
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
pinMode(8,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int Value = digitalRead(8);
delay(1000);
if (Value == 0)
{
analogWrite(LED,Brightness);
Brightness = Brightness + increase;
Serial.println(Brightness);
delay(30);
if (Brightness <=0 || Brightness >= 30)
{
increase = -increase;
}
}
else
Serial.println(Value);
analogWrite(LED,0);
}
Hello, I used delay 1 second so that the sensor and Arduino would not be stressed out. I didn't use any button. Once sensor detects something, LED fade should go on indefinitely until it detects nothing. Shall I use 'for' statement instead?
The full code was there and is working, I modified it including the code tags
How much data or buffer can ardiuno UNO handles? I have total of 6 IR sensor that soon needs to be installed on one UNO, and each one is without any delay program
In the IDE, click on Tools > Auto Format, or type T, to have the IDE auto align the code and you should see the problem. Your original code only has the Serial.print() as part of the 'else', and unconditionally executes the analogWrite.
If you would like to try this using libraries.. Here's one for you. I set it up using a button as the IR thing because I didn't have an IR thing handy. I tried to make it easy to switch over. You will need LC_baseTools (From the IDE library manager) to compile it. Not positive about the fade cycle times but they are very easy to tweak in this version.
#include <mechButton.h> // You can delete this line for the IR version.
#include <multiMap.h>
#define LEDPin 9
#define IRPin 8
mechButton aButton(IRPin); // This is just here for debuggin with a button. Delete for IR version.
timeObj fadeTimer(5000); // The time in MS it will take to go through the fade cycle from 0->on->0
multiMap fadeMapper; // A multimapper to mape the brightness values over time.
void setup(void) {
fadeMapper.addPoint(1,0); // Time wll for from 1 to 0 (fraction of full, like gas gauge)
fadeMapper.addPoint(.5,255); // 1/2 tank..
fadeMapper.addPoint(0,0); // Empty, time has expired.
pinMode(IRPin,INPUT); // For when you hook up the IR stuff.
}
bool checkIR(void) {
bool value;
value = !aButton.trueFalse(); // For debuggin we pretend the button is the IR sensor. Delete for IR verions.
//value = digitalRead(8); // This is the IR version from your code.
return value; // Send back the tru/false. (Did we see the IR thing?)
}
void loop(void) {
float fadeVal;
if (checkIR()) { // If we saw an IR reflection..
fadeTimer.start(); // Restart our fade timer.
analogWrite(LEDPin,0); // Turn off the LED.
} else { // Else, we see nothing, we hear nothing!
if (fadeTimer.ding()) { // If we have completed a fade cycle..
fadeTimer.start(); // We'll restart the fade timer.
}
fadeVal = fadeMapper.map(fadeTimer.getFraction()); // Map the time position in the face cycle to a fade value.
analogWrite(LEDPin,round(fadeVal)); // Write the fade value to the LED.
}
}
caineroad:
How much data or buffer can ardiuno UNO handles? I have total of 6 IR sensor that soon needs to be installed on one UNO, and each one is without any delay program
The FadeLed offering in the IDE library manager should interest you.
In the IDE, click on Tools > Auto Format, or type <Ctrl>T, to have the IDE auto align the code and you should see the problem. Your original code only has the Serial.print() as part of the 'else', and unconditionally executes the analogWrite.
else
Serial.println(Value);
analogWrite(LED, 0);
Thanks for sharing the autoformat, it is very handy. Silly me I now put the brackets in, and it is working finally. But it has ignored my delay(30) for the fading transition time, and uses the delay (1000) it's making the LED change brightness every second instead of (30)