CODE
void setup() {
Serial.begin(9600);
int r = 0;
Serial.print("How long\n");
int lng = Serial.read();
while (Serial.available() == 0) {
//Empty
}
char string[] = {'0'};
while (r < 62) {
if(r == 10) {
string[0] += 7;
}
if(r ==36) {
string[0] += 6;
}
Serial.println(string);
r ++;
string[0] ++;
}
Serial.print(lng, DEC);
}
void loop() {
}
Okay, so the first problem is the line Serial.print(lng, DEC);. It prints out a -1 on the serial monitor. If I input a 3 during int lng = Serial.read(), how can I get it to return the input?
Second, how would I set the length of string to lng and make sure each space starts with a 0 instead of being blank?
The final working code:
void setup() {
Serial.begin(9600);
int r = 0;
Serial.print("How long\n");
int lng;
while (1)
if (Serial.available() > 0)
{
lng = Serial.read();
break;
}
int l = (lng - 48);
char string[l];
for (int i = 0; i < l; i++)
string[i] = '0';
while (r < 62) {
if (r == 10) {
string[0] += 7;
}
if (r == 36) {
string[0] += 6;
}
Serial.println(string);
r++;
string[0]++;
}
char eof = '/';
Serial.println(eof);
}
void loop() {
}
The char eof = '/' is because I have a Python script that talks to the Arduino. The printing of the eof is a flag to let Python know that it is done printing and to close the serial connection.