Serial Monitor vs Serial Code

Hi everyone,

I've been working on a project using serial commands with an Arduino Nano and Bluetooth Module. I've been able to connect with my secondary Bluetooth device and send the commands I want but only in Serial Monitor. When I put the same commands in the Arduino code I don't see any of the commands. Is there a difference between the way Serial Monitor sends commands and the way serial.write or serial.print sends commands?

Let's see your code.

What do you have the line ending set to in the Serial monitor ?

I don't know if I understand you correctly.

However, serial.write() sends binary data to the serial port, while serial.print (or serial.println) writes ASCII text.

They are both used to send data to another device through the serial port. If you want to send data from your device to Arduino, you must use serial.read() into the loop.

Just to know, since you are talking about it, with Serial.read(); if a write something like "11" i get the two AASCII value of the number "1" for two times that is 49. How could i receive a two digits number? Maybe should i use an array?

There is a problem with your code and/or wiring (connecting things to the serial port is a little tricky, because those pins are also connected to the onboard USB serial adapter for programming the board.

If you want us to be able to help effectively, you should post your code and wiring.

I've provided my code below. I saw that someone said there's a difference between serial.write() and serial.print(). Should I be using serial.write() to mimic the serial monitor? For the record the commands have to be sent with a carriage return, and the CR&NL works in Serial Monitor. That's why I was trying to use serial.println().

const int hornButton = 2;   //Sets button location on Arduino
const int travelButton = 3; //^
const int stopButton = 4;   //^
const int stopLED = 13;     //Sets optional LED location
const int travelLED = 12;   //^
const int hornLED = 11;     //^

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);         //Sets serial BAUD rate
pinMode(hornButton, INPUT);   
pinMode(travelButton, INPUT);
pinMode(stopButton, INPUT);

}
const char stopCmd[] = "t400753000000000000";         //stop command
const char travelCmd[] = "t400754050000000000";     //travel command
const char hornCmd[] = "t400748000000000000";         //horn command
void loop() {
  // put your main code here, to run repeatedly:

if(digitalRead(stopButton)==HIGH){  
  Serial.println("O");          //Opens CAN
  Serial.println(stopCmd);
  digitalWrite(stopLED, HIGH);
}else if(digitalRead(travelButton)==HIGH){
  Serial.println(travelCmd);
  digitalWrite(travelLED, HIGH);
}
if(digitalRead(hornButton)==HIGH){
  Serial.println(hornCmd);
  digitalWrite(hornLED, HIGH);
}
else{
  digitalWrite(stopLED, LOW);
  digitalWrite(travelLED, LOW);
  digitalWrite(hornLED, LOW);
}
delay(500);
}

For testing can you use SoftwareSerial to create another Serial connection for your Bluetooth module (at 9600 baud) - that way you are keeping the HardwareSerial port available for debug messages.

I am not clear what you mean when you say you are sending stuff with the Serial Monitor - what are you sending them to?

As @DrAzzy said, make a couple fo pencil drawings showing the different wiring connections you are using and post photos of the drawings.

...R

I'm trying to connect an arduino & BT module to a BT to CAN device. The arduino with BT module just sends three button commands to the BT to CAN device. I'm using it to move a robot forward, stop it, and sound a horn. As far as I know, I can only use one port on the BT module for serial transmission. I have the MAC address for the BT module bound to the BT to CAN. I was switching between connecting it to the Arduino to run the code and connecting it straight to my computer to send the same commands through Serial Monitor.

I still don't have a clear picture of what is connected to what. For example I have no idea what a "BT to CAN device" is. And you have not explained what you mean by "send the commands I want but only in Serial Monitor"

Please provide the drawings that have been requested.

Also, if you can provide a simple diagram of the whole system it will probably explain it more clearly than words.

...R