Serial While/Loop questions

Hi everyone! I am trying to setup a system where I have to hit # that will make the status wait and recieve all the data in a function. Save it all as a String, then when I hit # again, it leaves the function. With the code provided below, I am able to do it, Expect it looks like the data is garbage and looping into itself. Can anyone help me?

#include <OneWire.h>
#include "pitches.h"

String keylinkData = "";
int serialCommand;

OneWire reader(12);

void setup() {
  Serial.begin(9600);}
  
 void checkStatus(){ byte addr[8]; reader.reset_search();
  if (!reader.search(addr)) {reader.reset_search(); return; } 
  else{iButton(); } }
  
 boolean array_cmp(int *a, int *b, int len_a, int len_b){
            if (len_a != len_b) return false; return true;}
            
 void  iButton(){
      byte i;
      byte addr[8];
      byte addr2[8];
       int arr_a[] = {addr[8]};
       int arr_b[] = {addr2[8]};
       
      reader.reset_search();
      
      while(reader.search(addr)) {
       for( i = 7; i > 0; i--) {
       if (addr[i] < 16) {
        Serial.print('0'); }
        
      if ( addr[2] + addr[6] + addr[5] + addr[4] + addr[3] == 00) {return;}
      
      Serial.print(addr[i] , HEX);
       }       
     if (addr[0] < 2 ) { Serial.print("0"); Serial.print(addr[0], HEX); }
     else { Serial.print(addr[0], HEX); }
     tone(8, NOTE_DS8, 200);
     Serial.println(" "); delay(1000);  }    
    
    
    while(reader.search(addr2)){
  if (array_cmp(arr_a, arr_b, 7, 7) == true){delay(2000); } }
   reader.reset_search();
       }
       
     
   char serialCheck(){
     
     if (Serial.available())
     {
         byte  serialCommand = Serial.read();
                if (serialCommand==35)
                {               
               while ((char)Serial.read() != '#' ) { 
                char inChar = (char)Serial.read(); 
               keylinkData += inChar;  
     Serial.println(keylinkData);           
               
  }
    }    
     }
    }



void loop()
    {
  checkStatus();
  serialCheck();
  Serial.println("Back to loop");
  delay(10);
  Serial.println(keylinkData);
  delay(500);
     }

this is the error I get:

ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

That doesn't look like any kind of error I've ever seen.

Is there any reason you didn't feel it necessary to post your code properly, without italics?

because this would be the first time ever I made a program and I did not know there was a correct format... Sorry

brolly759:
because this would be the first time ever I made a program and I did not know there was a correct format... Sorry

#7 below

http://forum.arduino.cc/index.php/topic,148850.0.html

Back to the problem at hand, I made a dumb down version of the error:

String keylinkData = "";
void setup() {
  
  Serial.begin(9600);
}

void loop() {
 char inChar = (char)Serial.read(); 
    keylinkData += inChar; 
    if (inChar == '#') {
     keylinkData = "";
    }    
    Serial.println(keylinkData);
    delay(500);
  }

That will also create the issue. My best guess is that it is looking at its own return and adding it to the value of inChar but I have no idea how to fix this...

char inChar = (char)Serial.read();

In other words, read a character without bothering to see if there's a character there to read?

Those y-umlaut characters often indicate a speed mismatch on the serial connection.

See but it works here.... If I add it outside the loop in serialEvent it will add all the numbers into the String like expected.

String keylinkData = "";
void setup() {
  
  Serial.begin(9600);
}

void loop() 
{
Serial.println(keylinkData);
delay(500);

}

   void serialEvent() {
  while (Serial.available()) {    
    char inChar = (char)Serial.read(); 
    keylinkData += inChar; 
    if (inChar == '#') {
     keylinkData = "";
    }  
  }
}

Those y-umlaut characters often indicate a speed mismatch on the serial connection.

But more often than not, they indicate you've read -1 and printed it.

Simple serial code examples that check for the presence of characters in the incoming serial buffer.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}
//zoomkat 3-5-12 simple delimited ',' string  
//from serial port input (via serial monitor)
//and print result out serial port

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
  
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

In the first program I showed, How can I hit a key lets say # (which works on the pgroam) then WAIT and anything comes through is going to the String, then hit # again and get out of that loop?

brolly759:
In the first program I showed, How can I hit a key lets say # (which works on the pgroam) then WAIT and anything comes through is going to the String, then hit # again and get out of that loop?

Perhaps you should give a technical explanation of "hit a key lets say #". Are you hitting a key on a pc and sending that character to the arduino via serial? There is sample serial code available with different start and stop data delimiting characters. You chose to use the same character, so you would probably need to set a flag in your code that toggled each time a # is received.

To answer my own question....

Here is what I changed to make it work. I do have another question though. I know the string is loaded in SRam. It is static memory and goes away, but my question is, how long will it stay there? Until I shut off the device? Every time I close the serial terminal and open it, the value disappears even if I do not shut down the device.... What am I doing wrong?

 char serialCheck(){
     
     if (Serial.available())
     {
         byte  serialCommand = Serial.read();
                if (serialCommand==35)
                {               
               while(Serial.available())
                 {
               char inChar = (char)Serial.read(); 
               keylinkData += inChar;
               delay(10);
                 
                                      
  }
    }    
     }
    }

Every time I close the serial terminal and open it, the value disappears even if I do not shut down the device.... What am I doing wrong?

You don't understand your arduino. Opening/closing the serial monitor (or terminal application that toggles DTR) will cause the arduino to reset. You can defeat the reset using a ~100 ohm resistor between the arduino 5v pin and the reset pin, or a large capacitor between the reset pin and ground.

I added a 220ohm resistor and that worked like a charm. Did not have a 100ohm on hand. You are right, I don't know my arduino. I know the basics and as much as I read information on these devices, I learn better if I have a goal. Each obstacle I run into helps me learn more and more by applying the fixes and figuring out work arounds to my mistakes.

Is there any reason to ever have DTR enabled to restart the arduino?

Is there any reason to ever have DTR enabled to restart the arduino?

It makes uploading programs to the arduino easier. Note you will probably need to remove the resistor if you want to upload a program to the arduino.

I just need to hit the manual reset switch and it allows me to program it.