Bluetooth LED not turning on

So right now I’m trying to control a LED via Bluetooth. I’m using an Arduino Mega, HC 05 module, a standard LED, and the MIT App inventor. The issue I have is that although I can connect, I can’t turn on or off the LED and I’m not sure why. Code is below, and I guess I attatched a photo of the code blocks. Cant upload .apk

#include <SoftwareSerial.h>

SoftwareSerial Bluetooth(1,0); // Arduino(RX, TX) Bluetooth(TX, RX)

int dataIn;
int ledPin = 6; 

void setup() {
  Serial.begin(9600);
  Bluetooth.begin(9600);
  pinMode (ledPin, OUTPUT); 
  digitalWrite(ledPin, HIGH);
}

void loop() {
  
  if(Bluetooth.available() ) {
    dataIn = Bluetooth.read(); 
  }
  if (dataIn== '0') {
    digitalWrite(ledPin, LOW);
  }
  else if (dataIn== '1'){
    digitalWrite(ledPin, HIGH);
  }
}

Put a serial print of the value of dataIn after the read. What is coming in, if anything?

For starters, you are using software serial on the hardware serial pins 0,1, which is a prettty sure way to disaster. Either get rid of the software serial and use the hardware serial, which seems otherwise unemployed, or move the software serial to another pair of pins.

No comment on Appinventor, except to leave it out of the game for the moment. Use a standard terminal.

You might find the following background notes useful.

I can't turn on or off the LED

It should already be on.... is it?

I'd debug that by losing Bluetooth to start, and checking the led turns off and on by sending a 0 or 1 over Serial from the monitor. Does the led actually come on to start, since it should with the high in setup()? No point checking BT if the led's not working in the first place, and not switching over Serial.

Then if that works, use a simple BT terminal to send the 0 and 1, rather than AppInventor as suggested.

Only then if it's working so far, throw AppInventor into the mix.

Further to my last, and in the light of reply #2, indeed leave Bluetooth on pins 0,1 and get rid of software serial altogether, thereby allowing you to prove what you doing with the serial monitor, as jubukraa suggests.

Off topic, but have to comment on my one and only attempt to use AppInventor a while back.... "Eeeewwwwww".

I'm not sure what "Eeeewwwwww" means, but it all looks very pretty - and a hell of a long way from ForTran.

and a hell of a long way from ForTran.

That's where I started too (WatFor), punched cards as an undergrad engineer....

Well there you go, and I thought I was the old fart around here. I have clipped your diagram as I assume it works, but I probably won't ever use it, as I suspect the ultimate bluetooth APP has already been INVented by some guy in Istanbul, with Bluetooth Graphics Terminal.

It's OP's diagram

Hello nongateways.

I think the test your sketch uses does not align with the data type being sent by the app.

The app block responding to a press of the LEDON button sends a one byte integer of value 1. That would be the bit pattern of 00000001. This is not the ASCII character "1", which is bit pattern 0011 0001 (decimal 49).

The sketch test is:

else if (dataIn== '1'){
digitalWrite(ledPin, HIGH);
}

I think this is testing if the value of dataIn is equal to that of the character '1' which is not the same as the numerical value of 1.

Try:

if (dataIn == 1)

Also, since you are sending just one byte, how about defining dataIn as type byte?

Alternatively, you could change the block to send the text "1" and modify the sketch to expect an incoming character, or send "LEDON" and have the sketch receive a string.

Nick knows Bluetooth and serial. Software serial should not use pins 0 & 1.

John.

For starters, you are using software serial on the hardware serial pins 0,1, which is a prettty sure way to disaster. Either get rid of the software serial and use the hardware serial, which seems otherwise unemployed, or move the software serial to another pair of pins.

OP is trying to use the same pins for hardware and software serial. Whether he/she actually ever does anything with the Serial instance is irrelevant.

Why OP is using SoftwareSerial on a device with 3 unused hardware serial ports is the real mystery.

Nick_Pyner:
For starters, you are using software serial on the hardware serial pins 0,1, which is a prettty sure way to disaster. Either get rid of the software serial and use the hardware serial, which seems otherwise unemployed, or move the software serial to another pair of pins.

No comment on Appinventor, except to leave it out of the game for the moment. Use a standard terminal.

You might find the following background notes useful.

Dropbox - GUIDE_2BT.pdf - Simplify your life
Dropbox - BT_2_WAY.ino - Simplify your life

Which pins would be the appropriate pins for software serial? I thought since serial pins were used for communication 0, 1 could have been used. I guess I switched over to 18 and 19?

