I must be doing something wrong. My app seems to be having a hard time with serial data from my Arduino

I'm making at app to control a tool I'm making for work. The way things are now, it technically works... eventually... I originally had a single command to toggle my outputs between HIGH and LOW but the state of the circuits would quickly desynchronize with what was displayed in the app, so I tried making it so when I sent the command from the app, the arduino would write its state change to serial and the app would use that state change data to trigger what was displayed in the app. That worked and things stayed synchronized but it took forever for everything to process. I'd click the button, about 2 seconds later i'd hear the relay trigger, and then about 15 seconds later the app would show that the relay was active. I'd click the button again, 2 seconds later the relay turns off, and about about 15 to 20 seconds later the app would show the relay is off. I wondered if maybe I needed to send less serial data now I have two separate buttons for on and off for each circuit and all the state change displays are handled within the app and not using serial data anymore.

So I have six buttons that control the six circuits on the output side, and I also have six inputs that just toggle between high and low when a high enough value is read, and it's going to have two more analog inputs from a voltage sensor and a current sensor. This is where I'm really getting frustrated. I only have the voltage sensor hooked up right now and I slowed it down so it only writes the voltage value to serial once a second, hoping that might help the app be more responsive, but its still taking a long time to display changes in the analog inputs and all the outputs are still taking several seconds to trigger.

Is this just the way things are when working with serial data in MIT App Inventor? Or is there something I can do to make things more responsive and snappy? I've attached the full text of my Arduino sketch and the export of my app if it will help you help me... Many thanks in advance for any help anyone can offer. I just got home from a busy day my mind's all over the place so let me know if there's any more details I can provide that might be relevant to figuring this out.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
//veh is for vehicle side 
//Trl is for trailer side
//TM is for Tail/Marker lights
//LT is for Left Turn/Brake Light
//RT is for Right Turn/Brake Light
//AUX is for auxilary power/battery charge
//REV is for Reverse/Backup
//voltMeter measures power supply voltage

const int vehTMPin = A0; 
const int vehLTPin = A1;
const int vehRTPin = A2;
const int vehEBPin = A3;
const int vehAUXPin = A4;
const int vehREVPin = A5;
const int voltMeterPin = A6;

unsigned long previousMillis = 0; // one second update interval on voltMeter to slow down volt meter reading on serial channel
const long interval = 1000;

int vehTMVal, vehLTVal, vehRTVal, vehEBVal, vehAUXVal, vehREVVal;
int vehTMstate = 0, vehLTstate = 0, vehRTstate = 0, vehEBstate = 0, vehAUXstate = 0, vehREVstate = 0;
float voltMeter, vltMtrReadout;

const int TrlTMoutput = 2;
const int TrlLToutput = 3;
const int TrlRToutput = 4;
const int TrlEBoutput = 5;
const int TrlAUXoutput = 6;
const int TrlREVoutput = 7;

bool TrlTMoutputState = false;
bool TrlLToutputState = false;
bool TrlRToutputState = false;
bool TrlEBoutputState = false;
bool TrlAUXoutputState = false;
bool TrlREVoutputState = false;

unsigned long previousTime = 0;
const unsigned long interval2 = 500;

void setup() {
  mySerial.begin(9600);
  pinMode(TrlTMoutput, OUTPUT);
  pinMode(TrlLToutput, OUTPUT);
  pinMode(TrlRToutput, OUTPUT);
  pinMode(TrlEBoutput, OUTPUT);
  pinMode(TrlAUXoutput, OUTPUT);
  pinMode(TrlREVoutput, OUTPUT);
}

