How to implement interrupt (or task) inside While Loop in *.CPP file without void loop()

I have this code running in STM32F469 DISCO KIT: discovery7/ui_events.cpp at main · neuberfran/discovery7 · GitHub

The lib SmartDrive Makes the motor run for 5 seconds (by default). When I want to stop the motor before that time, I click the stop button it works ok. Or else the motor stops by itself in 5s

But, when I want the motor to run for more than 5 seconds I need to put this routine: smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Reverse, 90); in While - Loop.

At that moment my issue enters because I am not able to stop the motor through the code below. Does anyone have any tips on how to resolve this? Would I have to use interrupt? Would I have to use another task? How to use?

New ui_events.cpp File with issue:

#include <Arduino.h>
#include "ui.h"
#include <SmartDrive.h>

SmartDrive smd = SmartDrive(SmartDrive_DefaultAddress);

bool STOP01 =  true;
#define STOP02  0

void run01right(lv_event_t * e)
{
    // Your code here
    while (STOP01)
    {
    smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Reverse, 90);
    }
    STOP01 = true;
}

void stopmotor01(lv_event_t * e)
{
    // Your code here
    STOP01 = false;
    smd.StopMotor(SmartDrive_Motor_ID_1, SmartDrive_Action_Brake);
    STOP01 = true;
}

void run01left(lv_event_t * e)
{
    // Your code here
    while (STOP01)
    {
    smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Forward, 90);
    }
    STOP01 = true;
}

Note :1) As the First photo below, it is not possible to implement void loop in this case.

  1. I stay using RTduino in RT-Thread OS

looks like a snippet. Please post the entire code.

What are You talking about? Must be an odd environment You use.

put the button reading in the same while loop

@b707 Thanks for the sugestion. Really, I would have to instantiate the function void stopmotor01(lv_event_t * e)
in an object (variable) and read it inside the while <-> Loop. Is this possible? How to do this?

I think you dont need this.
Where is your function that run motor for 5 seconds?

In the ui folder are all the most important/necessary files for issue resolution.

About your second question:

Regarding the void loop/void setup issue is the following. 1) I created an LVGL project in SLS that exports the ui files from the ui folder above. 2) As per this link: How can i use mqtt in lvgl - #5 by vm123 - How to - Forum - SquareLine Studio
I needed to rename the ui_events.c file to ui_events.cpp. I did this and the project worked fine(more or less). To some extent: MVP Open Close Window Contest RT THREAD/LVGL with STM32F469 DISCOVERY KIT - Part 2 - YouTube

Why. Because if I put the command smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Reverse, 90); in a while <-> loop the motor soh spins for 5s. I can stop with 2s. But I want to stop it over 5s too.

According to the ide I use, it was still not possible to put void setup/void loop

I asked YOUR code with this function, not the library.
Do you meant this?

void SmartDrive::Run_Seconds()

Lib has all the possibilities, including this one (SmartDrive::Run_Seconds()) that I've already tried as follows:

smd.SmartDrive.Run_Seconds(int motor_id, int direction, byte speed, byte duration, bool wait_for_completion=STOP01, int next_action )
Unfortunately, it didn't work. Because I imagine that during Run_Seconds function it doesn't read the boolean variables it should read. Only in the beginning.

I stay using: smd.Run_Unlimited(SmartDrive_Motor_ID_1, SmartDrive_Dir_Reverse, 90);

In an approach with kotlin/Android things with this Lib (written there in Java, of course) I use this too (Run_Unlimited)

In this Link, the author instantiated the variable buttonState( buttonState = digitalRead(interruptPin);
) in a way I would like to do in my project.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.