Bluetooth App Crash while sending data

I use 2 because I'm making two joysticks, one vertical the other horizontal.

ok, so how do I add a time loop to my arduino? Will just a Timer.setInterval() function work (I could set it to fire every 100 miliseconds in comparision to the 500 milisecon interval in my app)?

Will this smooth out the data transfer and reduce hang up?

Yes it will, combined with the fixing of the other bugs previously noted by myself and the others. Best to use an elapsed milliseconds approach. See basic example of that below.

You are sending data to the PC (Arduino Serial Monitor). If your Arduino is an UNO, it only has the one port (channel), so if you require the board to send and receive, you need to add the software serial library. The devil is in the detail.

//Receive data from App, Send to Arduino Serial Monitor (PC), Arduino UNO
#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11); // RX, TX

//vars
unsigned long lgUpdateTime;
char val[];

void setup()
{
         Serial.begin(9600);
         Serial1.begin(9600);
         lgUpdateTime = millis();
}

void loop()
{
         //Excute loop every 1/2 second
	     if(millis() - lgUpdateTime > 500) 
         {
               lgUpdateTime = millis();
               
               if (Serial1.available() > 0)
               {
                    //Read Data From App
                    val = Serial1.read();
               
                    //Send to Arduino Serial Monitor on PC (via USB cable)
                    Serial.println(val)
               }
         }
}

This part confuses me, if lgUpdateTime is being set to millis(), won't the result of millis()-lgUpdateTime be equal to 0? How does this loop every 1/2 second?

Hi Neerraj

millis() is not a fixed value, so that's why the method works so nicely. You can of course tweak the time interval. The trick is to get everything working first, optimise once that has been achieved. I noticed you have a lot of "if" conditions in your Sketch for example - you might find you can simplify that later.

The loop will run all the time, it is not blocked for 500ms unlike the "delay" command. Milis uses in the so-called multitasking. First, we load the current value of the milis counter into the variable, then the loop executes and checks in each run whether the current value of the milis counter is greater than the value stored in the variable. If it is higher then it writes the new value of the counter to the variable and executes the code which is to be run every 500ms. So we have something that runs every 500ms without blocking the entire loop, the rest of the program executes without delay.

1 Like