Code not working on Leonardo ?

This code work perfectly on the UNO, but cannot seem to get it to work on Leonardo.

Totally puzzled....

#define REVERSE
#ifdef REVERSE
  #include <InvertedTM1638.h>
#else
  #include <TM1638.h>
#endif
const int DATA_PIN=8;
const int CLOCK_PIN=9;
const int STROBE_PIN=7;

#ifdef REVERSE
   InvertedTM1638 module(DATA_PIN, CLOCK_PIN, STROBE_PIN);
#else
   TM1638 module(DATA_PIN, CLOCK_PIN, STROBE_PIN);
#endif


byte Gear; 
int Speed, Fuel_Remaining;
String Velocidad, Velocidad2;
String Fuel, Fuel2;
word Vel;
byte TramaSerie[8];
short int Puntero,Modo_Hud;
//57375
word leds [10] = {0, 1, 3, 7, 15, 31, 8223, 24607, 57375, 65535};
void setup()
{
  // Inicializar puerto serie a 9600 baudios
  Serial.begin(9600);
  Modo_Hud=-1;
}

void loop()
{

  if (Serial.available()>0)
  {
      if (Serial.available()>8)
      {
        Puntero=0;
        memset(TramaSerie, 0, sizeof(TramaSerie));
        TramaSerie[Puntero]=Serial.read();
        if (TramaSerie[Puntero]==0xFF)
        {
          for (Puntero=1; Puntero<8; Puntero++)
          {
            TramaSerie[Puntero]=Serial.read();
          }
          if( TramaSerie[1]!=Modo_Hud)
          {
           module.clearDisplay(); 
          }
          switch(TramaSerie[1])
          {
            case 0: Display_Velocidad();
                    break;
            case 1: Display_Fuel();
                    break;
            default: Display_Velocidad();
                    break;
          }
          module.setLEDs(leds[TramaSerie[5]]);
          Modo_Hud=TramaSerie[1];
         }
      }   
  }
  
}

void Display_Velocidad()
{
  switch(TramaSerie[2])
  {
    case 255:
        module.setDisplayToString("r", 0,0);
        break;
    case 0:
        module.setDisplayToString("N", 0,0);
        break;
    default:
        module.setDisplayToString(String(TramaSerie[2],DEC), 0,0);
  }
  Vel=(TramaSerie[4] << 8)| TramaSerie[3];
  Velocidad=String(Vel,DEC);
  Speed=Velocidad.length();
  switch (Speed)
  {
    case 1: Velocidad2="  "+Velocidad;
            break;
    case 2: Velocidad2=" "+Velocidad;
            break;
    default: Velocidad2=Velocidad;
            break;
                     
  }
  module.setDisplayToString(Velocidad2, 0,5);
}

void Display_Fuel()
{
  module.setDisplayToString("FUEL",0,0);
  Fuel=String(TramaSerie[6], DEC);
  Fuel_Remaining=Fuel.length();
  switch (Fuel_Remaining)
  {
    case 1: Fuel2="  "+Fuel;
            break;
    case 2: Fuel2=" "+Fuel;
            break;
    default: Fuel2=Fuel;
            break;
  } 
  module.setDisplayToString(Fuel2, 0, 5);
}

It seems to not serial the information.

For the Leonardo, you should wait until you open the serial monitor on the computer.

  Serial.begin(9600);
  while(!Serial);           // wait until the serial monitor on the computer is opened.

Thanks Peter,

I tried this and it still doesn't work.

I have just looked at the UNO and the TX lights flashes when the API is running, however the LEONARDO does nothing ?

Ok quick update,

I have now noticed that the information has been sent to the Leonardo, however the script is not refreshing the RX.

As when i have the API running, and then press to send the information is received and displayed.

However it will only send the information once... and is not refreshing.....the data to the LED1638

Unlike the UNO..

What are you sending the data to? How is that connected to the Arduino?

On the UNO, pins 0 and 1 are connected to Serial. On the Leonardo, they are connected to Serial1.

Arduino Uno, Yeti and TM1638

This is my setup.

The API information is sent to serial port , then read into bytes by UNO and sent out to the TM1638 - LED Module for display.

During this operation the UNO's RX light will flash as information is received.

However the Leonardo nothing happens.

As the previous post i noticed that if i have the API program running and then upload the sketch to the Leonardo is sudden display everything correctly on the TM1638. However it then fails to continue to refresh this information. i.e. a freeze frame of information, but not streaming. Until i upload the sketch and then it will update the information

This has lead my to believe that the hardware is functioning, but there may be a coding issue or hardware limitations, within the Leonardo.

The Leonardo is not the same as the Uno. It has different functions for the pins, the input voltage levels for a digital pin are not the same, and the serial communication to the computer is not the same.
You should test every part on its own.
When the serial communication is a problem, in some rare cases it helps to remove the bootloader.

Testing the display is not so hard. Create a test sketch to show a lot of numbers and see if everything is okay.

The serial communication is different, it is different for the computer, for the sketch and for the timing. If for example your TM1638 code was using the delay of the Serial library, then you are in trouble. The Leonardo does not really use the baudrate, the data is kind of free running over the usb cable.

Checked the TM1638 working, checked API using the Arduino.

I have seen some complaing about the DTR function of the Leonardo can freeze serial data.

The DTR function of the Leonardo ? Is that even possible ?

The computer uses a DTR signal for an Arduino Uno to reset it. That starts the bootloader and the computer can upload a new sketch.
For the Leonardo, instead of using the DTR, the baudrate is set to 2400 baud.