Can someone help me expand this to 10 ,it only works with 2.

for (int i = 0; i < input.length(); i++) {
if (input.substring(i, i+1) == “,”) {
time1 = input.substring(0, i).toInt();
time2 = input.substring(i+1).toInt();
break;

Expand what to ten?

time2 to time10?

Please post the complete program. (As well as answering the question in Reply #1).

...R

i need this function to have 10 of " “variable” = input.substring(i+1).toInt(); ". i am reading “input” from serial. i transmit 10 strings separated by “,” and i need to put them 10 variables but this function only works with 2.

for (int i = 0; i < input.length(); i++) {
if (input.substring(i, i+1) == “,”) {
time1 = input.substring(0, i).toInt();
time2 = input.substring(i+1).toInt();
break;

You mean like an array with a for loop?

i used an array with 10 but doesnt work in my program,i will post the entire program with the array function now.

this is just for testing if i can get strings from serial and parse them and then put them in the eeprom and then read them. i need them for some delays. if anyone can help me,ok,thanks,if you can’t,just shut up,i dont need wise guys making fun,i just need help. i’m not a super programmer.

#include <EEPROM.h>

int lastIndex = 0;
int counter = 0;
String input = “”;
const int numberOfPieces = 10;
String pieces[numberOfPieces];

String ok;

int time1;
int time2;
int time3;
int time4;

int address = 0;
int address1 = 50;

String num;
String num1;

int led = 12;
int led1 = 11;

String temp [10];
String set;
byte index = 0;

void setup(){
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
Serial.begin(9600);

}

void loop(){

// Check for data coming in from serial
if (Serial.available() > 0) {

// Read the first byte and store it as a char
char ch = Serial.read();

// Do all the processing here since this is the end of a line
if (ch == ‘\n’) {

for (int i = 0; i < input.length(); i++) {
// Loop through each character and check if it’s a comma
if (input.substring(i, i+1) == “,”) {
// Grab the piece from the last index up to the current position and store it
pieces[counter] = input.substring(lastIndex, i);
// Update the last position and add 1, so it starts from the next character
lastIndex = i + 1;
// Increase the position in the array that we store into
counter++;
}

// If we’re at the end of the string (no more commas to stop us)
if (i == input.length() - 1) {
// Grab the last part of the string from the lastIndex to the end
pieces[counter] = input.substring(lastIndex, i);
}
}

// Clear out string and counters to get ready for the next incoming string
input = “”;
counter = 0;
lastIndex = 0;
}
else {

//if we havent reached a newline character yet, add the current character to the string
input += ch;
}

time1 = pieces[0].toInt();
time2 = pieces[2].toInt();

}

EEPROM.put(address,time1); //write value to current address counter address
EEPROM.put(address1, time2);

address++; //increment address counter
if(address == EEPROM.length()) //check if address counter has reached the end of EEPROM
{
address = 0; //if yes: reset address counter
}

{
EEPROM.get(address,time1);
EEPROM.get(address1,time2);

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(time1); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(time1);

digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(time2); // wait for a second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
delay(time2);
}}

Declare an array to hold the time values.

const int numberOfPieces = 10;
String pieces[numberOfPieces];
int times[numberOfPieces];

Convert Strings to time values in a loop:

      Serial.print(counter); Serial.println(" values read:");
      for (int idx = 0; idx < counter; idx++) {
        times[idx] = pieces[idx].toInt();
        Serial.println(times[idx]);
      }

      // Clear out string and counters to get ready for the next incoming string
      input = "";
      counter = 0;
      lastIndex = 0;

You’ll have to adjust the rest of your code to use the times array.

thank you Blue Eyes!

I know it’s an old idea, but lose the Strings, use C char arrays, then strtok() will do this in three lines.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R