I am attempting to send some information from Matlab, to an Arduino Uno, via bluetooth.
I only need to send an 'up' or a 'down' signal. A yes or no, a 1 or 2, basically two different signals to perform two different tasks once they're received.
I am using a BlueSMiRF Gold as the bluetooth modem.
Here is my Arduino Script:
byte incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
}
And Matlab Script is:
%% Send Bluetooth Signal
ardBT=serial('COM5','BaudRate',9600); % create serial communication object
fopen(ardBT); % initiate bluetooth communication
fprintf(ardBT,1);
pause(1);
fclose(ardBT); % Close bluetooth communication
The pause in the Matlab script is so that I can see the green light come on on the BlueSMiRF modem showing that it is connected.
When I upload the arduino script and run the matlab one, I check the serial packet received and it is usually '255' but some times other values around 255 (but not higher). Weird bit is that I can send the same information, but the Serial Monitor prints changing values.
Can someone help? How do I get the Arduino to read the values I actually send via bluetooth?
Eg: I want to send "1" and have the Serial.read be "1" and then an action to occur if and only if a "1" is received.
Need to have the RX and TX pins of the bluetooth modem plugged into pins 3 and 2 on the arduino uno respectively during upload. Once the upload is done, then move the pins to the TX and RX pins of the arduino (remember TX->RX and RX->TX).
The arduino code is:
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
byte incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
}
void loop() {
if (Serial.available()>0){
incomingByte=Serial.read();
if (incomingByte==1){
Serial.println(incomingByte);
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
}
if (incomingByte==2){
Serial.println(incomingByte);
digitalWrite(13,HIGH);
delay(200);
digitalWrite(13,LOW);
delay(200);
digitalWrite(13,HIGH);
delay(200);
digitalWrite(13,LOW);
}
}
}
This script blinks once if a "1" is received and twice if a "2" is received and does nothing for any other number. It can receive any number from 0 to 255.
Then connect on Matlab using:
b=Bluetooth('name',1);
fopen(b);
where 'name' is the name of your bluetooth modem.
and write to the arduino using:
fwrite(b,1);
in this example a "1" was sent. Then remove the connection using:
fclose(b);
delete (b);