Increase Arduino Android Sampling Rate(Bluetooth)[Really Importent]

Hi, i am using Arduino Uno,nano and micra. :slight_smile: with bluetooth. And i used GraphView in my android program.

Arduino once read A0 analog potentiometer value and send to my android device with bluetooth. Baud Rate 115200 and delay time 1 ms .
My android program take these values and draw the screen with GraphView.
My problem ise sampling rate very slow. Its abaut 30 Hz.

According to the Nyquist theorem my sampling rate must be min. 80 Hz.
I try this with arduino to matlab serial communication and sampling rate is max 83 Hz.

Arduino include 16 MHz clock and baud rate is enough. However sampling rate is too slow.

How can i increase sampling rate ?

Matlab sampling rate is too slow but enough but Android sampling rate is not enough.

Arduino Code:

#define sensorPin A0
int led=7;
int durum=0;
void setup() {
  Serial.begin(115200);
  pinMode(led, OUTPUT); 
  digitalWrite(led,LOW );
}

void loop() {
  if(Serial.available()>0){ 
    char gelen = Serial.read();
    
    switch(gelen){
     case 'E':potOku(); break; 
     case 'L':ledYak(); break; 
   
    }
  }
}

void potOku(){
 while(1){
   Serial.print('s');
  Serial.print(floatMap(analogRead(sensorPin),0,1023,0,5),2);
  delay(5);
  
  if(Serial.available()>0){
    if (Serial.read()=='Q') return;
  }
 } 
}

void ledYak(){
  
  if(durum==0)
     {
       digitalWrite(led,HIGH ); 
       durum=1;
     }
     else
     {
       digitalWrite(led,LOW);
       durum=0;
    }
  
  }
float floatMap(float x, float inMin, float inMax, float outMin, float outMax){
  return (x-inMin)*(outMax-outMin)/(inMax-inMin)+outMin;
}

My Android function:

public void run() {

 byte[] buffer;  // buffer store for the stream
 int bytes; // bytes returned from read()

 // Keep listening to the InputStream until an exception occurs
 while (true) {
 try {
 try {
 sleep(30);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

 buffer = new byte[1024];
 // Read from the InputStream
 bytes = mmInStream.read(buffer);
 // Send the obtained bytes to the UI activity
 mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
 } catch (IOException e) {
 break;
 }
 }
 }

MATLAB Code:

s = serial('COM4','BaudRate',250000,'Terminator','CR/LF');
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
fopen(s);
 vektor = zeros(1,tmax*rate);
while t<tmax
    t = toc;
 
    a = fscanf(s,'%d')';
    vektor(i)=a(1)*5/1024;

delay(5);?

Yes delaying 5ms its necessary for serialwriting.

My graph experiencing(lag) freezes when 1 ms

all delay()'s stink loose them:

#define SendDelay 5 

unsigned int Timer;
void setup() {
Timer = millis();
  Serial.begin(115200);
 
}

void loop() {
  if (Timer <= millis() ) {
     Timer = SendDelay + millis(); 
      Serial.println("Hello");
  }
 // other code
}

Bad millis() code stinks more.

Your code fails 33 66 seconds after start.

My guess is that your Serial printing is slowing everything up. Revise your code so it only prints (say) once per second and use millis() to manage timing without blocking as illustrated in Several Things at a Time

Make sure to define variables related with millis() as unsigned long.

If you want something to happen quickly WHILE is also usually a bad idea.

...R

Hi, thanks for reply.

My new code;

#define sensorPin A0

#define SendDelay 5 
unsigned long nowTime;
unsigned long preTime;
bool isContinue;
int led=7;
int durum=0;

void setup() {
  Serial.begin(115200);
  pinMode(led, OUTPUT); 
  digitalWrite(led,LOW );
  preTime=0;
  isContinue=false;
}

void loop() {
  if(Serial.available()>0){ 
    char gelen = Serial.read();
    
    switch(gelen){
     case 'E':isContinue=true; break; 
     case 'L':ledYak(); break; 
     case 'Q':isContinue=false; break;
    }
  }


 nowTime=millis();
 if(isContinue && nowTime-preTime>5)
 {
  preTime=nowTime;
  Serial.print('s');
  Serial.print(floatMap(analogRead(sensorPin),0,1023,0,5),2);
  
  
 } 
}



void ledYak(){
  
  if(durum==0)
     {
       digitalWrite(led,HIGH ); 
       durum=1;
     }
     else
     {
       digitalWrite(led,LOW);
       durum=0;
    }
  
  }
float floatMap(float x, float inMin, float inMax, float outMin, float outMax){
  return (x-inMin)*(outMax-outMin)/(inMax-inMin)+outMin;
}

Code is more functional now. But sampling rate is same as before.

You don't seem to set the variable isContinue back to false anywhere.

Your printing interval is only 5 millisecs. Increase that to 1000 and see what happens.

...R

Robin2:
You don’t seem to set the variable isContinue back to false anywhere.

Your printing interval is only 5 millisecs. Increase that to 1000 and see what happens.

…R

i sent to Q in my android code then case ‘Q’:isContinue=false; break; work this.

I increased interval and data trasnfering is slowing? interval must be little time. Cause i want to increase data transfering then so increasing sampling rate drawing data.

I can't understand Reply #8

Please describe in English (not code) what you are trying to do.

Perhaps you need to gather several data points in quick succession and then pause and send them to the Android device rather than send them one at a time?

Can you arrange to do less of the work on the Arduino and more of it on the Android which is much more powerful?

...R

I would like to real-time draw values from the analog sensor. According to the Nyquist theorem, my sampling rate must be above 80 Hz. But my transfering and drawing rate is 30 Hz. If drawing rate is 30 Hz i loss many data. Understand?

How can i increase transfering rate or drawing rate ?

Thx. Akif.

How can i increase transfering rate

Decrease the time between Serial.print() calls.

or drawing rate ?

Write better Android code. This is NOT the place to get help with that.

mkfzdmr: my sampling rate must be above 80 Hz.

What rate does this give you?

void loop() {
   sensorVal = analogRead(sensorPin);
   Serial.println(sensorVal);
   delay(5);
}

...R