无延迟暂停()arduino [英] Pause without Delay() arduino

查看:37
本文介绍了无延迟暂停()arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 arduino uno、逻辑电平转换器、adafruit 双色 LED 矩阵、Raspberry pi 和一个按钮,但我遇到了一些问题.我的目标是,当按下 pi 上的按钮时,它会向 arduino Uno 发送一个信号,这将改变 LED 背包上的动画.问题是必须在循环重新启动时按住该按钮,如果您在其他任何时候单击它就无法工作,我认为这是因为我使用的是 delay() 暂停所有内容.有没有办法不使用delay()来暂停?我听说过使用 Millis() 但我是 Arduino 的新手,我完全不知道我将如何实现它.提前致谢.

I am using an arduino uno, logic level converter, adafruit Bicolor LED Matrix, a Raspberry pi, and a button but am having some issues. My goal is that when the button is pushed on the pi it sends a signal to the arduino Uno which will change the animation on the LED backpack. The issue is that the button must be held down right when the loop restarts and if you click it any other time it wont work, I think this is because I am using delay() which pauses everything. Is there any way to pause without using delay()? I have heard of using Millis() but I am new to Arduino's and im not sure how I would implement it at all. Thanks in advance.

#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include "blinky.h"       // Animation for blinking and just chilling
#include "looking.h"    // This one looks forward when it hears or speaks

Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();  // We are using a bicolor Matrix

int piPin = 3;
char count = 0;

void setup() {
  matrix.begin(0x70);  // Turns on Matrix
  Serial.begin(9600);
  pinMode(piPin, INPUT);  // Tells the pin that it's an input
}

void loop() {
  int sense = digitalRead(piPin); //Reads the Pi pin

  while(sense == HIGH){ count + 1;}

  if(count >= 1) { //If the Raspberry Pi is sending input

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_1, 8, 8, LED_YELLOW); // It will look forward
    matrix.writeDisplay();
    delay(3000);

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_2, 8, 8, LED_YELLOW); // It will look forward
    matrix.writeDisplay();
    delay(200);

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_3, 8, 8, LED_YELLOW); // It will look forward
    matrix.writeDisplay();
    delay(3000);

    matrix.clear();
    matrix.drawBitmap(0, 0, looking_2, 8, 8, LED_YELLOW);  //Makes it look back
    matrix.writeDisplay();
    delay(2000);

    count == 0;

  } else { 
    matrix.clear();
    matrix.drawBitmap(0, 0, blink_1, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(3000);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_2, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_3, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_4, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_5, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_6, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_7, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_8, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_9, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_8, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_7, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_6, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_5, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_4, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_3, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

    matrix.clear();
    matrix.drawBitmap(0, 0, blink_2, 8, 8, LED_GREEN); // It will just blink
    matrix.writeDisplay();
    delay(100);

  }
}

推荐答案

你说得对,你可以用 millis() 替换 delay().一种常见的方法是使用时间比较代码模式,它不会中断主循环.这允许 Arduino 以特定频率或时间执行命令.

You are right, you can replace delay() with millis(). A common approach to that is to use a time comparing code pattern, that does not interrupt the main loop. This allows the Arduino to execute commands in specific frequencies or times.

以下示例举例说明了如何在不使用 delay() 的情况下每秒(和每四秒)执行一个函数.我认为这应该可以解决您的问题并启用伪并行执行:

The following example exemplify how you can execute a function every second second (and every fourth second) without using delay(). I think this should solves your problem and enables a pseudo-parallel execution:

// 2 sec. frequency
unsigned long interval=2000;    // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

// 4 sec. frequency  
unsigned long interval1=4000;    // the time we need to wait
unsigned long previousMillis1=0; // millis() returns an unsigned long.

void setup() {
   //...
}

void loop() {

 // other CMD's...

 if ((unsigned long)(millis() - previousMillis) >= interval) {
    previousMillis = millis();
    // every second second
    // ... 
 }

 // other CMD's...

 if ((unsigned long)(millis() - previousMillis1) >= interval1) {
    previousMillis1 = millis();
    // every fourth second
    // ... 
 }

 // other CMD's...
}

此外,您已经可以在 stackoverflow 上找到一些类似的问题,例如 Arduino Multitasking.

In addition, you can already find some similar questions such Arduino Multitasking on stackoverflow.

这篇关于无延迟暂停()arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