Hi Friends,
So, I'm doing a project where I need to send a byte from the MIT AI2 to the Arduino and send a byte back using Bluetooth depending on the situation of a magnetic switch. I'm using an Arduino Uno, an HC-05, and a Magnetic field switch to determine which byte is sent. I can connect to the HC-05; however, I am not receiving the byte in the Arduino. Can someone please send suggestions? It says I cannot upload attachments because I'm a new user, so I will copy-paste my code below.
Thank you♥
#include <SoftwareSerial.h>
// Declarations
byte cmd = 0; // Stores the next byte of incoming data, which is a "command" to do something
byte closed = 0; // Stores the 2nd byte, which is the command parameter
byte opened = 1;
int input = 0;
int x = 100;
SoftwareSerial bluetooth (10, 9);
// Initialization
void setup() {
bluetooth.begin(9600); // start bluetooth communication at 9600bps
Serial.begin(9600);
}
// Arduino Execution Loop
void loop() {
if ( bluetooth.available() ) // if data is available to read
{
cmd = bluetooth.read(); // read it and store it in 'cmd'
input = (analogRead(A0)) / x;
Serial.println(input);
}
if (cmd == 1)
{
if (input > 5) {
bluetooth.write(closed);
}
else {
bluetooth.write(opened);
}
}
delay(5000);
};
Hello, as a new user, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).
your loop is always testing cmd regardless of the fact it was just received or not. Is that intended? (the if are not nested)
Ohhh, ok, I read the post and I will do the copy-paste thing correctly this time. Also, that was not intended. So, part two of this story is that my project mentor and I agreed that we only needed one byte to go back and forth because we are both pretty new at this, and he just now sprung it on me that I, in fact, need two bytes to make it work, and I don't know how to do that. So, I would like to know if it is possible to do just one byte communicating between an HC-05 and an MIT AI2 android app through Bluetooth. I'm so sorry this is so vague I don't even know how to begin to describe how much I do not understand about this project. I'm so sorry.
[code]
/*
BTArduino sketch
By Edward Mitchell
http://appinventor.pevest.com
Description:
Uses the JY-MCU Bluetooth module to enable an Arduino board to communicate over Bluetooth
wireless to an Android app written in MIT App Inventor 2, running on an Android device.
Credit:
This Arduino code based very loosely on the original Arduino code for the JY-MCU Bluetooth module tutorial at
http://robotosh.blogspot.com/2012/07/arduino-jy-mcu-bluetooth.html
A tutorial for using this code with App Inventor 2, is available at the appinventor.pevest.com
web site. Please refer to the tutorial for information and source code for the Android side of this sketch.
*/
#include <SoftwareSerial.h>
// Declarations
byte cmd = 0; // Stores the next byte of incoming data, which is a "command" to do something
byte closed = 0; // Stores the 2nd byte, which is the command parameter
byte opened = 1;
int input = 0;
int x = 100;
SoftwareSerial bluetooth (10, 9);
// Initialization
void setup() {
bluetooth.begin(9600); // start bluetooth communication at 9600bps
Serial.begin(9600);
}
// Arduino Execution Loop
void loop() {
if ( bluetooth.available() ) // if data is available to read
{
cmd = bluetooth.read(); // read it and store it in 'cmd'
input = (analogRead(A0)) / x;
Serial.println(input);
}
if (cmd == 1)
{
if (input > 5) {
bluetooth.write(closed);
}
else {
bluetooth.write(opened);
}
}
delay(5000);
};
[/code]
I would suggest to study Serial Input Basics to understand more about handling a serial communication
If you can do with 1 byte, I would nevertheless have nested if so that the code says
If there is something pending read it and if it’s X do this otherwise do that
At the moment your code loops and even when nothing is received you send something back
Having a 5s delay is probably a bad idea… when your code is stuck there waiting and doing nothing, you’ll miss any incoming command
Just to test try using the arduino remote from play store. And try writing the code as if you are using the serial monitor to communicate. Keep the hco5 wired up.
I suggest that you separate your Arduino code issues from any App Inventor issues. I would use a standard terminal app like Kai Morich's Serial Bluetooth Terminal.
I would also send text characters instead of bytes a it will make things easier to debug with Serial output.