-1

I'm trying to make a lamp that fades when it reacts with surrounding light. I've installed a library from github that doesn't require the delay (). The serial monitor doesn't refresh. The lamp is supposed to fade from 0-255 when in the dark and fade 255-0 when in light. It's supposed to happen only once so I can't put it in loop().

The LED is connected to PWM pin 10. The LDR sensor is connected to A0.

#include <LEDFader.h>
int sensorvalue = analogRead(A0);
LEDFader led = LEDFader(10);

void setup(){

  if(sensorvalue<=380){    
    led.set_value(0);
    led.fade(255, 3000);
  }
  if(sensorvalue>=555){
    led.set_value(255);
    led.fade(0,3000);
  }
}

void loop() {
  Serial.begin(9600);
  Serial.println(sensorvalue);
  led.update();

}

7
  • 2
    Please post the code as something other than a screen shot. You have some fairly big mistakes in that code but I'm not going to sit here re-typing it from a picture when you can't be bothered to copy and paste it.
    – Andrew
    May 3, 2017 at 14:56
  • 1
    @Andrew sorry was typing this question on a mobile phone. Code will be up in few minutes
    – user33369
    May 3, 2017 at 14:58
  • 1
    @Visual Micro i can't put the fade code at while loop as it will repeat it's process of turning on and off. I only want it to do it once
    – user33369
    May 3, 2017 at 15:06
  • I have edited the example to show how to only change fade when the analog value changes. If you only want it once then do it in the setup() but if it needs an analog reading that does not make sense. My example shows how the fade can respond to a valid analog reading at any single moment. May 3, 2017 at 15:10
  • Maybe you were still struggling so the answer has been updated to a complete working example. May 4, 2017 at 12:21

1 Answer 1

2

Arduino does not work that way. You only try to read a sensor value outside of a function, so will happen once only, if it works at all. You need to read the sensor more often and always read from within a function.

Serial.begin() is used once when the code starts, so that will be in the setup()

Your LED fade values are only set one time, when the cpu starts because you have the fade in the setup().

In the following example we do all fade code in the loop() but change the fade values only when the sensor reading changes.

#include <LEDFader.h>
LEDFader led = LEDFader(10);

int sensorValue = 0;
int sensorValueLast = 0;

void setup()
{
   Serial.begin(9600);
}

void loop()
{
  sensorValue = analogRead(A0);
  Serial.println(sensorValue);

        if (sensorValueLast!=sensorValue)
        {
          sensorValueLast = sensorValue;        
          if(sensorvalue<=380)
          {    
             led.set_value(0);
             led.fade(255, 3000);
          }
          if(sensorvalue>=555)
          {
            led.set_value(255);
            led.fade(0,3000);
          }
        }

  led.update();     
}

This example only calls led.fade() when the sensor value changes but uses the same sensor reading that was found during startup. The sensor is read just once time and the fade values are set just one time.

#include <LEDFader.h>
LEDFader led = LEDFader(10);

int sensorValue = 0;
int sensorValueLast = 0;

void setup()
{
   Serial.begin(9600);
}

void loop()
{
  sensorValue = analogRead(A0);
  Serial.println(sensorValue);

        if (sensorValueLast!=sensorValue)
        {
          sensorValueLast = sensorValue;        
          if(sensorvalue<=380)
          {    
             led.set_value(0);
             led.fade(255, 3000);
          }
          if(sensorvalue>=555)
          {
            led.set_value(255);
            led.fade(0,3000);
          }

          led.update();     
        }
}

This example does everything just one time, when the cpu starts

#include <LEDFader.h>
LEDFader led = LEDFader(10);

int sensorValue = 0;

void setup()
{
   Serial.begin(9600);

   sensorValue = analogRead(A0);
   Serial.println(sensorValue);

          if(sensorvalue<=380)
          {    
             led.set_value(0);
             led.fade(255, 3000);
          }
          if(sensorvalue>=555)
          {
            led.set_value(255);
            led.fade(0,3000);
          }

          led.update();     

}

void loop()
{
   //we will always see the same value
   Serial.println(sensorValue);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.