Problem With SoftwareSerial and my Bluetooh module

Hello Everyone,

I need some help on a thing that maybe is very easy. I'm new in this case. Currently I'm using an arduino mega

I developed a very simple application for android (Joystick) and it is sending to the arduino two simple numbers.

From the arduino I wrote this simple code:

while (Serial1.available() >= 2) {

x = Serial1.read(); delay(10); y = Serial1.read();

} delay(10);

Initially it was working very well, the entire arduino code was just composed by these lines. Everything was perfect.

Once ready I add this lines in my code in which I usually send some signals to my motors controller in order to control them by app. So I add the lines in a more complicated code.

I wrote all the control part in two particular interupts using two different clocks.

The lines related to the Serial.read, instead, are at the moment in the main loop.

This is what happen, the arduino start reading correctly the x and y signal. In a certain moment the reading values switch . So initially I have the right x and y, then x is y and y is x, after some time they are right and so on.

I think that is related to the freqency? once the code is more complecated the main loop doesn't respect the frequency? I don't have more lines in the main loop.

I need to use one interupt to a certain frequency in order to read x and y correctly?

Thank you very much for your time, I hope that my problem is clear enough.

Thank you , :)

Giovanni

On a Mega there are 3 spare HardwareSerial ports so you should not be using SoftwareSerial. And, as you are using Serial1 which connects to Pins 18 and 19 I suspect you are not using SoftwareSerial.

However you have not posted your complete program and it is not possible to answer your question without seeing it.

...R

cairoligiovanni: So I add the lines in a more complicated code.

So show that more complicated code. And use code tags when doing so.

[code]your code here[/code]

will result in

your code here

Hello Everyone,

I’m new in programming, so I’m very sorry

This is the main part:

#include "interrupts.h"
#include "myFunction.h"
#define BIT(n,i) (n>>i&1)

#define Signal 53

//////////////////////////////
////////// Variables//////////
//////////////////////////////

volatile int phase = 0;
volatile int i = 0;
volatile int h = 0;
volatile int j = 0;
volatile int k = 0;
volatile int w = 0;
volatile int a = 0;
volatile int b = 0;
volatile int Ini = 0;
volatile int Num = 60;

volatile int AvaInd = 0;
volatile int DesSin = 0;
volatile int AvaInd1 = 0;
volatile int AvaInd2 = 0;
volatile int DesSin1 = 0;
volatile int DesSin2 = 0;
volatile int Bat = 0;
volatile int Bat1 = 0;
volatile int Bat2 = 0;
volatile int Vel = 0;

volatile int ParAvaInd1 = 0;
volatile int ParAvaInd2 = 0;
volatile int ParDesSin1 = 0;
volatile int ParDesSin2 = 0;
volatile int LastBits1 = 0;
volatile int LastBits2 = 0;
volatile int ParLastBits1 = 0;
volatile int ParLastBits2 = 0;

volatile int NBitFirst = 9434;                                // Da calcolare
volatile int NBitSecond = 9434;                                // Da calcolare
volatile int NBitOUT = 174;
volatile int NBitIN = 1887; //1698; //90ms di ascolto : 0.053 ms //100ms:0.0053=1887                                    // Da calcolare
volatile int StringSignalInitial[] = {1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0,
                                      1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1,
                                      1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1,
                                      1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0,
                                      0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1
                                     };
volatile int NewSignal[]           = {1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0,
                                      1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1,
                                      1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1,
                                      1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0,
                                      0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1
                                     };
int StringSignalMove3[] =   {1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1,
                             0, 1,
                             0, 1, 0, 0, 1, 1,
                             1, 0, 1,
                             0, 1, 1, 0,
                             0, 0, 1, 1,
                             1,
                             0, 1,
                             1, 1, 0, 1,
                             0, 0, 1, 1,
                             0,
                             0, 1, 1, 1,
                             0, 1, 0, 0, 1, 1,
                             0, 0, 1,
                             0, 0, 0, 1,
                             0, 0, 1, 1,
                             0,
                             0, 1,
                             1, 1, 1, 0,
                             0, 0, 1, 1,
                             0,
                             0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1,
                             0, 0, 0, 1,                                        // Parte identificata con movimento Joystick
                             0, 0, 1, 1,
                             0,
                             0, 1,
                             0, 1, 1, 1,
                             0, 0, 1, 1,
                             0,
                             0, 1,
                             1, 1,
                             1, 1, 0, 0, 1, 1, 1, 0, 1,
                             0, 1,
                             0, 1, 0, 0, 1, 1,
                             1,
                             0, 1,
                             1, 0, 1, 1,
                             0, 0, 1, 1,
                             0,
                             0, 1, 1, 1, 0, 1, 1, 1, 1, 1
                            };


//########## Bluetooth variables ################

int xAxis, yAxis;
int  x = 0;
int  y = 0;
int motorSpeedA = 0;
int motorSpeedB = 0;


//##############################################

void setup() {
  cli();

  //set timer1 interrupt at Cycle frequency

  TCCR1A = 0;// set entire TCCR2A register to 0
  TCCR1B = 0;// same for TCCR2B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 10hz increments
  OCR1A = 3124;
  //45 Hz 1387.888 //40 Hz 1561.5 //35 Hz 1784.71 //30 Hz 2082.33 //25 Hz 2499 //15 Hz 4165.66667 //20 Hz 3124 //10 Hz 6249 //5 Hz 12499
  //6 Hz 10415.66667 //7 Hz 8927.57  //8 Hz 7811.5  //9 Hz 6943.4444//100 Hz 624 //90 Hz 693.444444// 80 Hz 780.25// 70 Hz 891.86// 60 Hz 1040.66667//50 Hz 1249//150 Hz 415.66667//200 Hz 311.5//300 Hz 207.333//400 Hz 155.25//500 Hz 124
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS01 and CS00 bits for 64 prescaler
  TCCR1B |= (1 << CS12);
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);

  //set timer3 interrupt

  TCCR3A = 0;// set entire TCCR2A register to 0
  TCCR3B = 0;// same for TCCR2B
  TCNT3  = 0;//initialize counter value to 0
  // set compare match register for 10hz increments
  OCR3A =  2082 ;//(16*10^6) / (freq*256) - 1 (must be <65536)
  // turn on CTC mode
  TCCR3B |= (1 << WGM32);
  // Set CS01 and CS00 bits for 64 prescaler
  TCCR3B |= (1 << CS32);
  // enable timer compare interrupt
  TIMSK3 |= (1 << OCIE3A);

  // Allow interrupts
  sei();
  Serial.begin(9600); // Default communication rate of the Bluetooth module
  Serial1.begin(9600); // Default communication rate of the Bluetooth module

  pinMode(Signal, OUTPUT);
  digitalWrite (Signal, HIGH);
  xAxis = 0;
  yAxis = 0;
}

void loop() {

  while (Serial1.available() >= 2) {
    x = Serial1.read();
    delay(5);
    y = Serial1.read();

  }
  delay(5);

}

then the next part has two different interupts. The first interupts at a certain frequency send the a particular signal the the motors controller.
The second interupt generate the signal NewSignal used in the previous interupt. The signal is characterized by 174 Bit.

#include "interrupts.h"
#include "myFunction.h"
#include <SPI.h>

#define BIT(n,i) (n>>i&1)



ISR(TIMER1_COMPA_vect) {


  switch (phase) {

    case 0:

      Ini = 0;
      pinMode(Signal, OUTPUT);
      //Serial.println("caso 0");

      for (i = 0; i < NBitFirst; i++) {
        digitalWrite (Signal , HIGH);
        delayMicroseconds(46);
      }
      if (i == NBitFirst) {
        phase = 1;
        i = 0;
      }

      break;

    case 1:
      //Serial.println("entrato caso 1");
      pinMode(Signal, INPUT);
      for (j = 0; j < NBitSecond; j++) {
        //digitalWrite (Signal , LOW);
        digitalRead(Signal);
        delayMicroseconds(46);
      }
      if (j == NBitSecond) {
        phase = 2;
        j = 0;
      }
      break;

    case 2:
      pinMode(Signal, OUTPUT);
      //Serial.println("entrato caso 2");

      for (k = 0; k < NBitOUT; k++) {
        digitalWrite (Signal , StringSignalInitial[k]);
        delayMicroseconds(46);
      }
      if (k == NBitOUT) {
        phase = 3;
        k = 0;
        Ini++;
      }
      break;

    case 3:
      //Serial.println("entrato caso 3");

      pinMode(Signal, INPUT);

      for (w = 0; w < NBitIN; w++) {
        //digitalRead (Signal);
        //digitalWrite (Signal , LOW);
        //digitalRead(Signal);
        delayMicroseconds(46);
      }
      if (w == NBitIN && Ini < 4) {
        phase = 2;
        w = 0;
      }
      if (w == NBitIN && Ini >= 4) {
        phase = 4;
        w = 0;
      }
      break;

    case 4:
      pinMode(Signal, OUTPUT);
      //Serial.println("entrato caso 4");
      for (a = 0; a < NBitOUT; a++) {
        digitalWrite (Signal , NewSignal[a]);
        delayMicroseconds(46);
      }

      if (a == NBitOUT) {
        phase = 3;
        a = 0;
      }

      break;

      //pinMode(Signal, INPUT);
  }
}

ISR(TIMER3_COMPA_vect) {



  // Makes sure we receive corrent values
  if (x > 60 & x < 220) {
    xAxis = map(x, 220, 60, 100, -100); // Convert the smartphone X and Y values to 0 - 1023 range, suitable motor for the motor control code below
  }
  if (y > 60 & y < 220) {
    yAxis = map(y, 220, 60, 100, -100);
  }

  //  if(xAxis > 99){ xAxis = 99;}
  //  if(xAxis < -99){ xAxis = -99;}
  //  if(yAxis > 99){ yAxis = 99;}
  //  if(yAxis < -99){ yAxis = -99;}

  Serial.print("x = ");
  Serial.print(x);
  Serial.print("      y = ");
  Serial.println(y);
  // Calcolo AvaInd1 AvaInd2 DesSin1 DesSin2 ParAvaInd1 ParAvaInd2 ParDesSin1 ParDesSin2

  AvaInd1 = PrimaParte(yAxis);
  AvaInd2 = SecondaParte(yAxis);

  DesSin1 = PrimaParte(xAxis);
  DesSin2 = SecondaParte(xAxis);

  ParAvaInd1 = Parit(AvaInd1);
  ParAvaInd2 = Parit(AvaInd2);
  ParDesSin1 = Parit(DesSin1);
  ParDesSin2 = Parit(DesSin2);
  /*
     DA NOTARE QUANDO POSIZIONI I 4 BIT INVERTO LA SEQUENZA, COME DIMOSTRATO NELL'ANALISI DEI SEGNALI
  */


  // set Bit Indietro + parità BIT 34,35,36,37 + 42 N=15
  if (xAxis >= 0 ) {
    //Serial.println("indietro");
    AvaInd = 11;
    NewSignal[34] = BIT(AvaInd, 0);
    NewSignal[35] = BIT(AvaInd, 1);
    NewSignal[36] = BIT(AvaInd, 2);
    NewSignal[37] = BIT(AvaInd, 3);
    NewSignal[42] = 0;
  }
  // set Bit Avanti + parità + parità BIT 34,35,36,37 + 42 N=10
  else if (xAxis < 0 ) {
    //Serial.println("avanti");

    AvaInd = 10;
    NewSignal[34] = BIT(AvaInd, 0);
    NewSignal[35] = BIT(AvaInd, 1);
    NewSignal[36] = BIT(AvaInd, 2);
    NewSignal[37] = BIT(AvaInd, 3);
    NewSignal[42] = 1;

  }

  // set Bit Destra + parità + parità BIT 67,68, 69,70 + 75 N=15
  if (yAxis >= 0 ) {
    //Serial.println("destra");

    DesSin = 11;
    NewSignal[67] = BIT(DesSin, 0);
    NewSignal[68] = BIT(DesSin, 1);
    NewSignal[69] = BIT(DesSin, 2);
    NewSignal[70] = BIT(DesSin, 3);
    NewSignal[75] = 0;
  }

  // set Bit Sinistra + parità + parità BIT 67,68, 69,70 + 75 N=10
  else if (yAxis < 0 ) {
    //Serial.println("sinistra");

    DesSin = 10;
    NewSignal[67] = BIT(DesSin, 0);
    NewSignal[68] = BIT(DesSin, 1);
    NewSignal[69] = BIT(DesSin, 2);
    NewSignal[70] = BIT(DesSin, 3);
    NewSignal[75] = 1;
  }

  // set primi 4 Bit Batteria + parità
  Bat = 9;
  NewSignal[100] = BIT(Bat, 0);
  NewSignal[101] = BIT(Bat, 1);
  NewSignal[102] = BIT(Bat, 2);
  NewSignal[103] = BIT(Bat, 3);
  NewSignal[108] = 1;

  // set secondi 4 Bit Batteria (fissi al MAX) + parità
  Bat1 = 15;
  NewSignal[111] = BIT(Bat1, 0);
  NewSignal[112] = BIT(Bat1, 1);
  NewSignal[113] = BIT(Bat1, 2);
  NewSignal[114] = BIT(Bat1, 3);
  NewSignal[119] = 1;

  // set terzi 4 Bit Batteria (fissi al MAX) + parità
  Bat2 = 15;
  NewSignal[122] = BIT(Bat2, 0);
  NewSignal[123] = BIT(Bat2, 1);
  NewSignal[124] = BIT(Bat2, 2);
  NewSignal[125] = BIT(Bat2, 3);
  NewSignal[130] = 1;

  // set Bit velocità (fissi per ora) + parità
  Vel = 15;
  NewSignal[133] = BIT(Vel, 0);
  NewSignal[134] = BIT(Vel, 1);
  NewSignal[135] = BIT(Vel, 2);
  NewSignal[136] = BIT(Vel, 3);
  NewSignal[141] = 1;

  // set secondi 4 Bit Avanti Indietro  + parità
  NewSignal[45] = BIT(AvaInd1, 0);
  NewSignal[46] = BIT(AvaInd1, 1);
  NewSignal[47] = BIT(AvaInd1, 2);
  NewSignal[48] = BIT(AvaInd1, 3);
  NewSignal[53] = ParAvaInd1;

  // set terzi 4 Bit Avanti Indietro  + parità
  NewSignal[56] = BIT(AvaInd2, 0);
  NewSignal[57] = BIT(AvaInd2, 1);
  NewSignal[58] = BIT(AvaInd2, 2);
  NewSignal[59] = BIT(AvaInd2, 3);
  NewSignal[64] = ParAvaInd2;

  // set secondi 4 Bit Destra Sinistra  + parità
  NewSignal[78] = BIT(DesSin1, 0);
  NewSignal[79] = BIT(DesSin1, 1);
  NewSignal[80] = BIT(DesSin1, 2);
  NewSignal[81] = BIT(DesSin1, 3);
  NewSignal[86] = ParDesSin1;

  // set terzi 4 Bit Destra Sinistra  + parità
  NewSignal[89] = BIT(DesSin2, 0);
  NewSignal[90] = BIT(DesSin2, 1);
  NewSignal[91] = BIT(DesSin2, 2);
  NewSignal[92] = BIT(DesSin2, 3);
  NewSignal[97] = ParDesSin2;

  // Set ultimi Bit di controllo

  LastBits1 = LastBits_1(AvaInd, AvaInd1, AvaInd2, DesSin, DesSin1, DesSin2, Bat, Bat1, Bat2, Vel);
  LastBits2 = LastBits_2(AvaInd, AvaInd1, AvaInd2, DesSin, DesSin1, DesSin2, Bat, Bat1, Bat2, Vel);
  ParLastBits1 = Parit(ParLastBits1);
  ParLastBits2 = Parit(ParLastBits2);


  NewSignal[144] = BIT(LastBits1, 0);
  NewSignal[145] = BIT(LastBits1, 1);
  NewSignal[146] = BIT(LastBits1, 2);
  NewSignal[147] = BIT(LastBits1, 3);

  NewSignal[152] = ParLastBits1;

  NewSignal[155] = BIT(LastBits2, 0);
  NewSignal[156] = BIT(LastBits2, 1);
  NewSignal[157] = BIT(LastBits2, 2);
  NewSignal[158] = BIT(LastBits2, 3);

  NewSignal[163] = ParLastBits2;

}

