Hello!
With the help of many people, I was able to get this far.
The slope was obtained through the 9-axis sensor.
And when I tilted the board to the left or right, I made a code to light each LED for a momentarily.
And I want to replace LED on with LED blinking in this code.
When the board tilts, the LED blinks for a certain period of time.
The code below is for LED ON/OFF.
if(val_x < -30 && State == 0) // right on
{
State = 1;
previousMillis = millis();
Serial.println("Right LED ON");
digitalWrite(right_led, HIGH);
digitalWrite(left_led, LOW);
}
/* else if(val_x > 30 && State == 0) // left on(blinking)
{
State = 1;
unsigned long currentMillis_blink = millis();
if(currentMillis_blink - previousMillis_blink > 500)
{
previousMillis_blink = currentMillis_blink;
Serial.println("Left LED ON");
if (led_state_2 == LOW)
led_state_2 = HIGH;
else
led_state_2 = LOW;
digitalWrite(left_led, led_state_2);
}
}*/
else
{
State = 0;
}
if( millis() >= previousMillis + 5000 && State == 0) //LED off after 5 seconds
{
digitalWrite(left_led, LOW);
digitalWrite(right_led, LOW);
Serial.println("LED OFF");
}
I tried to apply millis(), but the code will only become more strange.
I'd really appreciate your help.
Thank you in advance!
You need to look up state machines. You can have 0/1/2. 0 = off ,1 = on, 2 = blinking. write a function for blinking which should be relatively straight forward and if it is going to be active only for a short time then you change back to state 0.
Hello!
I'm using nano33ble board and using IMU, a 9-axis sensor.
I got the slope value from this IMU sensor.
And when I tilted the board to the left or right, I made a code to light each LED for a momentarily.
And I want to replace LED on with LED blinking in this code.
When the board tilts, the LED blinks for a certain period of time.
if(val_x < -30 && State == 0) // right on
{
State = 1;
previousMillis = millis();
Serial.println("Right LED ON");
digitalWrite(right_led, HIGH);
digitalWrite(left_led, LOW);
}
/* else if(val_x > 30 && State == 0) // left on(blinking)
{
State = 1;
unsigned long currentMillis_blink = millis();
if(currentMillis_blink - previousMillis_blink > 500)
{
previousMillis_blink = currentMillis_blink;
Serial.println("Left LED ON");
if (led_state_2 == LOW)
led_state_2 = HIGH;
else
led_state_2 = LOW;
digitalWrite(left_led, led_state_2);
}
}*/
else
{
State = 0;
}
if( millis() >= previousMillis + 5000 && State == 0) //LED off after 5 seconds
{
digitalWrite(left_led, LOW);
digitalWrite(right_led, LOW);
Serial.println("LED OFF");
}
I tried to apply millis(), but the code will only become more strange.
I'd really appreciate your help.
Thank you in advance!
jimLee:
You want the LED to blink faster? Or blink the same speed just for longer/shorter amount of time?
-jim lee
For example, if the board tilts to the left, the left LED (LED1) should flash at 0.5 second intervals for 5 seconds. After 5 seconds, the LED turns off. If tilted in the opposite direction (right) before 5 seconds have passed, the left LED will immediately turn off and the right LED will operate.
class timeoutBlinker : public blinker,
public timeObj {
public:
timeoutBlinker(int pin,float timeOutMs,float blinkOnMs,float periodMs);
virtual ~timeoutBlinker(void);
virtual void setOnOff(bool onOff);
virtual void idle(void);
};
timeoutBlinker::timeoutBlinker(int pin,float timeOutMs,float blinkOnMs,float periodMs)
: blinker(pin,blinkOnMs,periodMs),
timeObj(timeOutMs) { }
timeoutBlinker::~timeoutBlinker(void) { }
void timeoutBlinker::setOnOff(bool onOff) {
if (onOff) start();
blinker::setOnOff(onOff);
}
void timeoutBlinker::idle(void) {
if (blinking()) {
if (ding()) {
setOnOff(false);
}
}
blinker::idle();
}
Above is your class code.
Your part in this is to create the two global blinkers left and right like above.
And in loop() add the call.
idle(); // This runs everything in the background
You will need LC_baseTools from the library manager to compile what I gave you.
So, like above, after computing our your lean, right or left..
if (beginLeaningRight) {
rightBlinker. setOnOff(true);
leftBlinker. setOnOff(false);
else if (beginLeaningLeft) {
rightBlinker. setOnOff(false);
leftBlinker. setOnOff(true);
}
Now, you call this when you BEGIN to lean. You will need to NOT call it over-and-over while leaning, or it'll run-and-run. Call it once, note that it was called, then call it when you lean the other way.
Oh NEVER call delay() or the this won't work.. (And the world will end.)