M
M
macolub2016-07-08 23:17:45
Arduino
macolub, 2016-07-08 23:17:45

How to make that would smoothly turn on the diode through the photoresistor?

#define fadePin 3 //MOSFET control pin
int pirPin = 2; //pin for connecting the PIR control signal of the sensor
int light; //variable for storing the state of the light (on/off)
void setup(){
pinMode(fadePin, OUTPUT);// 3 pins per output, to control the transistor
light = 0; //set the variable for the first light on
}
void loop(){
if(( (analogRead (1))) >= (610));
{
if(light == 0) //and if the light was not on
{
for(int i=0; i<=150; i++) //then smoothly turn on the light
{
analogWrite(fadePin, i);
delay(10); // every 10ms increase by 1
}
light = 1; //and pass the value of the variable that the light is on
}
}
{
if(( (analogRead (1))) <= (610))
{
for(int i=150; i>=0; i--)//smooth extinguish its
{
analogWrite(fadePin, i);
delay(10);
}
light = 0; //and pass the value of the variable that the light is off
}
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-07-08
@alsopub

If you really have a photoresistor, and not PIR as in the variable names, then try something like this (offhand):

void loop() {

  if((analogRead(1) >= 610) && (light == 0)) {
    for(int i=0; i<=150; i++) {
      analogWrite(fadePin, i);
      delay(10);
    }
    light = 1; //и передаем значение переменной, что свет включен
  }

  if((analogRead(1) < 610) && (light == 1)) {
    for(int i=150; i>=0; i--) {
      analogWrite(fadePin, i);
      delay(10);
    }
    light = 0;
  }

  delay(10);

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question