Teensy 3.1 + Serial Bluetooth 4.0 BLE Module - iBeacon

Status
Not open for further replies.

Filippo

Member
Hi,

I want to write and read single bytes with this BLE-modules.
The "void read" is ok, the "void write" not. What am I doing wrong?
Do I need to add in addition to "Serial2.print (7)" something?

Here is my code:
Code:
void write() {
  if (button0.fallingEdge()) {
    Serial2.print(7);
  }
  if (button1.fallingEdge()) {
    Serial2.print(6);
  }
}

void read() {
  if (Serial2.available()) {
    int c = Serial2.read();

    if( c == 1 )               // if 'H' was received
    {
      digitalWrite(ledpin, HIGH);  // turn ON the LED
      millisec = 0;
    }
  }
}
 
Hi,

can someone confirm to me at least that my code is correct, so I'm supposed to find the error somewhere else?
 
First, let's assume "button0" and "button2" are Bounce objects. You didn't post the entire program (see the "forum rule"), so to give you helpful advise, I have to guess this part. If those are actually something else, ignore the rest of this message about Bounce.

The Bounce library requires its update() function to be called, to actually read the pin and cause fallingEdge() to be able to return true. I don't see any button0.update() and button2.update() calls in your code.

Perhaps those missing update calls are in some other part of your program? You didn't post a complete program, so it's hard to know (again, see the "forum rule" - which is meant to help you get the best help I and others can give).

The other thing I don't see is any Serial.print("something") lines. Printing to Serial will let you view info in the Arduino Serial Monitor, which can show you more info about what your program is actually doing. Often just a few well placed Serial.print() messages can suddenly make a difficult program much clearer.
 
Serial2.write() not Serial2.print()?? Are you trying to send something back through the bluetooth or trying to see something printed to the serial monitor? If you provide the code you are using and a little more explanation it will help. If you are trying to print to the monitor, as paul says, it should be Serial.print(whatever), but you most likely have to look at Serial2.write(whatever) rather than serial.print() as part of your sending through the module. ensure that you are on the correct Rx Tx pair for the Serial number, also ensure that Rx goes to TX and Tx to Rx between the board and the module.

If you want to both read the byte value on the monitor and send it out through the module you can put both lines in, something along the lines of:
Serial.println(7);
Serial2.write(7);
 
Last edited:
Hi PaulStoffregen,
Hi mortonkopf,

Thanks for the reply.

Send to monitor works fine.

If you want to both read the byte value on the monitor and send it out through the module you can put both lines in, something along the lines of:
Serial.println(7);
Serial2.write(7);
The've also tried, unfortunately without success.

Here is my complete code:
Code:
// Programm für Teensy 3.0 und Teensy 3.1

#include <Bounce.h>

const int pbutton = 7;
const int sensor = 6;
const int ledpin = 13; // 2;

Bounce button0 = Bounce(pbutton, 10);
Bounce button1 = Bounce(sensor, 10);  // 10 = 10 ms debounce time

unsigned long startTime = millis();
unsigned long millisec = 0;

void setup() {
  Serial2.begin(9600);
  pinMode(ledpin, OUTPUT);
  pinMode(pbutton,INPUT_PULLUP);
  pinMode(sensor,INPUT_PULLUP);
}

void loop() {
  button0.update();
  button1.update();
  
  read();
  write();
}

void write() {

  if (button0.fallingEdge()) {
    Serial2.print(7, DEC); //(7);
    Serial.println(7, DEC); //("0");
  }
  if (button1.fallingEdge()) {
    Serial2.print(6, DEC); //(6);
    Serial.println(6, DEC); //("+");
  }
}

void read() {
  if (Serial2.available()) {
    int c = Serial2.read();
    //Serial.println(c, DEC);
    if (c == 2) {
       //tone(12, 1000, 200); 
       tone(12, 2000, 50); 
    }
    if (c == 5) {
       //tone(12, 1200, 400); 
       tone(12, 2000, 300); 
    }
    if( c == 1 )               // if 'H' was received
    {
      digitalWrite(ledpin, HIGH);  // turn ON the LED
      millisec = 0;
    }
  }

  if ( (millis()-startTime)>50 ) {
      startTime = millis();
      millisec = millisec + 50;
  } 
  if ( millisec > 1000 ){
    digitalWrite(ledpin, LOW);   // otherwise turn it OFF
  }
}

tennsy_1.JPG
 
@Filippo, you code above still has the write action expressed as Serial2.print(7, DEC); //(7);

it might be worth changing that to Serial2.write…

I had a quick play with an alternative sketch as I could not get yours sending the 7 back to my phone. I ran some different code and managed to get text on way and the button to send the 7 back to my phone. Its really not good code by it might help ny way of thinking where the issue might lay.

Code:
// Programm für Teensy 3.0 und Teensy 3.1

#include <Bounce.h>

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

////////////////////
const int pbutton = 2;
const int sensor = 6;
const int ledpin = 13;

Bounce button0 = Bounce(pbutton, 10);
Bounce button1 = Bounce(sensor, 10);  // 10 = 10 ms debounce time

unsigned long startTime = millis();
unsigned long millisec = 0;

void setup() {  
  Serial2.begin(9600);
  pinMode(ledpin, OUTPUT);
  pinMode(pbutton,INPUT);
  pinMode(sensor,INPUT);
}

void loop() {
  
  readBT();
  
  int reading = digitalRead(pbutton);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
       Serial2.write("7");
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    buttonState = reading;
    }
 
  digitalWrite(ledpin, buttonState);
  lastButtonState = reading;
}
//////

void readBT() {
  while (Serial2.available()>0) {
    int c = Serial2.read();
    Serial.println(c, DEC); 
  }
}
 
Hi mortonkopf,

Thank you for your code, but unfortunately does not work, I get no byte on my iphone.
But I do not understand why it work only in one direction. :(

In principle, if it works in one direction then it should work in the other direction too, right?
Otherwise have the error in my Iphone software be.
 
I tested the code using HC-07 module and an android bluetooth app to send and receive the data to appear in text boxes on the screen. Did you give your app the correct permissions for receiving? I really have no idea on the iPhone permissions required, so this is a part that I can't help with.
 
Hi mortonkopf,

I also use HC-07 for Android with the same Ardiuno code without problems.
Unfortunately HC-07 does not work for iPhone. :(

Did you give your app the correct permissions for receiving? I really have no idea on the iPhone permissions required, so this is a part that I can't help with.
I think if I can send then I should be able to also receive.
 
Glad to hear the code works to send and receive to a hand set, so the issue is narrowed down to the setup of the iPhone. Which is why I no longer use iPhone or apple for any dev work. But thats another discussion thread…

Not much more from me on this, other than dig through the restrictions on incoming bluetooth requests?
 
Not much more from me on this, other than dig through the restrictions on incoming bluetooth requests?
With the iPhone, I also know how from too little.

I just wanted to hear my Ardiuno code is correct, now I know that you have to look for the error on iPhone.
 
Status
Not open for further replies.
Back
Top