Search code examples
arduinobluetoothsensorsarduino-uno

How to control the display and non-display of sensor values ​in Arduino using Bluetooth?


I have a sensor that connects to the body and displays muscle signals.
In the setup guide of this sensor, it is said to upload the following code on Arduino, and when we open the Serial Monitor, the sensor values start to be displayed.
Now I want to control the display of these signals using Bluetooth.
So that when I click on the start button in my App, Serial.print() will start working. Also, when I click on the Stop button, the display of these signals and numbers will stop.
Sensor setup guide is this :

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

void loop() {
  Serial.println(analogRead(A0));
}

And this is how it works properly :

enter image description here

But when I upload a piece of code that I wrote to my Arduino, it only shows me just on value.
this is my code :

#include <SoftwareSerial.h>

SoftwareSerial BTserial(0, 1); // RX | TX

char Incoming_value = 0;

void setup() {

  Serial.begin(9600);
  BTserial.begin(9600);

}

void loop() {

  Incoming_value = Serial.read(); // "1" is for Start

  if (Incoming_value == '1') {
    Serial.println(Incoming_value);
   
    StartSensor();

  }
 
}

int StartSensor() {
  int sensorValue = analogRead(A0);
      Serial.println(sensorValue);
      delay(200);
     
}

enter image description here

also please tell me How to write StopSensor Function for Stop print Sensor Value.


Solution

  • Try this code first (Without Bluetooth module)

    #include <SoftwareSerial.h>
    
    SoftwareSerial BTserial(0, 1); // RX | TX
    
    char Incoming_value = 0;
    
    int state = 0;
    
    void setup() {
    
      Serial.begin(9600);
      //BTserial.begin(9600);
    
    }
    
    void loop() {
    
      Incoming_value = Serial.read(); // "1" is for Start
    
      if (Incoming_value == '1') {
        state = 1;
      }
    
      else if (Incoming_value == '0') {
        state = 0;
      }
    
      if (state == 1) {
        StartSensor();
      } else {
        Serial.println(0);
      }
    }
    int StartSensor() {
      int sensorValue = analogRead(A0);
      Serial.println(sensorValue);
      delay(200);
    
    }