I'm trying to make a struct of array where each index has its own set of properties. For example, I want channels[0].name to have its own name and channels[1].name to be different too.
Each time I have a new input, the old one is forgotten and shows the new one repeatedly. I would really appreciate some tips.
Part of the code:
#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() {
}
This is the result:
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
CodePudding user response:
For each string you read from the the serial input you're populating the whole channels
array with that string and printing it out 'LIMIT' times.
Try taking out the for loop - and replacing it with:
channels[i ].name = details
Then change the while(1);
condition to while(i < LIMIT);
so the loop exits when the channels
array is full.