简体   繁体   English

Arduino-执行循环时读取串行数据

[英]Arduino - Reading Serial Data while performing for loop

I just got my arduino the other day. 前几天我刚得到了arduino。 I am currently making a program in java to control my arduino using serial communication. 我目前正在用Java编写一个程序,以使用串行通信控制arduino。 So far, I only have the program turn it on or off. 到目前为止,我只打开或关闭该程序。 But I ran into an issue. 但是我遇到了一个问题。 I have my arduino fade two rgb leds, looping through every color. 我的arduino淡入两个rgb led,在每种颜色之间循环。 I run into my issue here. 我在这里碰到我的问题。 When I press the button to turn it off(java program), it doesn't turn off until its ran through every color(complete the for loops). 当我按下按钮将其关闭时(java程序),它直到所有颜色都运行时才关闭(完成for循环)。 I want it to instantly shut off. 我希望它立即关闭。 Is there any way I can read serial data in the for loops, or is there any possible way I can turn it off instantly, not having to wait for the for loops to complete? 有什么方法可以读取for循环中的串行数据,或者有什么可能的方式可以立即关闭串行数据,而不必等待for循环完成? Here is the code: 这是代码:

const int redPins[] = {11,6};
const int greenPins[] = {10,5};
const int bluePins[] = {9, 3};

const int pinCountPerColor = 2;

const int sensorPin = 0;
int lightLevel;
int val = 0;
boolean isOn;

void setup() {
  Serial.begin(9600);
  setColourRgb(0,0,0);
  isOn = true;
}

void loop() {
  if(isOn) {
    unsigned int rgbColour[3];

    lightLevel = analogRead(sensorPin);

    if(lightLevel >= 400) { 
      rgbColour[0] = 255;
      rgbColour[1] = 0;
      rgbColour[2] = 0;  

      for (int decColour = 0; decColour < 3; decColour += 1) {
        int incColour = decColour == 2 ? 0 : decColour + 1;

        for(int i = 0; i < 255; i += 1) {
          lightLevel = analogRead(sensorPin);
          if(lightLevel <= 400) { 
            setColourRgb(255, 255, 255);
          } else {
            rgbColour[decColour] -= 1;
            rgbColour[incColour] += 1;

            setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
            delay(5);
          }
        }
      } 
    } else {
      setColourRgb(255, 255, 255);
    }
  }
}

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  for(int r = 0; r < pinCountPerColor; r++) {
    analogWrite(redPins[r], red);
  }
  for(int g = 0; g < pinCountPerColor; g++) {
    analogWrite(greenPins[g], green);
  }
  for(int b = 0; b < pinCountPerColor; b++) {
    analogWrite(bluePins[b], blue);
  }
}

void serialEvent() 
{
  while (Serial.available())
  {
    val = Serial.parseInt();
    if(val == 1)   
    {
      isOn = true;
      //do nothing
    }
    else if(val == 0) 
    {
      setColourRgb(255, 255, 255);
      isOn = false;
    }
  }

  Serial.println("Succesfully received.");
}

It is best to create a state machine for the colors, that does not have any blocking for loops, to dwell in. Especially loops with delays. 最好为颜色创建一个状态机,该状态机不留任何循环阻塞,特别是对于有延迟的循环。 So that each cycle through the loop changes the color and polls the Serial. 这样循环中的每个循环都会更改颜色并轮询序列号。

This is the art to writing real time code, or simply non-blocking code. 这是编写实时代码或简单地非阻塞代码的艺术。

Note it is possible to create a timer interrupt to better schedule RGB updates. 请注意,可以创建一个定时器中断来更好地调度RGB更新。 See Answer about Timer Interrupts to write precision updates. 请参阅有关定时器中断的答案以编写精度更新。

@mpflaga answered your question correctly. @mpflaga正确回答了您的问题。

I would add that if you projects get bigger than just two leds fading, you might want to use something more reliable than a hand-made state machine. 我要补充的是,如果您的项目变得不仅仅是两个led衰落所致,您可能希望使用比手工制造的状态机更可靠的产品。

That is called a Real Time Operating System (RTOS) and it allows you to have different threads running at different frequencies. 这称为实时操作系统(RTOS),它允许您以不同的频率运行不同的线程。

I personally use ChibiOS/RT for my robot project . 我个人将ChibiOS / RT用于我的机器人项目 There is a port for Arduino you can download here , it's very well documented and I'd say pretty easy to use once you get the basics. 有一个用于Arduino的端口,您可以在此处下载,该端口已被很好地记录下来 ,一旦您掌握了基础知识,我会说它非常易于使用。 The nice thing to do is to add a higher level layer to manage the threads. 不错的事情是添加一个更高层次的层来管理线程。

Here is a page with describing it and other solutions : Arduino rtoslibs 这是描述它和其他解决方案的页面: Arduino rtoslibs

And here are some tutorials on real time and ChibiOS for Arduino: 以下是一些实时和Arduino ChibiOS教程:

Hope it helps! 希望能帮助到你! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM