Usando una variable int con Serial.print() en Arduino

CÓDIGO

    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?


El código de trabajo final:

  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.

preguntado el 05 de mayo de 13 a las 20:05

The second part of the question is ambiguous. Please elaborate more on what you are trying to do. -

1 Respuestas

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){}

a:

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';

contestado el 06 de mayo de 13 a las 10:05

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'}. - AlchemyDragon

You're welcome AlchemyDragon. I edited the answer and tried to answer to your second problem. :) - red_eyes

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. - AlchemyDragon

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.