Turning on 2 leds with 2 buttons using processing and arduino (Bluetooth) - android

What I want to do is to control 2 leds in the arduino board with a processing program.
If I press any point on the upper half of the screen the led (Pin 13) will light on, and if I press a point on the lower half of the screen, it will turn on other led (Pin 12).
So, I programmed 2 buttons, with 2 leds(Pin 12 an 13) and no matter what button I press, It always turns on pin 13.
I made a separate experiment, with only 1 button, change only the pin 13 by 12. It does not work, always turns on pin 13.
ARDUINO CODE:
boolean estado;
boolean estado1;
byte a;
void setup()
{
Serial.begin(9600);
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
pinMode (13, OUTPUT);
digitalWrite (13, LOW);
randomSeed(analogRead(0));
estado = false;
estado1 = false;
}
void loop()
{
delay(100);
Serial.write(random(40));
while(Serial.available() > 0)
{
a = Serial.read();
if (a == 0)
{
estado = !estado;
digitalWrite(12, estado);
}
if (a == 1)
{
estado1 = !estado1;
digitalWrite(13, estado1);
}
}
}
PROCESSING CODE:
void compruebaBoton()
{
if( mouseY < 640)
{
try
{
ons.write(0);
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
}
if( mouseY > 640)
{
try
{
ons.write(1);
}
catch(Exception ex)
{
estado = 4;
error = ex.toString();
println(error);
}
}
}
the code I am implementing is correct for what I am trying to do?

After playing with the code for hours, I found the problem!!
The program is not loaded into the arduino if the bluetooth board is connected to the arduino board.

Related

MIT App Inventor 2 App notification triggered by an Arduino code action

I have an App that is, besides other things, supposed to alert when the Fire Alarm that I have on my project is triggered. The sensor is a thermistor and it activates a blinking led and a buzzer when the data it is getting is >700. I am using Bluetooth (HC-05) for my connection. I already have on the app a temperature sensor that is supposed to work independently and the readings from the Arduino are already being received, so I can´t mix the values (receive text number of bytes already being used). I was thinking of, e.g. when the led is blinking (meaning that the fire alarm is triggered), it sends some kind of info to the app that notifies me through an extension and the internal Notifier, without entering in conflict with the other blocks.`
visual aspect of app`
blocks of app
[code]
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11); //TX, RX respetively
float temp;
float seno;
int frequencia;
float temptermistor;
String state;// string to store incoming message from bluetooth
void setup() {
BT.begin(9600);// bluetooth serial communication will happen on pin 10 and 11
Serial.begin(9600); // serial com. to check the data on serial monitor
pinMode(A0, INPUT); //temperatura
pinMode(A1, INPUT); //termistor
pinMode(9, OUTPUT); //led alarme
pinMode(13, OUTPUT); //buzzer
}
void loop() {
temp = analogRead(A0);
delay(200);
temp = analogRead(A0);
temp = temp * 0.48828125;
temptermistor = analogRead(A1);
delay(10);
temptermistor = analogRead(A1);
delay(10);
Serial.println(temp);
BT.println(temp);
delay(250);
if (temptermistor >= 700)
{
digitalWrite(9, HIGH);
delay(50);
digitalWrite(9, LOW);
delay(50);
for(int x=0;x<180;x++){ //converte graus para rad. e obtém o valor do seno
seno=(sin(x*3.1416/180)); //gera uma frequência a partir do valor do seno
frequencia = 2000+(int(seno*1000));
tone(13,frequencia);
delay(2);
}
}
else
{
noTone(13);
}
while (BT.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = BT.read(); //Conduct a serial read
state += c; //build the string- either "On" or "off"
}
if (state.length() > 0) {
if(state == "A")
{
digitalWrite(2, HIGH);
}
else if(state == "a")
{
digitalWrite(2, LOW);
}
if(state == "B")
{
digitalWrite(3, HIGH);
}
else if(state == "b")
{
digitalWrite(3, LOW);
}
if(state == "C")
{
digitalWrite(4, HIGH);
}
else if(state == "c")
{
digitalWrite(4, LOW);
}
if(state == "D")
{
digitalWrite(5, HIGH);
}
else if(state == "d")
{
digitalWrite(5, LOW);
}
if(state == "E")
{
digitalWrite(6, HIGH);
}
else if(state == "e")
{
digitalWrite(6, LOW);
}
if(state == "F")
{
digitalWrite(7, HIGH);
}
else if(state == "f")
{
digitalWrite(7, LOW);
}
if(state == "G")
{
digitalWrite(8, HIGH);
}
else if(state == "g")
{
digitalWrite(8, LOW);
}
if(state == "H")
{
digitalWrite(12, HIGH);
}
else if(state == "h")
{
digitalWrite(12, LOW);
}
state ="";}
}
[/code]
For sending the Temperature value and notification message you can use delimiter as like & or some kind of symbol in the BT.println();.
Try something like this
BT.print(temp);
BT.print("&");
BT.println("notification");
When you see the output in Bluetooth terminal you can see output like this.
temp&notification
temp&notification
Then at the Mobile App capture that output and break it from the & symbol. then you can grab the two variables.
You can refer these tutorials.
1 2 3

