'class SoftSerial' 没有名为 'readString' 的成员! Arduino DigiSpark attiny85 SoftSerial.h SoftwareSerial.h NeoPixel

'class SoftSerial' has no member named 'readString' ! Arduino DigiSpark attiny85 SoftSerial.h SoftwareSerial.h NeoPixel

我正在使用 Arduino IDE 1.8.12,它设置为使用 digispark 库(我认为),不记得我做了什么,很多年前在网上看过一个视频。

我的草图有问题,当我尝试编译它时它给我这个错误:

 error: 'class SoftSerial' has no member named 'readString'

     dataIn = bluetooth.readString();

                        ^

exit status 1
'class SoftSerial' has no member named 'readString'

我并不感到惊讶,因为原始代码是为 Arduino 编写的,它使用 SoftwareSerial.h 而不是 SoftSerial.h 但由于某些奇怪的原因,我无法加载或开始使用 softwareserial现在,我在 arduino 文件夹中有这些库,但我不知道是否可以加载它们并使用它们,因为我已经将 IDE 设置为与 digispark 一起使用。

另一个解决方案是找到一种以 SoftSerial 识别的方式执行“readString”的方法,我也不确定这是否可行。

请帮忙!

#include <Adafruit_NeoPixel.h> // NeoPixel Lib
#include <SoftSerial.h>  // Serial Lib


#define LED_PIN    1
#define LED_COUNT 30

SoftSerial bluetooth(3, 4); // RX TX
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);


int brightness = 100;   //
int redColor = 0;
int greenColor = 0;
int blueColor = 0;

String dataIn = "";   //

unsigned long timer1 = 0;


void setup()
{
  Serial.begin(9600);
  bluetooth.begin (9600);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(brightness); // Set BRIGHTNESS to about 1/5 (max = 255)

}

void loop(){
  if (bluetooth.available() > 0);{
    dataIn = bluetooth.readString();
    delay(20);
//    Serial.println(dataIn);
    if (dataIn.startsWith("1")){
      delay(10);
      String R = dataIn.substring(dataIn.indexOf("R") + 1, dataIn.indexOf("G"));
      redColor = R.toInt();
      Serial.println(R);
      String G = dataIn.substring(dataIn.indexOf("G") + 1, dataIn.indexOf("B"));
      greenColor = G.toInt();
      Serial.println(G);
      String B = dataIn.substring(dataIn.indexOf("B") +1, dataIn.indexOf("E"));
      blueColor = B.toInt();
      Serial.println(B);
    }

    else if (dataIn.startsWith("2")){
      String stringBrightness = dataIn.substring(dataIn.indexOf("2") + 1, dataIn.length());
      brightness = stringBrightness.toInt();
      strip.setBrightness(brightness); 
      Serial.println(brightness);
    }

    for (int i = 0; i < LED_COUNT; i++){
      strip.setPixelColor(i, strip.Color(greenColor, redColor, blueColor));
    
    }
  }

  strip.show();
  delay(20);
}

使用 read() 在草图中简单地实现相同的功能。

readString 阻塞一段时间。它会冻结等待传输完成的草图 1 秒钟。这对大多数草图来说都不是很好。大多数情况下,实现非阻塞读取会更好。在较小的微控制器(如 UNO,尤其是 digiSpark 中的 tiny85)上使用字符串 class 也存在很大问题。在那个平台上最好坚持使用 c 风格的字符串。但是,如果这确实是您想要做的,那么这里是模仿 readString 的方法。

String myReadString() {

   unsigned long startTime = millis();
   char c = 0;
   String message = "";
   while (c!= '\n' && (millis() - startTime <= 1000)){
      if(bluetooth.available()){
          c = bluetooth.read();
          message += c;
      }
   }
   return message;

然后像这样使用它:

if (bluetooth.available() > 0);{
    dataIn = myReadString();

同样,这不是一件特别好的事情,因为所有这些字符串连接都会在 85 上的少量内存中产生漏洞。这可能就是他们没有在库中实现它的原因为了它。如果我是你,我会花一些时间学习如何使用串行数据。 Google“Robin2 串行输入基础知识”是从 Arduino 论坛阅读的好地方。但是如果你真的坚持使用 String class 那么有一个函数可以完成 readString 的功能。

因此,在阅读了“Robin2 串行输入基础知识”并尝试了一些不同的草图之后,我想出了这个,t 编译并且它在内存参数中等等,但我不知道如何获取我在“receivedChars”中收到的变量并将它们插入此处:

colorWipe(strip.Color(0, 0, 0), 50);

而不是 (0, 0, 0) 有这样的东西:

colorWipe(strip.Color(receivedChars), 50);

#include <Adafruit_NeoPixel.h> // NeoPixel Lib
#include <SoftSerial.h>  // Serial Lib


#define LED_PIN    2
#define LED_COUNT 30

SoftSerial bluetooth(4, 5); // RX TX
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);


const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;




void setup()
{
  Serial.begin(9600);
  bluetooth.begin (9600);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

}



void loop()
{
  recvWithStartEndMarkers();
  controlLed();
}



void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '[=10=]'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}


void controlLed()
{
  if (newData == true )
  {
         colorWipe(strip.Color(0, 0, 0), 50);
  }
  newData = false;
}
 



void colorWipe(uint32_t color, int wait) {
  
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

k,我现在到这里了,还没有工作,但我想我离工作越来越近了

所以现在我正在尝试发送 <100,100,100,100> 并将其存储为 INT 并使用它... 不确定为什么不工作:/ 顺便说一句,我将它作为 ASCII 发送,也许是因为这个?

#include <Adafruit_NeoPixel.h> // NeoPixel Lib
#include <SoftSerial.h>  // Serial Lib
#define LED_PIN    2
#define LED_COUNT 30

SoftSerial bluetooth(4, 5); // RX TX
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;

int redColor = 100;
int greenColor = 100;
int blueColor = 100;
int xbrithness = 20;


void setup() {
  Serial.begin(9600);
  bluetooth.begin (9600);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}


void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with [=10=]
  parseData();
  controlLed();
  newData = false;
  }
}


void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            } else {
                receivedChars[ndx] = '[=10=]'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }   else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}


void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    redColor = atoi(strtokIndx);     // convert this part to an integer
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    greenColor = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    blueColor = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    xbrithness = atoi(strtokIndx);     // convert this part to an integer
}


void controlLed() {
         colorWipe(strip.Color(redColor, greenColor, blueColor), xbrithness);
}
 

void colorWipe(uint32_t color, int wait) {
  
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}