The program freezes

The program freezes, and the screen display only starts to refresh after the stepper motor program is executed. I used a non blocking delay, but it still remains the same.

#include <MsTimer2.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <Bounce2.h>
#include <afstandssensor.h>
#include <Stepper.h>


#define OLED_RESET     4
// 这里设置步进电机旋转一圈是多少步
#define STEPS 100
//设置步进电机的步数和引脚(就是注意点2里面说的驱动板上IN1~IN4连接的四个数字口)。
Stepper stepper(STEPS, 4, 6, 5, 7);
AfstandsSensor afstandssensor(A1, A2);  // Starter afstandssensoren på ben 13 og 12.
Adafruit_SSD1306 display(128, 64, &Wire,OLED_RESET);
DHT dht3(13, 11);

unsigned long windowStartTime = 0;
const unsigned long windowDelayTime = 3000; // 窗户旋转的延迟时间,单位毫秒

int Light_val;
float WD;
float Value_cm;
int SD;
int Rain = 8;
int mode = 1;
int BUTTON_PIN = A3;  //模式切换按键引脚 
int speak = 9;
int window_mode = 0;  //1是已经关窗      2是已经开窗
Bounce2::Button button = Bounce2::Button();//实例化一个抖动对象



void updateDisplay() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("TEMP: ");
  display.setCursor(30, 0);
  display.print(WD);
  display.setCursor(62, 0);
  display.print("C");
  display.setCursor(75, 0);
  display.print("Hum: ");
  display.setCursor(99, 0);
  display.print(SD);
  display.setCursor(113, 0);
  display.print("%H");
  display.setCursor(0, 14);
  display.print("Light: ");
  display.setCursor(35, 14);
  display.print(Light_val);
  display.setCursor(70, 14);
  display.print("Rain: ");
  display.setCursor(100, 14);
  if(digitalRead(8)==HIGH)
  {
    display.print("No");
  }
  else
  {
    display.print("Yes");
  }
  display.setCursor(0, 28);
  display.print("Space: ");
  display.setCursor(40, 28);
  display.print(Value_cm);
  display.setCursor(73, 28);
  display.print("CM");
  display.setCursor(0, 42);
  display.print("Window: ");
  display.setCursor(45, 42);
  if(window_mode == 1)
  {
    display.print("CLOSE");
  }
  else if(window_mode == 2)
  {
    display.print("OPEN");
  }
  display.setCursor(0, 56);
  if(mode == 1)
  {
  display.print("Mode1: Auto pattern");
  }
  else if(mode == 2)
  {
    display.print("Mode2: Speak pattern");
  }
  else if(mode == 3)
  {
    display.print("Mode3: Wifi pattern");
  }
  display.display();
  delay(10);
}

void setup() {
  // put your setup code here, to run once:
  // 设置电机的转速:每分钟为90步
  stepper.setSpeed(90);
  Serial.begin(9600); 
  dht3.begin();
  pinMode(13, INPUT);
  pinMode(8, INPUT);
  pinMode(A0, INPUT);
  MsTimer2::set(100, flash); // 500ms period
  MsTimer2::start();  // enables the interrupt.
  button.attach( BUTTON_PIN, INPUT_PULLUP);
  button.interval(5);//间隔是5ms
  display.begin(SSD1306_SWITCHCAPVCC,0x3C);
  display.setTextColor(WHITE);//开像素点发光
  display.clearDisplay();//清屏
  display.setTextSize(1); //设置字体大小 
  display.setRotation(2);
  // MsTimer2::stop();  // disables the interrupt.
}

void window_OFF()
{
  // 逆时针旋转
  if (millis() - windowStartTime >= windowDelayTime) {
    // 逆时针旋转
    stepper.step(2048); //4步模式下旋转一周用2048 步
    windowStartTime = millis(); // 更新窗户操作开始时间
  }
  window_mode = 1;
}

void window_ON()
{
// 顺时针旋转
   if (millis() - windowStartTime >= windowDelayTime) {
    // 顺时针旋转
    stepper.step(-2048); //4步模式下旋转一周用2048 步
    windowStartTime = millis(); // 更新窗户操作开始时间
  }
  window_mode = 2;
}

void loop() {
  WD = dht3.readTemperature();
  SD = dht3.readHumidity();
  Light_val = analogRead(A0);
  Value_cm = afstandssensor.afstandCM();
  switch (mode) {
    case 1:
        if((digitalRead(8)==HIGH || Value_cm < 10 || Light_val > 400) && window_mode == 1)
        {
          window_ON();
        }
        else
        {
          window_OFF();
        }
      break;
    case 2:

      break;
    case 3:
 
      break;
      default:
    
      mode = 0;
      break;
  }
  updateDisplay();
    
}

void flash() {
button.update();//更新
  if (button.pressed()) {               //检测模式选择按键是否按下,如果按下则mode加1,进入不同的模式中
    mode++;
    if (mode > 3) {
      mode = 1;
    }
   }
}

Try the MobaTools library. It does all the stepping in the background using interrupts so your program will not block while the motors move.

1 Like

You have a timer running for window_ON() and window_OFF(), use a similar non-blocking timer for a stepper motor...

my stepper is ULN2003,Can I use this lib ?

my stepper is ULN2003

Show your complete circuit diagram.

The MobaTools stepper library works fine with the ULN2003 druver. See the examples for code examples.

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