#include <Wire.h>
#include <MPU6050.h>
extern volatile unsigned long timer0_millis;
unsigned long NuovoValoreMillis = 0;
unsigned long TempoInizioLoop = 0;
const int Luce1 = 11;
const int Luce2 = 12;
int UltimoValorePitch;
int Differenza;
int periodoFadeLed = 900;
int displaceFadeLed = 500;
int valoreLed1, valoreLed2 ;
boolean RilevazioneMovimento = false;
int TempoSpegnimentoLed = 5000;
MPU6050 mpu;
void setup()
{
Serial.begin(9600);
pinMode(Luce1, OUTPUT);
Serial.println("Initialize MPU6050");
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
}
void loop()
{
// Read normalized values
Vector normAccel = mpu.readNormalizeAccel();
// Calculate Pitch & Roll
int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis)) * 180.0) / M_PI;
//int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI;
// Output
//Serial.print(" Pitch = ");
//Serial.print(pitch);
//Serial.print(" differenza = ");
//Serial.println(Differenza);
//Serial.print(" Roll = ");
//Serial.print(roll);
//Serial.println();
//delay(200);
Differenza = (UltimoValorePitch - pitch);
//if (Differenza != 0) // SE E' DIVERSO DA ZERO
if ((Differenza != 0) && (Differenza != 1 && (Differenza != -1))) // SE DIVERSO DA -1, 0, 1 OK
// if ((Differenza >= -1) && (Differenza >= 1)) SE E' COMPRESO FRA -1 E 1 KO
{
//analogWrite(Luce1, 50);
TempoInizioLoop = millis();
valoreLed1= 128+127*cos(2*PI/ periodoFadeLed * TempoInizioLoop);
analogWrite(Luce1, valoreLed1);
//SetUltimoValorePitch(pitch());
UltimoValorePitch = (pitch);
//Serial.print(" differenza = ");
//Serial.println(Differenza);
//delay(4000);
}
//Time to turn off the LED?
//if ((Differenza = 0) || (Differenza = 1 || (Differenza = -1) && millis() - TempoInizioLoop >= TempoSpegnimentoLed ))
if (millis() - TempoInizioLoop >= TempoSpegnimentoLed )
{
analogWrite(Luce1, 0);
UltimoValorePitch = (pitch);
Serial.print(" differenza = ");
Serial.println(Differenza);
// RESET MILLIS
Serial.print("millis = "); Serial.println(millis());
setMillis(NuovoValoreMillis);
delay(333);
}
}
void setMillis(unsigned long NuovoValoreMillis) {
uint8_t oldSREG = SREG;
cli();
timer0_millis = NuovoValoreMillis;
SREG = oldSREG;
}
// RESET MILLIS
hallo, in this code:
When motion detected
Led Blink-Fader
When no motion detected for 5 seconds, led switch off
Problem is as soon as no motion, led stop brightness instant
than switch off when 5 seconds no motion detected, this is ok
pylon
August 25, 2022, 5:45pm
2
I don't understand what your problem actually is. What does "led stop brightness instant" mean?
Don't do such stuff. It depends on the code that might be changed in future versions of the IDE and it make your code hardly readable. Other code (as libraries) might stop working because it depends on the millis() default behavior.
1 Like
if ((Differenza != 0) && (Differenza != 1 && (Differenza != -1)))
If motion in range, Led start to fade-on-off-on... - ok
analogWrite(Luce1, valoreLed1);
As soon as motion stopped, brightness freeze at actual value - not ok,
it should follow to fade-on-off untill 5 seconds motion in range
Than after 5 seconds motion not in range, switch off - ok
Restart loop - ok
You mean it will work in next updates?
Now can fix with code change?
pylon
August 26, 2022, 3:31pm
4
You depend on an implementation detail of the internal libraries. The Arduino people might change the implementation at any time because they expect people to use only the public interface and not internals.
You have to restructure your code completely. You must remove that setMillis() code as with that code in use you won't be able to have more than one timing feature. It was extremely bad code style anyway, so you should have eliminated it only to keep the code maintainable.
If you detect that there is no motion store the actual time and set a flag. Every time you detect motion you clear that flag again. If the flag is set you check if 5 secs went since you set the flag. During that time you still run the code for fading. Only if more time went by since the flag was set you stop calling that code for fading.
1 Like
i tryed flag way but motion/mpu was stop working when led fading. Maybe wrong code, i retry and post code here
#include <Wire.h>
#include <MPU6050.h>
unsigned long TempoInizioLoop = 0;
const int Luce1 = 11;
const int Luce2 = 12;
int UltimoValorePitch;
int Differenza;
int periodoFadeLed = 900;
int displaceFadeLed = 500;
int valoreLed1, valoreLed2 ;
boolean Mooving = false;
int TempoSpegnimentoLed = 5000;
MPU6050 mpu;
void setup()
{
Serial.begin(9600);
pinMode(Luce1, OUTPUT);
Serial.println("Initialize MPU6050");
while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
}
void loop()
{
// Read normalized values
Vector normAccel = mpu.readNormalizeAccel();
// Calculate Pitch & Roll
int pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis * normAccel.YAxis + normAccel.ZAxis * normAccel.ZAxis)) * 180.0) / M_PI;
//int roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI;
Serial.print(" Pitch = ");
Serial.print(pitch);
Serial.print(" differenza = ");
Serial.println(Differenza);
//Serial.print(" Roll = ");
//Serial.print(roll);
//Serial.println();
delay(200);
Differenza = (UltimoValorePitch - pitch);
// DETECT MOOVING BY COORDINATE DIFFERENCE
if ((Differenza != 0) && (Differenza != 1 && (Differenza != -1)))
{
Mooving = true;
for (int i = 0; i < 255; i = i + 1 ) {
analogWrite(Luce1, i);
delay(10);
//SET LAST COORDINATE VALUE
UltimoValorePitch = (pitch);
}
}
else
//Turn off the LED
{
Mooving = false;
analogWrite(Luce1, 0);
UltimoValorePitch = (pitch);
Serial.print(" differenza = ");
Serial.println(Differenza);
//SET LAST COORDINATE VALUE
UltimoValorePitch = (pitch);
}
}
with this code Led switch off when no moove detected
i need it switch off if no moove detected for 5 seconds
if moove detected, restart timer 5 seconds
pylon
August 26, 2022, 7:14pm
7
You removed all timing code. You should use millis(), just resetting it's value is a very bad idea. Save the relevant values of millis() and calculate what time went by in the meantime to know when to activate the wanted actions.
With millis, led freeze brightness as soon as no moove detect, see code in first post.
If not reset millis, cpu hang after long time.
5 seconds delay not work with led fade,
i posted this code just for show code without setMillis() as you requested.
pylon
August 29, 2022, 4:02pm
9
I already wrote that in the first code you misused millis(). If you use millis() without resetting it's value you can bring it to work.
No, that's not true. The CPU won't hang, it just may have an overrun on that value but used correctly that won't affect you code.
Correct, that's why you should use millis() instead.
system
Closed
February 25, 2023, 4:02pm
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.