Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Simple code results in crash/reboot..

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    4

    Simple code results in crash/reboot..

    I using an Teensy 3.6 but i dont understand whats going here wrong when i send an command to the teensy.. xD
    Code:
    String cmd;
    void SerialSend(char c){
      digitalWrite(4,HIGH);
      delay(10);
      Serial.write(c);
      digitalWrite(4,LOW);
      delay(10);
    }
    
    String SerialRead(){
      cmd = "";
      
      if (!Serial.available()){return "";}
          Serial.println ("DEBUG - 0");
          while (Serial.available()){
          Serial.println ("DEBUG - 1");
          cmd+=SerialRead();      
          Serial.println ("DEBUG - 2");
          digitalWrite(3,HIGH);
          Serial.println ("DEBUG - 3");
          delay(10);
          Serial.println ("DEBUG - 4");
          digitalWrite(3,LOW);
          Serial.println ("DEBUG - 5");
          delay(10);     
          Serial.println ("DEBUG - 6");
          return cmd;
        }
      }
    
    
    void SendString(String s){
      for (int i = 0;i<=(s.length()-1);i++){
        SerialSend(s[i]);
      }  
      Serial.write("\n");
    }
    
    void SysSetup(){
      digitalWrite(0,HIGH);
      Serial.begin(115200);
      SendString("Dongle V0.1");
      
    }
    
    void LedSetup(){
    pinMode(0,OUTPUT);
    pinMode(1,OUTPUT);
    pinMode(2,OUTPUT);
    pinMode(3,OUTPUT);
    pinMode(4,OUTPUT);
    
    
    
    }
    
    void setup() {
      // put your setup code here, to run once:
    SysSetup();
    LedSetup();
    }
    
    
    
    void loop() {
      SendString("Systemtest");
      if (SerialRead() == "TEST"){SendString("TESTSTRING");}
      // put your main code here, to run repeatedly:
    
    }

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,108
    Code:
          cmd+=SerialRead();
    You are calling the SerialRead function from within itself. That's called recursion and is totally inappropriate here because it will fill up the stack and crash the processor, as you have discovered.
    I think what you really meant was
    Serial.read()

    Pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •