gas sensor reading over bluetooth

Hello,

I’m working on this code to read gas sensor and send the data over bluetooth to another arduino board.

#include <SoftwareSerial.h>

#define GAS_SEN A0

// objects
SoftwareSerial detect_st(4,5); // RX, TX


/****************************** TIMERS ******************************/
// mq4 gas sensor timers:
uint32_t mq4_st;
uint32_t mq4_cr;
uint16_t mq4_pr = 100;

/****************************** VARIABLES ******************************/
// MQ-4 variables:
uint16_t mq4_val;
byte mq4_str[4];

void setup() {
  Serial.begin(9600);
  detect_st.begin(9600);

  pinMode(GAS_SEN,INPUT);

  mq4_st = millis();
}

void loop() { // run over and over


  mq4_cr = millis();
  if(mq4_cr - mq4_st >= mq4_pr){
    mq4_val = analogRead(GAS_SEN);
    sprintf(mq4_str, "%d", mq4_val);
    for(byte i = 0; i<4; i++){
      detect_st.write(mq4_str[i]);
      Serial.write(mq4_str[i]);
    }
    detect_st.write('\n');
    Serial.println();
    mq4_st = millis();
  }

}

Now I’m testing the code to send the data to my smartphone.

As with adjusting the timing, I adjusted the reading and sending the data streams every 100 ms. Is that the fastest reading I can get?

Can I go more than that? For example, 50 ms time delay. But I think at this time the reading get unstable.

I want to know other ideas from you guys :slight_smile:

Thank you,

Test and see - don't be afraid to experiment

Might try increasing the bauds of both Serial and detect_st in addition to lowering the delay

Power_Broker:
Test and see - don’t be afraid to experiment

Might try increasing the bauds of both Serial and detect_st in addition to lowering the delay

Yep absolutely !

That’s what I thought about :slight_smile:

I wanted to know if there’s something I should know about. Always, hints and opinions are very helpful to me.

Thank you,

At 9600 bps you can send only about 1,000 characters per second. You seem to send 4 or 5 characters each time, no problem there. The "slow" analogRead() takes about 0.1 ms, so that's also not it.

That leaves the Bluetooth connection itself as potential bottleneck.

Does it work when you send it to the other Arduino? Only not when sending to your phone? It may be that the app you use is too bloated and slow?

wvmarle: At 9600 bps you can send only about 1,000 characters per second. You seem to send 4 or 5 characters each time, no problem there. The "slow" analogRead() takes about 0.1 ms, so that's also not it.

Ok, thanks for the info about those timings :)

That leaves the Bluetooth connection itself as potential bottleneck.

Does it work when you send it to the other Arduino? Only not when sending to your phone? It may be that the app you use is too bloated and slow?

Well I actually didn't connect it to another arduino, just testing with my smartphone.

Maybe the apps .. got to check that next time and update the progress of this project :)

Thanks,