Hi everyone!!,
I am using the function millis() to lit an led strips....each interval( a time that I set) It get into the an if and check a variable....and what I want to do and It doesnt work is turn off the led strips each one second...using millis() function too...but It doesnt work, It seems that It doenst get into the sencond if....any idea why It's not working as I was expecting???, and I'm using as well function delay() I dont know if delay() is affected by using millis()....I guess it should work properly but I'm not sure...here is my code....and I'm using differents variable to using function millis() in differents if...here is my code:
#include <TimerOne.h>
#include "Wire.h"
int BH1750_Device = 0x23;
int LedPin1 = 10;
int LedPin2 = 9;
long previousMillis = 0;
long previousMillis2 = 0;
long interval = 500;
long interval2 = 1000;
int T1 = 0;
int intervalLux = 10;
int minusintervalLux = -10;
int previousLux = 0;
int Timer1count = 0;
int previousT1 = 0;
int change = 0;
int save = 0;
void setup(){
Timer1.initialize(100);
Wire.begin();
Serial.begin(9600);
pinMode(LedPin1,OUTPUT);
pinMode(LedPin2,OUTPUT);
configure_BH1750();
//set timer1 interrupt at 1Hz
}
void loop(){
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();
int currentLux = BH1750_Read();
Serial.print("measurement in lux:");
Serial.print(currentLux);
Timer1.pwm(LedPin1,T1);
Timer1.pwm(LedPin2,T1);
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if( T1 > 200){
T1 = 0;
previousT1 = 0;
}
if( currentLux - previousLux >= intervalLux){
previousLux = currentLux;
if(0 <= T1 < 20){
change = 2;
}
else if(20 <= T1 < 50){
change = 5;
}
else{
change = 10;
}
T1 = previousT1 + change;
previousT1 = T1;
}
if( currentLux - previousLux <= minusintervalLux){
previousLux = currentLux;
if(0 <= T1 < 20){
change = 2;
}
else if(20 <= T1 < 50){
change = 5;
}
else{
change = 10;
}
T1 = previousT1 + change;
previousT1 = T1;
}
}
if(currentMillis2 - previousMillis2 > interval2){
previousMillis = currentMillis;
save = T1;
T1 = 0;
delay(200);
T1 = save;
}
}
int BH1750_Read()
{
unsigned int i=0;
Wire.beginTransmission(BH1750_Device);
Wire.requestFrom(BH1750_Device, 2);
while(Wire.available())
{
i <<=8;
i|= Wire.read();
}
Wire.endTransmission();
return i/1.2; // Convert to Lux
}
void configure_BH1750()
{
Wire.beginTransmission(BH1750_Device);
Wire.write(0x13); //set resolution to 4 lux countinouosly L-resolution mode,each 16ms make a measure
Wire.endTransmission();
}