如何通过串行方式将号码发送到arduino?

时间:2019-05-30 09:06:51

标签: arduino serial-port processing

我正在尝试通过串行方式将数字值发送到Arduino,以控制可单独寻址的LED灯条。使用Arduino IDE“串行监视器”,我可以毫无问题地将编号发送到灯条。但是,当我尝试通过在处理中从文本文件读取来从外部进行操作时,它不会通过。

经过一些调试后,我可以看到Processing具有正确的数字并将其存储在变量中。但是,灯条只会照亮led,而不是所给的数字。

处理代码:

import processing.serial.*;
import java.io.*;


Serial myPort;  // Create object from Serial class

void setup() 
{
  size(200,200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}
void draw() 
{
  while(true) {
  // Read data from the file
  {
    String[] lines = loadStrings("Lifenumber.txt");
    int number = Integer.parseInt(lines[0]);
    println(number);
    myPort.write(number);
    delay(5000);
  }
  }
}

Arduino代码:

if ( Serial.available()) // Check to see if at least one character is available
  {
    char ch = Serial.read();
      if(index <  3 && ch >= '0' && ch <= '9'){
      strValue[index++] = ch; 
      }
      else
      {
        Lnum = atoi(strValue);
        Serial.println(Lnum);
        for(i = 0; i < 144; i++)
        {
          leds[i] = CRGB::Black; 
          FastLED.show(); 
          delay(1);

        }
        re = 1;
        index = 0;
        strValue[index] = 0; 
        strValue[index+1] = 0; 
        strValue[index+2] = 0; 
    }
  }

我希望程序执行的操作是从文本文件中读取一个数字,然后点亮144led灯条上的该数量的LED。

1 个答案:

答案 0 :(得分:0)

这里有一些关于您的代码的注释,这些注释应有助于将来进行改进。尽早养成良好的编码习惯很重要:它将使您的生活变得更加轻松(我说的是一个主要是自学成才的,以Flash开头的程序员,所以我知道杂乱无章是什么;))

import processing.serial.*;
//import java.io.*; // don't include unused imports


Serial myPort;  // Create object from Serial class

void setup() 
{
  size(200,200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  // handle error initialising Serial
  try{
    myPort = new Serial(this, portName, 9600);
  }catch(Exception e){
    println("Error initializing Serial port " + portName);
    e.printStackTrace();
  }
}
void draw() 
{
  // don't use blocking while, draw() already gets called continuously
  //while(true) {
  // Read data from the file
  //{
    // don't load the file over and over again
    String[] lines = loadStrings("Lifenumber.txt");
    int number = Integer.parseInt(lines[0]);// int(lines[0]) works in Processing, but the Java approach is ok too
    println("parsed number: " + number);
    if(myPort != null){
      myPort.write(number);
    }
    // don't use blocking delays, ideally not even in Arduino
    //delay(5000);
  //}
  //}
}

这是处理代码的一个版本,可加载文本文件,解析整数,然后仅将其发送到串行一次(如果一切正常,否则基本错误检查应显示调试友好信息):

import processing.serial.*;
Serial myPort;  // Create object from Serial class

void setup() 
{
  size(200,200); //make our canvas 200 x 200 pixels big
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  // handle error initialising Serial
  try{
    myPort = new Serial(this, portName, 9600);
  }catch(Exception e){
    println("Error initializing Serial port " + portName);
    e.printStackTrace();
  }
  // Read data from the file
  try{
    String[] lines = loadStrings("Lifenumber.txt");
    int number = Integer.parseInt(lines[0]);
    println("parsed number: " + number);
    // validate the data, just in case something went wrong
    if(number < 0 && number > 255){
      println("invalid number: " + number + ", expecting 0-255 byte sized values only");
      return;
    }
    if(myPort != null){
      myPort.write(number);
    }
  }catch(Exception e){
    println("Error loading text file");
    e.printStackTrace();
  }
}
void draw() 
{
  // display the latest value if you want
}

// add a keyboard shortcut to reload if you need to

您的测试条有144个led(小于255个),非常适合一个字节,这是处理草图发送的

在Arduino方面,没有什么太疯狂的事情了,因此只要您验证传入的数据就可以了:

#include "FastLED.h"

#define NUM_LEDS 64 
#define DATA_PIN 7
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

int numLEDsLit = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("resetting");
  LEDS.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
  LEDS.setBrightness(84);
}

void loop() {
  updateSerial();
  updateLEDs();
}

void updateSerial(){
  // if there's at least one byte to read
  if( Serial.available() > 0 )
  {
    // read it and assign it to the number of LEDs to light up
    numLEDsLit = Serial.read();
    // constrain to a valid range (just in case something goes funny and we get a -1, etc.)
    numLEDsLit = constrain(numLEDsLit,0,NUM_LEDS);
  }  
}

// uses blacking delay
void updateLEDs(){
  // for each LED
  for(int i = 0; i < 144; i++)
  {
    // light up only the number of LEDs received via Serial, turn the LEDs off otherwise
    if(i < numLEDsLit){
      leds[i] = CRGB::White;
    }else{
      leds[i] = CRGB::Black;
    }
    FastLED.show(); 
    delay(1);  
  }
}

调整引脚/ LED芯片组/等。根据您的实际物理设置。