BLYNK
BLYNK.IO       📲 GETTING STARTED       đź“— DOCS       👉 SKETCH BUILDER

Setinterval blynk interfere the stepper motor accelstepper

i am using a stepper motor nema 17 in the accelstepper library but i need to insert a set interval in the setup, but the time that i set for that to happen is the time that the motor stops. in other words, 1000L every second that I read the analog (or any type) the engine freezes and returns the execution it was in. Any option to replace the setinterval in the setup but continue reading the analog?


void setup ()
timer.setInterval(1000L, rain);

void rain()
{
  int xxc= analogRead(chuva);
}
void loop()
timer.run();

Maybe if you posted your full code instead of snippets, and explained exactly what it is you are trying to achieve, we’d be able to offer some helpful advice.

Pete.

the motor without reading range of the analogical runs normally. But when I put the interval in, the engine stops every 1 second. it spins, after a second lock, keeps turning + 1 second lock again. if I put it in the 2 second interval it does the same thing but every 2 seconds …

#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <AccelStepper.h>
char auth[] = 
  /
  char ssid[] = 
  char pass[] =

#define chuva A0  
#define STEP 12 //D6 
#define DIR 14 //D5

const int fim = 4;       //D2        fim de curso quando abrir
const int liberar = 13;  //porta enable
const int dist = 10000; // numero de passos
int  chuvaselect = LOW;         
void ICACHE_RAM_ATTR fimdecurso(); 


 AccelStepper stepper(1, STEP, DIR);   
 BlynkTimer timer; 

void setup()
{
    Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
    delay(10);
    stepper.setMaxSpeed(4000);
    delay(10);
    homing();
    timer.setInterval(1000L, sensordechuva);
  //timer.setInterval(2000L,homing );
//timer.setInterval(50L, posicao1);
//timer.setInterval(50L, posicao2);
 
  while (Blynk.connect() == false) {
  }
    pinMode(liberar,OUTPUT);
          attachInterrupt(digitalPinToInterrupt(fim), fimdecurso, RISING);
       pinMode(fim,INPUT);  
                 
}


//##################################################################################################################


  void homing(){
  int ifim = digitalRead(fim);
    if (ifim == LOW){
  digitalWrite(liberar, LOW); 
  stepper.setAcceleration(800);
            stepper.setSpeed(500); 
                stepper.moveTo(-50000);
                Blynk.virtualWrite(V5,"Home");                
}
  }
//##################################################################################################################
BLYNK_WRITE(V2)
{
  int chuvaselect = param.asInt();
  
}
  
  void sensordechuva()
{
  int chuvinha = analogRead(chuva);
  if (chuvaselect == HIGH){ 
       
    if (chuvinha >= 700){ 
      Blynk.virtualWrite(V5, "No");
  }
  
  if(chuvinha < 700){     
  Blynk.virtualWrite(V5, "Chuva");
  digitalWrite(liberar, LOW); 
  stepper.setAcceleration(800);
            stepper.setSpeed(300); 
                stepper.moveTo(-50000);
  }
  }
}


//##################################################################################################################

BLYNK_WRITE(V0) {         // fechar janela
  int vel = param.asInt();  
if (vel == LOW)  {
       fecharjanela();
     }
   }     
  void fecharjanela () {    
    digitalWrite(liberar, LOW); 
     stepper.stop();  
       stepper.setAcceleration(800);
            stepper.setSpeed(1000); 
                stepper.moveTo(dist);
                Blynk.virtualWrite(V4,"fechou");
                                 //long posicaomotor1 = stepper.currentPosition();
  }

BLYNK_WRITE(V1) {
  int abrir = param.asInt();
  if (abrir == LOW){
  abrirjanela();
}
}

void abrirjanela(){
  stepper.stop();
  digitalWrite(liberar, LOW);// fechar JANELA
        
     // stepper.setCurrentPosition (move1);
       stepper.setAcceleration(500);
            stepper.setSpeed(1000);     
               stepper.moveTo(0);                   
            }
        
     //###################################################################################################################### 
 void fimdecurso(){
       
 int ifim = digitalRead(fim);
if (ifim == HIGH){
  stepper.stop();
    digitalWrite (liberar , HIGH);
    stepper.setCurrentPosition(0);
        Blynk.virtualWrite(V4," 123");
  }
}


 //###################################################################################################################### 
void loop()
{
  Blynk.run();
  stepper.run();
  timer.run();
  }

But that doesn’t explain what your desired behaviour is.
Take time to explain what it is that you are actually trying to achieve, so we can point you in the direction of the most appropriate solution.

Pete.

Peter I made a video to get better what is happening. Pay attention to the sound and movement. every 1 second it experiences an interruption, which does not happen if I remove the setinterval

it may be because every time the the function runs it is resetting the stepper speed and acceleration. Thus, causing it to experience and interruption.

You could try either increasing the interval it runs to a value that is longer than the time for it to complete the movement. Or add in a flag that does not allow it to run that code again until it gets to its position.

Also, take a look at this example. It may (or may not) help.

1 Like

I had thought about it as soon as I found this error, but it turns out that even if I set a longer reading time it would end up shocking, the reading is a rain sensor for the window to close if it rains.