Send data from Arduino to Android not working

We are doing a project where we move a servo motor to open and close a lock with Arduino and Bluetooth HC-06. We are trying to send one number (1 or 2) as the state of the servo motor before doing anything, as soon as we connect our app to the Bluetooth HC-06 just to know if the lock is already open or close (we are using EEPROM to not lose the last position of the servo). However, with our code we receive the data but the servo stop working well. If we delete the first two lines from the loop, it will work but we will not know the initial state of the servo. How can we solve this?
char state;
Servo myservo;
int btx=3;
int brx=2;
SoftwareSerial blue(btx,brx);
int pos = 0;
void setup() {
Serial.begin(9600);
myservo.attach(9);
blue.begin(9600);
// An EEPROM value of 1 == UNLOCKED and a value of 2 == LOCKED
if(EEPROM.read(0) == 1){ //Lock opened
myservo.write(70);
delay(200);
pos = 1;
//blue.println(1); not working
}
else if(EEPROM.read(0) == 2){ //Lock closed
myservo.write(180);
delay(200);
pos = 2;
//blue.println(2); not working
}
//blue.println(pos); not working
}
void loop() {
while(!blue.available()){ // <-- The problem is in this two lines
blue.println(pos); // send state to app
}
String voice;
while(blue.available()){
delay(10);
char c = blue.read();
if (c == '#'){
break;
}
voice += c;
}
if(voice.length() > 0){
if (voice == "open"){
myservo.write(70);
EEPROM.write(0, 1);
blue.println(1);
delay(15);
}
else if (voice == "close"){
myservo.write(180);
EEPROM.write(0, 2);
blue.println(2);
delay(15);
}
}
}
Because those two lines are an infinite loop! so you should change your code to this and give your code a chance to continue :)
void loop() {
if(!blue.available()){
blue.println(pos);
}else if(blue.available()){
String voice;
while(blue.available()){
delay(10);
char c = blue.read();
if (c == '#'){
break;
}
voice += c;
if(voice.length() > 0){
if (voice == "open"){
myservo.write(70);
EEPROM.write(0, 1);
blue.println(1);
delay(15);
}
else if (voice == "close"){
myservo.write(180);
EEPROM.write(0, 2);
blue.println(2);
delay(15);
}
}
}
}
}

Arduino LED blinking issue

So I have a Bluetooth module that I've created some code for, so that when i push button 1 it turns on the led, when i push 2 it turns off the led, and 3 is supposed to make the led continuously blink. Everything works perfectly fine, except that when i push 3 the led blinks once then stops. What can i do to the code to make it blink continuously until i either hit button 1 or 2 again?
char LED = 0;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // Opens Serial port
}
void loop() {
if (Serial.available()> 0){
LED = Serial.read();
Serial.print(LED);
if (LED == '1')
digitalWrite(13, HIGH);
if (LED == '0')
digitalWrite(13, LOW);
if (LED == '3') {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
}
}
You need to change main loop to:
void loop() {
if (Serial.available()> 0){
LED = Serial.read();
Serial.print(LED);
}
if (LED == '3') {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
else if (LED == '1')
digitalWrite(13, HIGH);
else if (LED == '0')
digitalWrite(13, LOW);
}
In this case LED == '3' will be work on every loop iteration, even if Serial is not contain any data
You put the blinking code into the if (Serial.available()> 0) block, which means that it's only run when you send a '3'. You would need to keep sending '3' to keep the LED blinking.
Sergey gave you a working solution, but that code unnecessarily keeps setting the LED on or off, and the code is unresponsive while the LED is blinking because of the use of delay.
To fix those problems you need to use a variable to keep a state (In Sergey's code that role took your existing LED variable) and use millis() instead of delay() for blinking, like in the BlinkWithoutDelay builtin Arduino IDE example.
char LED = 0;
enum {
NONE,
BLINKING,
} state = NONE;
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
Serial.begin(9600); // Opens Serial port
}
void loop() {
if (Serial.available() > 0){
LED = Serial.read();
Serial.print(LED);
switch (LED) {
case '1':
state = NONE;
digitalWrite(BUILTIN_LED, HIGH);
break;
case '2':
state = NONE;
digitalWrite(BUILTIN_LED, LOW);
break;
case '3':
state = BLINKING;
break;
}
}
switch (state) {
case NONE: break;
case BLINKING:
static unsigned long last_blink = 0;
if (millis() - last_blink > 1000)
{
digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED));
last_blink = millis();
}
break;
}
}