jubukraa:
It should already be on.... is it?

I'd debug that by losing Bluetooth to start, and checking the led turns off and on by sending a 0 or 1 over Serial from the monitor. Does the led actually come on to start, since it should with the high in setup()? No point checking BT if the led's not working in the first place, and not switching over Serial.

Then if that works, use a simple BT terminal to send the 0 and 1, rather than AppInventor as suggested.

Only then if it's working so far, throw AppInventor into the mix.

Ah, I forgot that I uploaded a different version. Yes, the LED is on with that particular sketch, I was just messing with the HIGH and LOW states and forgot to adjust properly.

What do you mean by a BT terminal? I looked it up and it seemed to a bluetooth app, but nothing is really clear as to how to use it?

EDIT: So I removed the quotation marks from if (dataIn==0), and now the LED won't turn on. I am guessing there wasn't supposed to be any quotation marks to begin with since that would be associated with a char?

HillmanImp:
Hello nongateways.

I think the test your sketch uses does not align with the data type being sent by the app.

The app block responding to a press of the LEDON button sends a one byte integer of value 1. That would be the bit pattern of 00000001. This is not the ASCII character "1", which is bit pattern 0011 0001 (decimal 49).

The sketch test is:

else if (dataIn== '1'){
digitalWrite(ledPin, HIGH);
}

I think this is testing if the value of dataIn is equal to that of the character '1' which is not the same as the numerical value of 1.

Try:

if (dataIn == 1)

Also, since you are sending just one byte, how about defining dataIn as type byte?

Alternatively, you could change the block to send the text "1" and modify the sketch to expect an incoming character, or send "LEDON" and have the sketch receive a string.

Nick knows Bluetooth and serial. Software serial should not use pins 0 & 1.

John.

Hmm so several interesting things happen here. When I do what you suggested (changed "int dataIn" to "byte dataIn", and removed the quotation marks, it still doesn't work, and the LED does not turn on. In the sketch, I have the LED turned on in my setup, but once I make those changes the LED isn't on. I'm currently working on using characters like you said now.

Thanks

groundFungus:
Put a serial print of the value of dataIn after the read. What is coming in, if anything?

I am being spammed with “LED OFF”. I also removed the quotation marks from

 if (dataIn== "0") {
    digitalWrite(ledPin, LOW);

since they weren’t supposed to be there anyways? Below is the code with the serial print

#include <SoftwareSerial.h>

SoftwareSerial Bluetooth(1,0); // Arduino(RX, TX) Bluetooth(TX, RX)

int dataIn;
int ledPin = 6;

void setup() {
  Serial.begin(9600);
  Bluetooth.begin(9600);
  pinMode (ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
}

void loop() {
  
  if(Bluetooth.available() ) {
    dataIn = Bluetooth.read();
    Serial.print("Connected");
  }
  if (dataIn== 0) {
    digitalWrite(ledPin, LOW);
    Serial.print("LED OFF");
  }
  else if (dataIn== 1){
    digitalWrite(ledPin, HIGH);
        Serial.print("LED ON");

  }
}
SoftwareSerial Bluetooth(1,0); // Arduino(RX, TX) Bluetooth(TX, RX)

int dataIn;
int ledPin = 6;

void setup() {
  Serial.begin(9600);
  Bluetooth.begin(9600);

You can NOT so software serial on the hardware serial pins while doing hardware serial on the same pins.

Since you have a Mega with 4 hardware serial ports, GET RID OF SoftwareSerial altogether.

Yeah I did that and it worked thanks. I am curious, so if I were to use SoftwareSerial, I would need to use pins 12-19 then?

so if I were to use SoftwareSerial, I would need to

have your head examined.

Look at the SoftwareSerial documentation for the pins you can use on the Mega (19 is a hardware serial pin). But, in your case, you should simply NOT be using SoftwareSerial.

I copied your sketch and app. It did not work with "1" and "0". It does work with 1 and 0, that is, "if (dataIN == 1)".

It works for dataIn declared as either int or byte type.

I've used sw serial on pins 3 & 4 (a non-genuine Nano) and instead of an external LED I used pin 13 which drives the on board LED.

If you're getting spammed with "LED OFF" that's simply cos the code is making that happen. If you carefully follow the logic of the code you'll work it out.

I suppose you realise that the app messages "LED ON" and "LED OFF" don't really mean what they say. They're just telling you what button was last pressed -- the app doesn't know if the LED has in fact being turned on or off.

John.