1

I have a problem , when send 1 or 0 by Bluetooth module the serial monitor print two time (RECEIVED SMS) . what's the wrong ?

This is my code :

 char BluetoothData = 0 ; 
void setup() {
 // initialize serial communication:
 Serial.begin(9600);
 for (int pin = 1; pin <= 13; pin++)
 {
 pinMode(pin, OUTPUT); // sets the digitals pin as outputs
 digitalWrite(pin, LOW); // sets the digitals pin off defult intial states 
 } 
 Serial.println("ALL LEDs NOW OFF \n "); 
 Serial.println("Please, Enter : ( 1 to Turn on All LEDs ) or ( 0 to Turn off LEDs ) \n");
}
void loop() { 
 // read the sensor:
 if (Serial.available() > 0) {
 BluetoothData = Serial.read();
 Serial.print("*** RECEIVED SMS *** : ==>> ");
 Serial.println(BluetoothData);
 if (BluetoothData == '1'){ 
 for (int Pin = 1; Pin <= 13; Pin++) {
 digitalWrite(Pin, HIGH);
 } 
 Serial.println("ALL LEDs NOW ON");
 }
 // turn all the LEDs off:
 else if (BluetoothData == '0') {
 for (int Pin = 1; Pin <= 13; Pin++) {
 digitalWrite(Pin, LOW);
 }
 Serial.println("ALL LEDs NOW OFF"); 
 }
 }
 delay(100); 
} 

This is photo for serial monitor

enter image description here

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked Mar 31, 2020 at 11:56
4
  • Please do not post photos of the output, rather select the text, then copy/paste the text into the question, as indented code Commented Apr 2, 2020 at 23:37
  • for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. Commented Apr 2, 2020 at 23:39
  • the posted code does not contain a main() function, not the necessary #include statements. So how are we to reproduce the problem? Commented Apr 2, 2020 at 23:41
  • I cannot understand all the functions and terms of the site. I weak in that, is there a video that explains the matter? Commented Apr 3, 2020 at 7:34

2 Answers 2

2

Probably the first time because you received the character '1' or '0'. The second character is probably a '0円' or '\d', an end-of-string or end-of-line character.

You can easily check this by printing the value of each received character, like

Serial.println((int)(BluetoothData));

You probably see the value for '0' (48), '1' (49), end of string '0' of end of line '13' or maybe '10' (new line).

answered Mar 31, 2020 at 12:57
1
  • you're welcome. Commented Mar 31, 2020 at 13:42
1

In the Arduino IDE you should select "No Line Ending" from the menu, instead of "New line". Otherwise you are sending an extra "\n".

answered Aug 31, 2021 at 15:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.