Arduino无限循环

时间:2017-06-01 13:06:33

标签: c++ arduino

我一直在为Arduino工作。 这是代码:

#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 :(得分:1)

当您在Arduino环境中开发代码时,您编写的代码将使用main.cpp进行编译,该代码将进入核心SW。

此代码循环调用代码模块中的函数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;
}

你可以看到,主要是永远调用loop()!

请注意,在main中,启用串行通信时可以调用函数serialEventRun()if (serialEventRun) serialEventRun();)。如果避免这种行为,您将无法进行串行通信。