我正在嘗試創建一個陣列結構,其中每個索引都有自己的一組屬性。例如,我希望 channels[0].name 有自己的名稱,而 channels[1].name 也不同。
每次我有一個新的輸入,舊的被遺忘并重復顯示新的。我真的很感激一些提示。
部分代碼:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define LIMIT 26
struct protocol {
String name;
char character;
int values;
int minimum;
int maximum;
};
void create_channels() {
String details;
struct protocol channels[LIMIT];
int i = 0;
do {
byte b = Serial.available();
if (b != 0)
{
details = Serial.readStringUntil('\n');
details.trim();
if (details.startsWith("C")) {
Serial.print("Channel description: ");
Serial.println(details);
channels[i].name = details;
for (i=0; i < LIMIT; i ) {
channels[i].name = details;
Serial.println(channels[i].name);
}
} else if (details.startsWith("V")) {
} else if (details.startsWith("X")) {
} else if (details.startsWith("N")) {
}
else {
Serial.println("Invalid character");
}
}
} while (1);
}
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
Serial.begin(9600);
Serial.setTimeout(10);
lcd.setBacklight(7);
create_channels();
}
void loop() {
}
這是結果:
Channel description: CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
CAMain
Channel description: CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
CBSecondary
uj5u.com熱心網友回復:
對于您從串行輸入中讀取的每個字串,您將channels使用該字串填充整個陣列并將其列印出 'LIMIT' 次。
嘗試取出 for 回圈 - 并將其替換為:
channels[i ].name = details
然后將while(1);條件更改為,while(i < LIMIT);以便在channels陣列已滿時退出回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/468820.html
標籤:C Arduino arduino-uno
上一篇:如何正確重新分配字串陣列?
下一篇:C寫和讀單個字符