Send data from Arduino to Android (AppInventor2) via HC-06 bluetooth

I am trying to send the number "25" from arduino to android application created by using MIT AppInventor2. At the same time, I want to make "on" or "off" a LED by clicking "lock" and "unlock" buttons on the android app. There is no problem making LED status "on" and "off" but I can not read the 25 on the application. I only read 2 or 5. I read 25 very very rarely.
What is the problem about my arduino code or AppInventor blocks?
Arduino code:
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
Serial.println(25);// Here I want to send 25
delay(20);
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
// Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
Edit: Time interval for AppInventor clock is 1000.

arduino to android bluetooth: detecting if both are connected

The Goal:
To have the ardiuno check if it's connected to the android with bluetooth. Then to perform an act if it is connected or reconnect if it's not connected.
What I am using:
Bluesmirf silver with arduino uno and note 3
What I've done so far:
[ARDUINO CODE]
The Bluesmirf is in master mode auto connect. The arduino is supposed to check if the android app is sending an H character. If it is that means its connected. If not then it needs to keep re-connecting.
#include <SoftwareSerial.h>
#include <TextFinder.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
boolean running = false;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
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
delay(100);
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
//Check If Connected
if(bluetooth.available()) // If the bluetooth sent any characters
{
//Check if bluetooth recieved an H and store in a value
char val = bluetooth.read();
if(val == 'H')
{
running = true;
}
else if(val != 'H')
{
running = false;
}
}
else if(!bluetooth.available())
{
running = false;
}
//Actions to perform if arduino is connected or not connected
if(running == true)
{
//It's connected so wait 5 seconds
delay(5000);
}
else if(running == false)
{
//It's not connected: Attempt to reconnect
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("C,30196692D7C0");
delay(500);
bluetooth.println("---");
delay(3000);
}
}
[ANDROID CODE]
And this is the method of the android app that sends an H once the app is connected.
private void sendMessage(BluetoothSocket socket, String msg) {
OutputStream outStream;
try {
outStream = socket.getOutputStream();
byte[] byteString = (msg).getBytes();
outStream.write(byteString);
} catch (IOException e) {
Log.d("BLUETOOTH_COMMS", e.getMessage());
}
}
Side Note:
I've tried so many things to get this arduino to check if its connected or not. I only just started programming 3 weeks ago so this is becoming increasingly difficult. Any help would be appreciated.
[UPDATE #1]
I've managed to send an 'h' with the android app with this snippet here:
//call send method to send this character over bluetooth
sendMessage(socket,"h");
//Method used to send 'h' over bluetooth
private void sendMessage(BluetoothSocket socket, String msg) {
OutputStream outStream;
try {
outStream = socket.getOutputStream();
//byte[] byteString = (msg).getBytes();
byte[] byteString = stringToBytesUTFCustom(msg);
outStream.write(byteString);
} catch (IOException e) {
Log.d("BLUETOOTH_COMMS", e.getMessage());
}
}
//Method used to convert
public byte[] stringToBytesUTFCustom(String str) {
char[] buffer = str.toCharArray();
byte[] b = new byte[buffer.length << 1];
for (int i = 0; i < buffer.length; i++) {
int bpos = i << 1;
b[bpos] = (byte) ((buffer[i]&0xFF00)>>8);
b[bpos + 1] = (byte) (buffer[i]&0x00FF);
}
return b;
}
And with arduino I can properly read the 'h' using this snippet.
if (bluetooth.available() > 0) { // if the data came
char incomingByte = bluetooth.read(); // read byte
if(incomingByte == 'h') {
running = true;
}
}
New Problem
I am having trouble telling when the arduino has lost connection with the android app.

Categories

Resources