2017-06-01 4 views
-3

私はArduinoでプロジェクションをしています。 これはコードです:Arduino for loop無限

#include <ss.h> 

//#include <Lampe.h> 
//#include <Motor.h> 
#include <stdlib.h> 


#include <Arduino.h> 
#include <SoftwareSerial.h> 

void setup() { 
    Serial.begin(9600); 

} 

void loop() { 
    char* cc= "Bon{jour {ca{a v{a et{ toi?"; 
    Ss* str = new Ss(cc); 
    Ss* tr; 
    Serial.println(F("ON COMMENCE")); 
    for(int k=0; k<20; k++){ 
    tr = str->substring(k); 
    Serial.println(str->getString()); 
    Serial.println(tr->getString()); 
    Serial.println(k); 
    Serial.println(F("--------------------------")); 
    } 
    delay(100000); 

} 

とクラスSsの*:私の問題は、ループしている

#include "ss.h" 
#include <stdlib.h> 
#include <Arduino.h> 

Ss::Ss(char* k){ 
    this->length = strlen(k); 
    this->string = (char*) malloc(strlen(k)); 
    strcpy(this->string,k); 
    this->string[length] = '\0'; 

} 

int Ss::lengthh(){ 
    this->length = strlen(this->string); 
    return this->length; 
} 

int Ss::getlength(){ 
    return this->length; 
} 

char* Ss::getString(){ 
    return this->string; 
} 

void Ss::setString(char* k){ 
    this->string = (char*) malloc (strlen(k)); 
    strcpy(this->string, k); 
    this->lengthh(); 
    this->string[strlen(k)] = '\0'; 
} 

char Ss::charAt(int n){ 
    return this->string[n]; 

} 


Ss& Ss::operator=(const Ss& other) { 
     length = other.length; 
     string = (char*) malloc(length); 
     strcpy(string,other.string); 
     string[length] = '\0'; 
     return *this; 
    } 




void Ss::concatt(char*& txt, char* txt2){ 
    int l1 = strlen(txt); 
    int l2 = strlen(txt2); 
    //Serial.println(txt); 
    //Serial.println(txt2); 
    const int ltot = l1+l2; 
    char* txtconcat ; 
    txtconcat = (char*) malloc(ltot); 
    strcpy(txtconcat,txt); 
    txtconcat[l1] = '\0'; 
    strcat(txtconcat,txt2); 
    txtconcat[l1+l2] = '\0'; 
    txt = (char*) malloc(ltot); 
    strcpy(txt,txtconcat); 
    txt[l1+l2]= '\0'; 
    //Serial.println("On est dans concatt : ");Serial.println(txt); 
    free(txtconcat); 

} 




Ss* Ss::substring(int n){ 
     Ss* gauche = new Ss(""); 
     for(int k=0; k<n; k++){ 
     char* toadd = (char*) malloc(1); 
     char c; 
     c = this->charAt(k); 
     char* cl = (char*) malloc(1); 
     cl[0] = c; 
     cl[1] = '\0'; 
     strcpy(toadd,cl); 
     toadd[1] ='\0'; 
     char* tocopy = (char*) malloc(gauche->lengthh()); 
     strcpy(tocopy,gauche->getString()); 
     tocopy[gauche->getlength()] = '\0'; 
     concatt(tocopy,toadd); 
     gauche->setString(tocopy); 
     free(cl); 
     free(toadd); 
     free(tocopy); 
     } 
     gauche->string[gauche->lengthh()]= '\0'; 
     Serial.print(F("On est dans la fonction substring  ")); Serial.println(gauche->string); 
     return gauche; 
} 




void Ss::remove(int pos, int nbchar){ 
    char* left = (char*) malloc(pos-1); 
    char* right = (char*) malloc(this->lengthh()-pos-nbchar); 
    left = strcpy(left,this->substring(pos)->string); 
    right = strcpy(right,this->substringr(pos+nbchar)->string); 
    left[strlen(left)] = '\0'; 
    right[strlen(right)] = '\0'; 
    Serial.println("###"); 
    Serial.println(right); 
    Serial.println(strlen(right)); 
    Serial.println("###"); 
    concatt(left,right);free(right); 
    strcpy(this->string,left); 
    this->string[strlen(left)] = '\0'; 
    Serial.print("On est dans la fonction remove  "); Serial.println(this->string); 
    free(left); 

} 

()。 私のforループは終了せず、遅延は考慮されていません。 forループが10に達すると、loop()関数がリセットされるようです。あなたの助けのための

This is what the BoardCom returne

感謝。

+1

「ループ()関数をリセットする」という意味はどうですか?あなたは何の行動を見ていますか、それはあなたが期待したものとどのように違うのですか? –

+0

さて、カウンタはforループでは20にはならず、10にしかならず、スクリーンショットでは遅延を考慮せずに "ON COMMENCE"という行が繰り返し表示されていることがわかります –

+1

制限されたメモリを持つデバイスに不必要にメモリを割り当て、割り当てた1バイト以上を書き込み、割り当てられたメモリを解放しない場合に発生します。そしてループよりもすべて? –

答えて

1

Arduino環境でコードを開発すると、コードはコアSWにあるmain.cppでコンパイルされます。

このコードは、コードモジュールにある関数loop()を呼び出してループします。ここで

Arduinoのコード:あなたが見ることができます

/* 
* \brief Main entry point of Arduino application 
*/ 
int main(void) 
{ 
    init(); 

    initVariant(); 

    delay(1); 

#if defined(USBCON) 
    USBDevice.attach(); 
#endif 

    setup(); 

    for (;;) 
    { 
     loop(); 
     if (serialEventRun) serialEventRun(); 
    } 

    return 0; 
} 

は、メインで、そのループは、()永遠に呼ばれています!

シリアル通信が有効な場合(if (serialEventRun) serialEventRun();)、主に機能serialEventRun()が呼び出されることがあります。この動作を回避すると、シリアル通信は行われません。

+0

彼はArduinoが再起動しているので、ループ関数の最後に到達することはありません。 –

+0

はい、私はより良い方法で質問を理解しました! :) ...私は質問の下のコメントに返信を追加しました! ... すみません! –

+0

あなたの答えをありがとう!しかし、バッファマネージャを使用するとどういう意味ですか? @Syndorik。 –