Go Down

Topic: Interrupt Routines within Bluetooth (Read 889 times) previous topic - next topic

Wilson99

Hello,

I am using a Seeed Bluetooth shield (http://www.seeedstudio.com/wiki/Bluetooth_Shield) on my Arduino Due.  So far I have had success connecting to a PC and sending data from the Arduino in my main program loop.  However, for the project I am working on, I need to send data at a 1kHz rate.  I have done this by setting up a timer compare interrupt to fire every millisecond.

My problem is that when I upload the sketch, the Bluetooth shield has to establish a connection first and I think the interrupt is messing up the system somehow.  The shield has some indicator lights that flash for different statuses (Alternating red and green LED blinking = device inquirable, 2 flashes of green LED/second = Idle state, 1 flash green LED/sec = Active state).  When I upload the sketch, the shield will only blink for about 4 seconds until the lights turn off completely and do nothing from then on out.  I have tried to open the COM port I am using but it won't open (the Arduino isn't connected I am assuming).   

In the setup loop there is a "setupBlueToothConnection" function for the shield.  I disabled global interrupts so the interrupt wouldn't get in the way, but haven't had any luck.

I wrote another sketch using the same interrupt to send data to the PC over a USB serial connection and the program works fine. 

I was hoping you guys would have some ideas of what I could do to fix this issue.

Cheers!

ookid

You are going to want to post your code so that we can read over it.

Wilson99

This is the basic task I want to do.  A timer interrupt occurs at a ~1k rate where it reads the pin and outputs the reading.  I have been able to do this over the USB serial connection so I know the logic works.  It is when I try to use my Bluetooth shield that I have issues.

Thanks!


Code: [Select]
#include <SoftwareSerial.h>   //Software Serial Port
#define RxD 6
#define TxD 7

#define DEBUG_ENABLED  1

SoftwareSerial blueToothSerial(RxD,TxD);

#include "avr/pgmspace.h"
#include <avr/io.h>
#include <avr/interrupt.h>

int sensorVoltage = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(2, OUTPUT);   //Notch Filter
  pinMode(3, OUTPUT);   //Pre Charge
  pinMode(4, INPUT);    //Gain
  pinMode(RxD, INPUT);  //Receiver
  pinMode(TxD, OUTPUT); //Transmitter
  setupBlueToothConnection();
 

  /* initialize Timer1 */
  cli(); /* disable global interrupts */
  TCCR1A = 0; /* set entire TCCR1A register to 0 */
  TCCR1B = 0; /* same for TCCR1B */
  /* set compare match register to desired timer count: */
  OCR1A = 15999; /* with a 16 MHz clock, this will give about 1KHz interrupt */
  /* turn on CTC mode: */
  TCCR1B |= (1 << WGM12);
  TCCR1B |= (1 << CS10);  /* Set CS10 bit for 1023 no prescaler: */
  TIMSK1 |= (1 << OCIE1A);
  sei(); /* enable global interrupts: */
}

ISR(TIMER1_COMPA_vect)
{
     sensorVoltage = analogRead(A0);
     blueToothSerial.println(sensorVoltage);
     
     blueToothSerial.flush()
}

void loop()
{
  while(1)
  {
    //DO NOTHING
    delay(1000);

  }
}

void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

Riva

A quick look at the link you supplied and it shows a switch that allows the BT modules connect status to be read on A1
Quote
PIO1    Status instruction port of Bluetooth module can be read by Arduino A1 port: low-disconnected, high-connected.

In setup before doing the interrupt setup code wait for a connection.
You should not be doing serial print or flush within an ISR.
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

Wilson99

Quote
In setup before doing the interrupt setup code wait for a connection.

Do I do this by making the setupBluetoothConnection() return a value and not starting the timer initialization until that setup is complete?

Quote
You should not be doing serial print or flush within an ISR.

How else besides ISRs could I make the data print at the rate I want it to?

PaulS

Why are you running an infinite loop inside an infinite loop? Looks a trifle stupid to me.

The blink without delay example would show that interrupts are not needed at all.

Go Up