1

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.

1
  • The second part of the question is ambiguous. Please elaborate more on what you are trying to do. Commented May 5, 2013 at 20:19

1 Answer 1

1

The program enters in busy waiting after it attempts to read. So it won't read anything. Change this part:

int lng = Serial.read();
while(Serial.available() == 0){}

into:

int lng;
while(1)
    if (Serial.available() > 0)
    {
        lng = Serial.read();
        break;
    }

Regarding the second problem:

char myString[lng];
for (int i = 0; i < lng; i++)
    myString[i] = '0';
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you red_eyes. BY the way love your image. The code fix the problem. I also had to change the line ` Serial.print(lng, DEC); ` to ' Serial.println((char)lng); ' to get it to print the same value that I put in. Also the second problem is if I put in 3 for lng value then I wold like for sting to equal '000'. Or possible in the array format so it is easier to change a specific char. EX: lng = 3 so that string[3] = {'0','0','0'}.
You're welcome AlchemyDragon. I edited the answer and tried to answer to your second problem. :)
That helped. I had to add another line before the for statement. int l = (lng - 48); is what I had to do, because the arduino sees a 51 if I input a 3. Dang those ascii tables. I am going to post the final code that works. Thanks for all of your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.