FIrst post, Someone knows whar is happening on this code?

The function reverte(reverse) work inside the functiion, the values appears right, but when it's return the values look random.

bool debug=1;

void printDebug(String phrase){
  if(debug){
    Serial.println(phrase);
  }
}

String printList(byte list[],byte Size){
  String retorno="[";
  for(byte i=0;i<Size;i++){
      retorno+=String(list[i])+(i<Size-1?',':']');
    }
    return retorno;
}

byte* reverte(byte list[],byte Size){
  byte listaReversa[Size];
  for(byte i=Size;i>0;i--){
    listaReversa[i-1]=list[Size-i];
  }
  printDebug("List: "+printLista(list,8)+", reverse: "+printLista(listaReversa,8)+'.');
  return listaReversa;
}

String Estado(bool test){
  if(test){
    return "on";
  }else{
    return "off";
  }
}

void Ladder(byte primeirosLeds[],byte segundosLeds[],int time,bool state,byte Size){
  printDebug("Lader of leds: "+printList(primeirosLeds,Size)+" and "+printList(segundosLeds,Size)+" to: "+Estado(state)+'.');
  for(short i=0;i<Size;i++){
    printDebug("\Putting ports: "+String(primeirosLeds[i])+" and "+String(segundosLeds[i])+" to: "+Estado(state)+'.');
    digitalWrite(primeirosLeds[i],state);
    digitalWrite(segundosLeds[i],state);
    Espera(time);
  }
}

void setup(){
  byte leds[]={13,12,11,10,9,8,7,6,5,4,3,2,22,24,26,28,30,32,34,36,37,39,41,43,45,46,47,48,50,52};
  for(byte i=0;i<sizeof(leds);i++){
    pinMode(leds[i],OUTPUT);
  }
  Serial.begin(115200);
}

byte ledsAmarelos2[]={22,24,26,28},
     ledsAmarelos3[]={30,32,34,36},
     ledsAmarelos4[]={39,41,43,45},
     ledsAmarelos1[]={46,48,50,52},
     todosAmarelos[]={46,48,50,52,22,24,26,28,30,32,34,36,39,41,43,45,46,48,50,52},
     firstHalfAmarelos[]={46,48,50,52,22,24,26,28},
     segundHalfAmarelos[]={30,32,34,36,39,41,43,45,46,48,50,52}

void loop(){
  byte* reverso=reverte(segundHalfAmarelos,8);
  printDebug("Lista Reversa: "+printList(reverso,8));
  Ladder(firstHalfAmarelos,reverso,500,0,8);
}

Funcoes.ino (4.42 KB)

FuncoesListas.ino (467 Bytes)

re-aprendendo.ino (2.74 KB)

is there a question hidden somewhere?

Please include all your code in your Post so we don't have to download it.

It would also be a big help if you explain the purpose of each program.

...R

The function is returning a pointer to a variable that is local to the function (the scope of the variable is limited to the function). The memory for the variable is allocated at the start of the function, and de-allocated when the function ends, after which it is being used for some other purpose and overwritten.

david_2018:
The function is returning a pointer to a variable that is local to the function (the scope of the variable is limited to the function). The memory for the variable is allocated at the start of the function, and de-allocated when the function ends, after which it is being used for some other purpose and overwritten.

Is there anyway of returning the exact value of the list from the function, not just the pointer?

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Hi,
Sorry it is not your first post.
https://forum.arduino.cc/index.php?topic=676602.msg4553573#msg4553573

Which thread do you want to keep, you cannot have two threads asking the same "question".

Tom.. :slight_smile: