Interfacing with arduino and getting wrong values

isozforever

New Member
Licensed User
Longtime User
Hello Guys!

Iam having a problem interfacing arduino with android.. i try to use TextReader1 and TextWriter1 but there is a huge(3sec) delay and when i try to lower the timer delay it crashes.... i dont know what to do...

Now iam trying to use Async it works fast!! no delay!! but there is a problem, i am getting wrong values.
Exemple: iam waiting for the number 80, iam gatting 080080 its just 2 number! i want to get the right bytes there is something like TextReader.readline? becouse readline stop reading when there is a println Pls help and sorry about my english :sign0013:

Arduino code
B4X:
int letras[4]; // array para esperar 4 letras em formato int diferentes
int analogInPin[4]; //array para abrir as  4 portas analogicas
int mapping=0;
void setup(void) {
  
  Serial.begin(9600);   // abre porta serial 1x
}

void loop(void) {
  if ( Serial.available()) { // Se serial estiver disponivel faca
    char recebido = Serial.read(); // le o que vem da porta serial e atribui em uma variavel do tipo char
    int reconv = recebido; // converte a variavel char para int  exemplo: a letra "a" e igual a 97 de acordo com a tabela asc
    int i = 0;
    for (i=0; i<=3;i++){
      letras[i] = 97+i;
      analogInPin[i] = i; // Aqui gera as letras em formato asc a,b,c,d

      if (letras[i]==reconv){ // se letras geradas for igual a letra convertida 

        int analog = analogRead(analogInPin[i])/4; //Atribui o valor do sensor 0,1,2 ou 3 e divide por 4 na variavel analog


        if (analog > 127){ //se o velor do sensor for maior que 127

          mapping = map(analog,127,255,0,80); //transforma o valor 127 a 255 para 0 a 80, assim fica melhor para controlar o braco robotico no simulador


        }
        else{


          mapping = map(analog,0,127,-80,0); // mesmo do comentario acima 

        }
        
        Serial.println(mapping); // envia a variavel mapping para porta serial, assim o Sketchup pode coletar o valor do sensor
          // O comando readline do python espera o comando \n para poder parar de receber o valor do sensor, caso nao coloque o \n o sketchup vai entrar em um loop eterno. 
      }


    }
  }

}
Android
B4X:
'Activity module
Sub Process_Globals
   Dim Serial1 As Serial
   Dim TextReader1 As TextReader
   Dim TextWriter1 As TextWriter
   Dim Timer1 As Timer
   Dim connected As Boolean
   
End Sub

Sub Globals
   Dim btnSend As Button
   Dim txtLog As EditText
   Dim txtSend As EditText
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Serial1.Initialize("Serial1")
      Timer1.Initialize("Timer1", 3)
   End If
   Activity.LoadLayout("1")
   Activity.AddMenuItem("Connect", "mnuConnect")
   Activity.AddMenuItem("Disconnect", "mnuDisconnect")
End Sub
Sub Activity_Resume
   If Serial1.IsEnabled = False Then
      Msgbox("Please enable Bluetooth.", "")
   Else
      Serial1.Listen 'listen for incoming connections
   End If
End Sub
Sub mnuConnect_Click
   Dim PairedDevices As Map
   PairedDevices = Serial1.GetPairedDevices
   Dim l As List
   l.Initialize
   For i = 0 To PairedDevices.Size - 1
      l.Add(PairedDevices.GetKeyAt(i))
   Next
   Dim res As Int
   res = InputList(l, "Choose device", -1) 'show list with paired devices
   If res <> DialogResponse.CANCEL Then
      Serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
   End If
End Sub

Sub Serial1_Connected (Success As Boolean)
   If Success Then
      ToastMessageShow("Connected successfully", False)
      TextReader1.Initialize(Serial1.InputStream)
      TextWriter1.Initialize(Serial1.OutputStream)
      Timer1.Enabled = True
      connected = True
   Else
      connected = False
      Timer1.Enabled = False
      Msgbox(LastException.Message, "Error connecting.")
   End If
End Sub
Sub mnuDisconnect_Click
   Serial1.Disconnect
   connected = False
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnSend_Click
   If connected Then
      TextWriter1.WriteLine(txtSend.Text)
      TextWriter1.Flush
      txtSend.Text = ""
   End If
End Sub
Sub Timer1_Tick
   If connected Then
      TextWriter1.Write("a")
      TextWriter1.Flush
      
      If TextReader1.Ready Then 'check if there is any data waiting to be read
         Label1.Text = TextReader1.ReadLine
'         txtLog.Text = txtLog.Text & TextReader1.ReadLine & CRLF
'         txtLog.SelectionStart = txtLog.Text.Length
      End If
   End If
End Sub
 
Top