Basic Question...

It sounds like a basic question...

  • 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);
  
}

why is there a 1 second delay in loop?

do you intend to hold the button down the entire cycle?

The code you posted is incomplete and won't work. You will not be able to use delay(1000) and have an LED fade.

Please edit your post to include complete code, that compiles. Always use code tags.

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

what keeps it cycling after you release the button?

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);

}

The final analogWrite is executed every time through the loop, was it your intent for that to be part of the 'else' ?

caineroad:
Hello, I used delay 1 second so that the sensor and Arduino would not be stressed out.

I guarantee you that's not a thing.

There is no button used here...

For analogWrite, yes it was my intent as I needed to make the LED go off if sensor detects 1

How can I keep the fade cycle going without keep restarting due to sensor hitting 0 every second?

gfvalvo:
I guarantee you that's not a thing.

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

caineroad:
How much data or buffer can ardiuno UNO handles?

Many hundreds of gigabytes, given long enough.

Sp. "Arduino"

caineroad:
For analogWrite, yes it was my intent as I needed to make the LED go off if sensor detects 1.

You left out a set of brackets:

  else
  {
    Serial.println(Value);
    analogWrite(LED, 0);
  }

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.

  else
    Serial.println(Value);
  analogWrite(LED, 0);

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.
  }
}

Good luck!

-jim lee

I used delay 1 second so that the sensor and Arduino would not be stressed out.

Not something you need to worry about, or that delay() could fix.

Be sure to use a current limiting resistor with every 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.

david_2018:
You left out a set of brackets:

  else

{
   Serial.println(Value);
   analogWrite(LED, 0);
 }




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)

it has ignored my delay(30) for the fading transition time

Please post the code as it is now

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.