I want to say thank you again for your help

These are the implemented Function:

#include "myFunction.h"

// calcola la parte decimale del numero e lo riscala da 6 a 15
int PrimaParte(int value1) {
 int Numero = 0;
 int Decimale = value1 / 10;
 Decimale = Decimale % 10;

 if (Decimale == 9) {
   Numero = 6;
 }
 if (Decimale == 8) {
   Numero = 7;
 }
 if (Decimale == 7) {
   Numero = 8;
 }
 if (Decimale == 6) {
   Numero = 9;
 }
 if (Decimale == 5) {
   Numero = 10;
 }
 if (Decimale == 4) {
   Numero = 11;
 }
 if (Decimale == 3) {
   Numero = 12;
 }
 if (Decimale == 2) {
   Numero = 13;
 }
 if (Decimale == 1) {
   Numero = 14;
 }
 if (Decimale == 0) {
   Numero = 15;
 }
 return Numero;
}

// calcola la parte unità del numero e lo riscala da 6 a 15
int SecondaParte(int value1) {

 int Numero = 0;
 int Unit = value1 % 10;

 if (Unit == 9) {
   Numero = 6;
 }
 if (Unit == 8) {
   Numero = 7;
 }
 if (Unit == 7) {
   Numero = 8;
 }
 if (Unit == 6) {
   Numero = 9;
 }
 if (Unit == 5) {
   Numero = 10;
 }
 if (Unit == 4) {
   Numero = 11;
 }
 if (Unit == 3) {
   Numero = 12;
 }
 if (Unit == 2) {
   Numero = 13;
 }
 if (Unit == 1) {
   Numero = 14;
 }
 if (Unit == 0) {
   Numero = 15;
 }

 return Numero;
}

// Restituisce 1 se somma bit pari 0 se somma bit dispari
int Parit(int value1) {

 int Somma = BIT(value1, 0) + BIT(value1, 1) + BIT(value1, 2) + BIT(value1, 3);
 int Resto = Somma % 2;
 int Risultato = 0;

 if (Resto == 0) {
   Risultato = 1;
 }
 if (Resto == 1) {
   Risultato = 0;
 }

 return Risultato;
}

int LastBits_1(int value1, int value2, int value3, int value4, int value5, int value6, int value7, int value8, int value9, int value10) {

 int Numero = 0;
 int Somma = (value1 - 6) + (value2 - 6) + (value3 - 6) + (value4 - 6) + (value5 - 6) + (value6 - 6) + (value7 - 6) + (value8 - 6) + (value9 - 6) + (value10 - 6) + 9; // traslo i valori da 6 a 15 in valori da 0 a 9 e sommo 9 alla fine

 // Ora trasformo la Somma finale in modo da avere 4 Bit di prima parte e 4 Bit di seconda parte

 int Decimale = Somma / 10;
 Decimale = Decimale % 10;

 if (Decimale == 9) {
   Numero = 15;
 }
 if (Decimale == 8) {
   Numero = 14;
 }
 if (Decimale == 7) {
   Numero = 13;
 }
 if (Decimale == 6) {
   Numero = 12;
 }
 if (Decimale == 5) {
   Numero = 11;
 }
 if (Decimale == 4) {
   Numero = 10;
 }
 if (Decimale == 3) {
   Numero = 9;
 }
 if (Decimale == 2) {
   Numero = 8;
 }
 if (Decimale == 1) {
   Numero = 7;
 }
 if (Decimale == 0) {
   Numero = 6;
 }

 // Restituisco solo la Prima parte

 return Numero;
}