void loop() {
  
  //Inputs from the vehicle connector
  vehTMVal = analogRead(vehTMPin); 
  vehLTVal = analogRead(vehLTPin);
  vehRTVal = analogRead(vehRTPin);
  vehEBVal = analogRead(vehEBPin);
  vehAUXVal = analogRead(vehAUXPin);
  vehREVVal = analogRead(vehREVPin);
  voltMeter = analogRead(voltMeterPin);
  vltMtrReadout = (55.65/1023.)*voltMeter;

  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    //Volt Meter reading in app
    mySerial.print(vltMtrReadout);
    mySerial.println("VDC");
  }
  
  //Commands to conrol the relays
  if (mySerial.available()) {
    String command = mySerial.readStringUntil('\n');
    if (command == "TrlTMON") {
      TrlTMoutputState = true;
      digitalWrite(TrlTMoutput, HIGH);
    }
    else if (command == "TrlTMOFF") {
      TrlTMoutputState = false;
      digitalWrite(TrlTMoutput, LOW);
    }
    else if (command == "TrlLTON") {
      TrlLToutputState = true;
      digitalWrite(TrlLToutput, HIGH);
    }
    else if (command == "TrlLTOFF") {
      TrlLToutputState = false;
      digitalWrite(TrlLToutput, LOW);
    }
    else if (command == "TrlRTON") {
      TrlRToutputState = true;
      digitalWrite(TrlRToutput, HIGH);
    }
    else if (command == "TrlRTOFF") {
      TrlRToutputState = false;
      digitalWrite(TrlRToutput, LOW);
    }
    else if (command == "TrlEBON") {
      TrlEBoutputState = true;
      digitalWrite(TrlEBoutput, HIGH);
    }
    else if (command == "TrlEBOFF") {
      TrlEBoutputState = false;
      digitalWrite(TrlEBoutput, LOW);
    }
    else if (command == "TrlAUXON") {
      TrlAUXoutputState = true;
      digitalWrite(TrlAUXoutput, HIGH);
    }
    else if (command == "TrlAUXOFF") {
      TrlAUXoutputState = false;
      digitalWrite(TrlAUXoutput, LOW);
    }
    else if (command == "TrlREVON") {
      TrlREVoutputState = true;
      digitalWrite(TrlREVoutput, HIGH);
    }
    else if (command == "TrlREVOFF") {
      TrlREVoutputState = false;
      digitalWrite(TrlREVoutput, LOW);
    }
  }

//vehicle side
if (vehTMVal >= 75 && vehTMstate == 0) { 
    mySerial.println("VehTMON"); 
    vehTMstate = 1; 
  } else if (vehTMVal < 75 && vehTMstate == 1) {
    mySerial.println("VehTMOFF");
    vehTMstate = 0;
  }

  if (vehLTVal >= 75 && vehLTstate == 0) {
    mySerial.println("VehLTON");
    vehLTstate = 1;
  } else if (vehLTVal < 75 && vehLTstate == 1) {
    mySerial.println("VehLTOFF");
    vehLTstate = 0;
  }

  if (vehRTVal >= 75 && vehRTstate == 0) {
    mySerial.println("VehRTON");
    vehRTstate = 1;
  } else if (vehRTVal < 75 && vehRTstate == 1) {
    mySerial.println("VehRTOFF");
    vehRTstate = 0;
  }

  if (vehEBVal >= 75 && vehEBstate == 0) {
    mySerial.println("VehEBON");
    vehEBstate = 1;
  } else if (vehEBVal < 75 && vehEBstate == 1) {
    mySerial.println("VehEBOFF");
    vehEBstate = 0;
  }

  if (vehAUXVal >= 75 && vehAUXstate == 0) {
    mySerial.println("VehAUXON");
    vehAUXstate = 1;
  } else if (vehAUXVal < 75 && vehAUXstate == 1) {
    mySerial.println("VehAUXOFF");
    vehAUXstate = 0;
  }

  if (vehREVVal >= 75 && vehREVstate == 0) {
    mySerial.println("VehREVON");
    vehREVstate = 1;
  } else if (vehREVVal < 75 && vehREVstate == 1) {
    mySerial.println("VehREVOFF");
    vehREVstate = 0;
  }

//blinky blink for turn signals

  unsigned long currentTime = millis();

  if (currentTime - previousTime >= interval2) {
    previousTime = currentTime;
    
    if (TrlLToutputState) {
      digitalWrite(TrlLToutput, !digitalRead(TrlLToutput));
    }
    if (TrlRToutputState) {
      digitalWrite(TrlRToutput, !digitalRead(TrlRToutput));
    }
  }
delay(100);
}

TrailerTester.aia (10.4 KB)

Try it:
TrailerTester.aia (10.2 KB)

I sped up the clock and tweaked your conditional block a bit, and added "\n" to commands because your arduino expects that character in commands.....

1 Like

Patryk, you are my hero once again! Everything is so much better now. I can't thank you enough. I looked around for a way to change the clock speed but couldn't find it and figured it wasn't an option. I was up late last night redoing the sketch to handle the separate on/off buttons and forgot all about the /n lol Tomorrow the current sensor should be arriving in the mail so I'm really looking forward to getting this all set up so I can move all this from the breadboard to an enclosure to use at the shop! again, a thousand thank yous!

In the designer, click on the clock component and you will see several properties you can set.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.