Hello, I am new to arduino and I am trying to make a 3x4 Keypad for Android Phones. I tried the sample code for SoftwareSerial which is
/*
Software serial multple serial test
Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.
The circuit:
* RX is digital pin 2 (connect to TX of other device)
* TX is digital pin 3 (connect to RX of other device)
created back in the mists of time
modified 9 Apr 2012
by Tom Igoe
based on Mikal Hart's example
This example code is in the public domain.
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
//int inputSwitch = 7;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(38400);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(38400);
mySerial.println("Hello, world?");
//set input
// pinMode(inputSwitch, INPUT);
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
/* if(digitalRead(inputSwitch)== LOW){
Serial.println("Alryt!");
}*/
}
I adjusted the baud rate to 38400 from the default 57600
The code is supposed to communicate with the android phones through a Bluetooth terminal(I used an app called Bluetooth SPP).
It is supposed to have the ability to send or receive texts or characters from the phone or vice versa. It was able to that but the characters sent from each other where unrecognised. This is the link of the image of the output on the Serial Monitor https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-ash4/481442_2569654298674_1193692205_n.jpg
The read() methods of HardwareSerial and SoftwareSerial returns an int. You are then sending that int as binary data to the Serial Monitor which expects text. You are then surprised when the data is "misinterpretted"?
Im sorry that was from the other code that I was trying here is code that I have used
/*
Software serial multple serial test
Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.
The circuit:
* RX is digital pin 2 (connect to TX of other device)
* TX is digital pin 3 (connect to RX of other device)
created back in the mists of time
modified 9 Apr 2012
by Tom Igoe
based on Mikal Hart's example
This example code is in the public domain.
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
Im sorry that was from the other code that I was trying here is code that I have used
And how is that different? The read() methods still return ints, and you are still sending them as binary data (that's what write() does, and how it differs from print()).
I've already made (a subtle) suggestion. The Serial.available() method (as well as the SoftwareSerial.available() method) determines if there is anything to read. If the calls to the read() methods always happen inside if(Serial.available()) blocks, the value returned by Serial.read() (and SoftwareSerial::read()) can be stored in a char.
DO THAT.
Then use the print() methods NOT the write() methods to output the data.
I replaced all the read() to print all the sent data from the android where shown as numbers while the data send from the arduino are still diamonds with question mark...
I replaced all the read() to print all the sent data from the android where shown as numbers while the data send from the arduino are still diamonds with question mark...
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
//int inputSwitch = 7;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(38400);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(38400);
mySerial.println("Hello, world?");
//set input
// pinMode(inputSwitch, INPUT);
}
void loop() // run over and over
{
if (mySerial.available())
Serial.print(mySerial.read());
if (Serial.available())
mySerial.print(Serial.read());
}
kelvs24:
Thanks! I told you that I am still new to this stuff. But the data that was sent from the android was these "?xþxþxàxþø" while I have input "Hello"
kelvs24:
Got It! The Baud Rate is 9600. Is it possible that android phones have different baud rate?
Is there a physical connection between the Android and Arduino, or is it communicating through Bluetooth? If the latter, the baud rate of the Android doesn't matter, it's whatever the Bluetooth Module is set to.
kelvs24:
Got It! The Baud Rate is 9600. Is it possible that android phones have different baud rate?
Is there a physical connection between the Android and Arduino, or is it communicating through Bluetooth? If the latter, the baud rate of the Android doesn't matter, it's whatever the Bluetooth Module is set to.