Sorry, but I am confused by your last reply. Are you saying you that increasing the timer interval to say 10 seconds does not keep the motor from stuttering? Or, that having that long of a time between readings will not work for you application?

As @PeteKnight had mentioned, if you explain what you are actually trying to do (close a window when rain is detected), you may get a suggestion on a better way to accomplish it. If you include more details (i.e. type of sensor to detect rain, hardware, etc.) you may get a more detailed response.

yes.can match and read too. the motors work independently of the sensor, if I use a virtual pin to start the motor and in that interval the 10 seconds of sujeridos finish it will stop as shown in the video. the reading has to be happening all the time so that if it reaches the value it triggers alone. but it cannot interfere with manual activation by the blynk app
this is the sensor I use :slight_smile:

nobody? :frowning_face:

Since I posted this you’ve gone to great lengths to explain what the current behaviour is, including posting a video of the current behaviour, but you haven’t taken time to explain an overview of the project - including where the controls in the Blynk app come in to play.

I’ve taken a back seat waiting for that, which is why you haven’t had any more input from me since.

Pete.

the expected behavior is that there is no interruption in the motor behavior. as you can see in the video. the fact of reading every second interferes with the engine’s rotation. as if it brakes and turns normally again. what is expected and that one thing does not interfere with the other. the sensor reading and to detect rain. The engine had stayed in the window where these pauses during the window’s activation bother, as if something was blocking everything

the expected behavior is natural for an engine. it spins free without crashes.

Okay, still not nearly enough info for me, so I’ll leave it to others who have better psychic skills than me.

Pete.

As @PeteKnight said, you need to provide more background information for people to be able to help. Things like what board you are using, the stepper motor control board type, wiring diagram, etc. would be very helpful for people to give suggestions.

From what I can gather it seems that you are trying to open/close a window via BLYNK, but also have it close if it detects rain. Please advise if this is correct, and maybe expand a bit on the description if you can.

One thing you may be able to try is to see if the timer is causing the disruption, or if it is the code within the timer function. Try commenting out the code inside the function and see if you still have the stutter. If not, slowly add in parts of the code and see where it starts to happen.

Other information that may be helpful is what is the sensor condition when the stutter happens. If its value greater than the 700 threshold that you have set in your program? Does it happen regardless of the value from the sensor?

The sensor you linked to looks like it may also have a digital output that is adjustable vie the potentiometer on the sensor board. Maybe using the digital output with an interrupt routine to trigger the closing may not cause the stuttering, as that function would only run when the sensor triggers.

You also have some of your pinmode declaration inside a while loop, is there a reason for that?

As you can see, the more information you provide the less guessing we have to do, and the easier it is for us to help. And the more likely people are to give their input. Be as detailed as you can, what have you tried and what result did it have? Help us Help you.

1 Like

-I am using nodemcu V2, the drive is a drv8825.
-exactly the window is in manual mode, open and close via blynk but if it detects rain it closes by itself.
-this is exactly the problem is the timer that is causing interruption, my doubt and precisely that if there is another way for me to call a team for reading other than this one (set.interval) to test the functioning.I do as you said . I do parts of the code and test before putting more stuff. and the interruption happens when I interrupt. as you can see there are others commented precisely for not being able to solve this problem (the comments are for knowing the position of the engine that would be useful for me but the same interruption problem happens with them so I commented and stopped using.)

  • there is nothing inside the while loop, there is a switch closing before. it’s your impression. there is no reason to have anything inside the while loop
  • The motor interruption happens regardless if it is> 700 or <700. whatever type of set.interval I use this happens.
  • in other words, set.interval is blocking the motor’s operation normally, as I’m not so good at programming, I came to seek help for not being able to think of anything to do the reading in a time interval that doesn’t use the Void Loop and the set. interval. Sorry for my bad English :frowning:

Pete… I have the same problem … any Setinterval that it contains in the code will cause the stepper motor to stop the evolution and start again at each estimated time … this causes the stepper motor to stop !! is there any way around this?
Why does a Setinterval from another function influence the stepper motor? they shouldn’t be independent !!!

@Celio_P_Silva I’d suggest that you begin a new topic, and start by posting as much relevant information about the project, it’s hardware and the code that you are running, otherwise it’s very difficult to try to help.

Assuming that you are running a board with a single processor core (which we don’t know, because you’ve not shared that information) then the processor can only do one thing at a time. For most projects, this is sufficient, as switching between tasks rapidly manages to “keep all the plates spinning at the same time”, which gives the impression of a multi-tasking/multi-threaded processing environment.

If your hardware/stepper controller requires constant attention from the processor of your MCU to keep it moving smoothly, and you set-up a timer that tells the MCU to go off and do something else for a while then your stepper won’t be getting the attention it needs to keep moving smoothly.

Blynk needs a lot of processor time to make the “Blynk magic” happen, so it’s quite fussy about timing. From my very limited experience of working with stepper motors, they are also very fussy about timing.
You can get the two working together well, provided you don’t want to control multiple steppers at the same time, but the code needs to be well written and efficient.
If you want more control, or simultaneous control of multiple steppers, then you’re probably going to needs to use multiple boards to achieve this. Take a look at this project to see what can be achieved using that approach:

Pete.