Printing String received from Bluetooth on Serial Monitor

Hi,

I have created a MIT app which sends text through Bluetooth. I was successful in turning LED brightness using the app (not with the below code). Now I want to print the input string revived from the Bluetooth. I tried using the below code but it doesn’t print anything post “Start”.

#include <SoftwareSerial.h>

String s;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
  Serial.begin(9600);
  Serial.println("Start"); 
}

void loop() {
unsigned long currentMillis = millis();
s=Serial.readString();
if(currentMillis - previousMillis >= interval)
{

Serial.println(s);
s="Next";
previousMillis = currentMillis;


}}
  s = Serial.readString();

This removes the String from the input buffer each time through loop() so the buffer will be empty most of the time and certainly by the time that the period ends. Move the Serial.read() into the actions taken at the end of the period and read the input just once before you print it.

Other comments :

#include <SoftwareSerial.h>

Why ?

It would be better to test whether Serial data is available before reading it

Use unsigned long for all timing variables to avoiud problems with millis() rollover to zero

hi there. Looks like you defined your string after you’ve asked it to print it. Is this what you wanted?

#include <SoftwareSerial.h>

String s;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
  Serial.begin(9600);
  Serial.println("Start"); 
}

void loop() {
unsigned long currentMillis = millis();
s=Serial.readString();
s="Next";
if(currentMillis - previousMillis >= interval)
{

Serial.println(s);

previousMillis = currentMillis;


}}
s=Serial.readString();
s="Next";

Read the String then immediately set it to a new value ?

Really ?

I tried simplifying the code but still no luck.

#include <SoftwareSerial.h>

String s;

void setup() {
  Serial.begin(9600);
  Serial.println("Start"); 
}

void loop() {

if(Serial.readString()>0)
{

Serial.println(Serial.readString());
}

}
void loop()
{
  if (Serial.readString() > 0)
  {
    Serial.println(Serial.readString());
  }
}

If serial data is available it is removed by the first readString() so there will be nothing for the second one to read. Use Serial.available() to test whether data is available

#include <SoftwareSerial.h>

Why ?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Which pin(s) does the Bluetooth module connect to ?

The TX pin of bluetooth is connected to Pin 0 (RX) of Arduino and RX pin of Bluetooth is connected to Pin 1 (TX) of Ardunio.

Found a way, this code works. Thank you all.

#include <SoftwareSerial.h>

void setup() {
  Serial.begin(9600);
  Serial.println("Start"); 
  
}

void loop() {

while(Serial.available()==0)
{
  }

char s;
while(Serial.available()>0)
{
  s=Serial.read();
  
Serial.print(s);

}

}

#include <SoftwareSerial.h>I will ask again. Why ?

The TX pin of bluetooth is connected to Pin 0 (RX) of Arduino and RX pin of Bluetooth is connected to Pin 1 (TX) of Ardunio.

Generally the Bluetooth module would be attached to 2 pins defined for the SoftSerial interface leaving pins 0 and 1 free for use by hardware serial. What you have done is an unholy alliance of the two and whilst it might work for this simple project it would be advisable to split the two serial interfaces to avoid interaction.

I was using different TX/RX earlier in the code hence had the used #include <SoftwareSerial.h>. Will remove if from the code.