Sketch works on UNO but not on Mega 2560

Hi, I've been trying all weekend to make this code work with the Mega, we needed to connect a gamepad with Arduino so I made a prototype with an array of leds, if I pressed the button 1 (sending the info using Serial communication) it would turn on the first led of the array and so on, the sketch works wonderfully in the Uno but I don't know why it won't work on the Mega, it doesn't turn on the leds at all.

Any help would be really appreciated.

int leds[] = {
  7, 6, 5, 4, 3};

String entrada;

void setup()
{
  for (int i = 0; i < 5; i++){
    pinMode(leds[i], OUTPUT);
  } 

  Serial.begin(9600);
}

void loop()
{
  entrada = "";
  if(Serial.available() > 5)
  {
    entrada = Serial.readStringUntil('.');
    for (int i = 0; i < 5; i++){
      String temp = (String) entrada.charAt(i);
      int temp2 = temp.toInt();
      activar(leds[i], temp2);  
    }
  }  
}

void activar(int led, int estado){
  int estado_;
  if (estado == 0)
  {
    estado_ = LOW;
  }
  if (estado == 1)
  {
    estado_ = HIGH;
  }
  digitalWrite(led, estado_);
}

In your code, why do you need variable estado_? The if statement block really isn't needed:

void activar(int led, int estado){

  digitalWrite(led, estado);
}

I'm assuming that the data coming in over the Serial object is a String like: "10110.", right? Also, adding a trailing underscore character to the name of a function argument is a bad way to name a temporary variable in the function.

Can you tell what the data is that you send to the serial port ? What is the format ? So I can test it on my Mega.

This line is strange : String temp = (String) entrada.charAt(i);
I was expecting this : String temp = String( entrada.charAt(i));
http://arduino.cc/en/Reference/StringConstructor

You can read an integer from the Serial port with : Serial.parseInt() - Arduino Reference

A binary number can be read with : http://arduino.cc/en/Serial/read

Could you give the first parameter the name "pin" ? The name "led" confuses me : void activar(int pin, int estado){

econjack:
In your code, why do you need variable estado_? The if statement block really isn't needed:

void activar(int led, int estado){

digitalWrite(led, estado);
}




I'm assuming that the data coming in over the Serial object is a String like: "10110.", right? Also, adding a trailing underscore character to the name of a function argument is a bad way to name a temporary variable in the function.

Well, you're right, I don't know why I duplicated the variables, so it is fixed now, but the issue still persists.

Yep, the data is coming like you said and it works, but only in the Uno, in the Mega it doesn't do anything. And to add more salt to the wound, that was just a prototype, the full sketch involves controlling a RC car using bluetooth and the gamepad, it works, but I need to use the leds and I can't find a way to make that part work in the Mega, so I'm trying to make the prototype work before implementing the fix on the big sketch.

Thanks for your assistance.

Peter_n:
Can you tell what the data is that you send to the serial port ? What is the format ? So I can test it on my Mega.

This line is strange : String temp = (String) entrada.charAt(i);
I was expecting this : String temp = String( entrada.charAt(i));
http://arduino.cc/en/Reference/StringConstructor

You can read an integer from the Serial port with : Serial.parseInt() - Arduino Reference

A binary number can be read with : http://arduino.cc/en/Serial/read

Could you give the first parameter the name "pin" ? The name "led" confuses me : void activar(int pin, int estado){

I'm sending a string of inputs to the serial port, an int won't work because 001 might be converted into 1 and that would be incorrect.

Regarding the strange line, honestly I didn't find it strange because that's the way I use in java to cast values, so I went from a char to a string so I could then use the toInt() function to get the integer value of that char.

The first parameter of the function activar tells you the position in the led array to use, so let's say activar(1, 1) would mean that I would do a digitalWrite to leds[1] = 6 with a value of HIGH.

Thank you for your time.

Update:

I did the switch, from String temp = (String) entrada.charAt(i); to String temp = String (entrada.charAt(i));

It worked like a charm, I'll be trying it tomorrow on the big sketch, thank you for your assistance guys.

String temp = String (entrada.charAt(i));

Why do you need to piss away resources making a String out of a character? The integer value of '5' is 5. Just subtract '0' from entrada.charAt(i) to get temp2.