int LastBits_2(int value1, int value2, int value3, int value4, int value5, int value6, int value7, int value8, int value9, int value10) {

 int Numero = 0;
 int Somma = (value1 - 6) + (value2 - 6) + (value3 - 6) + (value4 - 6) + (value5 - 6) + (value6 - 6) + (value7 - 6) + (value8 - 6) + (value9 - 6) + (value10 - 6) + 9; // traslo i valori da 6 a 15 in valori da 0 a 9 e sommo 9 alla fine

 // Ora trasformo la Somma finale in modo da avere 4 Bit di prima parte e 4 Bit di seconda parte

 int Unit = Somma % 10;

 if (Unit == 0) {
   Numero = 15;
 }
 if (Unit == 9) {
   Numero = 14;
 }
 if (Unit == 8) {
   Numero = 13;
 }
 if (Unit == 7) {
   Numero = 12;
 }
 if (Unit == 6) {
   Numero = 11;
 }
 if (Unit == 5) {
   Numero = 10;
 }
 if (Unit == 4) {
   Numero = 9;
 }
 if (Unit == 3) {
   Numero = 8;
 }
 if (Unit == 2) {
   Numero = 7;
 }
 if (Unit == 1) {
   Numero = 6;
 }

 // Restituisco solo la Seconda parte

 return Numero;
}

You are pissing memory away like nobody’s business. Why? None of those int arrays, as far as I can see, need to be int.

// calcola la parte unità del numero e lo riscala da 6 a 15
int SecondaParte(int value1) {

 int Numero = 0;
 int Unit = value1 % 10;

 if (Unit == 9) {
   Numero = 6;
 }
 if (Unit == 8) {
   Numero = 7;
 }
 if (Unit == 7) {
   Numero = 8;
 }
 if (Unit == 6) {
   Numero = 9;
 }
 if (Unit == 5) {
   Numero = 10;
 }
 if (Unit == 4) {
   Numero = 11;
 }
 if (Unit == 3) {
   Numero = 12;
 }
 if (Unit == 2) {
   Numero = 13;
 }
 if (Unit == 1) {
   Numero = 14;
 }
 if (Unit == 0) {
   Numero = 15;
 }

 return Numero;
}

or:

// calcola la parte unità del numero e lo riscala da 6 a 15
int SecondaParte(int value1)
{
   return 15 - value1 % 10;
}

Your other functions are equally needlessly complex.

Serial.print() has no place in an ISR. Print the values in loop(), when they change.

int xAxis, yAxis;

Variables used in loop() and in an interrupt handler need to be declared volatile.

Hi Paul,

As I sad I’m new in programming, I would like to improve my self and try to write code in the best way as you suggest, I’m sorry.

Do you think that my problem ( asynchronous reading with the serial) is due to your considerations?

Second question:

“None of those int arrays, as far as I can see, need to be int.”

What do you suggest to use instead?

Thank you very much,

Giovanni

What do you suggest to use instead?

The proper type. The proper type is the smallest type that can hold all the values that might be in the array. For arrays that only ever hold values of 0 or 1, byte is better than int. But, you can pack 8 bits in a byte, so there would still be opportunities for improvement.

Do you think that my problem ( asynchronous reading with the serial) is due to your considerations?

What I do when someone posts code that "doesn't work", and either doesn't explain why or rambles on for 15 minutes and I fall asleep, is that I look at the code, and point out opportunities for improvement.

I got lost in your description of the problem. Serial data is being sent by something, at some interval. You are using that data in an interrupt service routine, for something I can not understand. Meanwhile, some other ISR, being triggered at some frequency I didn't follow, is using delays and may be taking longer than expected to complete.

Whether the issues I pointed out, when corrected, will cause your problem to go away, or not, I have no idea.

You are right,

I will try to use Byte instead of int. After that I will be able to replay.

Thank you for your incitement, it's helpfull in order to find a solution to a problem.

Giovanni

cairoligiovanni: As I sad I'm new in programming,

The code you have posted is certainly not the code for someone who is new to programming.

And post the complete program as a single file. Attach your .ino file if it is too long include in your next Reply.

...R