Serial.println() won't work in the loop()

Hi guys !

This is my first topic here so please accept my apology if I don’t respect posting rules :slight_smile:

I am trying to send data using a bluetooth module when a button is pressed.


UPDATE : IT APPEARS THAT I JUST CANT send any message from the program…
The initial Serial.println(“geeereeeeeek”); is written to the console but not sent via bluetooth even from the setup()

However, when I go to the modem window and type a message then “send”
the message is well sent via bluetooth.
Did I muss something here ?


To do so, I followed a bluetooth start guide that works like a charm. A message is sent upon starting and well received by the destination device :

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0,1);

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

void loop(){
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }}

However, things start getting complicated when I try sending data from the loop() function.

When I do :

  Serial.begin(9600);
  Serial.println("geeek");

Alone in the loop() or as a full function with button click detection :

#include <SoftwareSerial.h>
#define BTN_PIN 2

SoftwareSerial mySerial(0,1);

void setup() {   
  pinMode(BTN_PIN, INPUT); 
  Serial.begin(9600);
  Serial.println("geeek");
  Serial.flush();
}

void loop(){
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }

  static uint8_t lastBtnState = LOW;
  uint8_t state = digitalRead(BTN_PIN);
  if (state != lastBtnState) {
    lastBtnState = state;
    if (state == HIGH) {
        Serial.begin(9600);
        Serial.println("geeek");
    }
  }
  delay(100);
}

The message is well written to the console but no data is sent to the device.

Did I miss anything ?

Any help will be welcome :slight_smile:

Vincent

0 and 1 are the hardware Serial pins. Don't use those for your Software Serial

SoftwareSerial mySerial(0,1);

Did I miss anything ?

Yes.

You created an instance of the SoftwareSerial class, using the wrong pins, but you never called that instance's begin() method. You can't expect there to be data to read without having called begin() to specify the baud rate.