Go Down

Topic: Processing to Arduino with Bluetooth  (Read 248 times) previous topic - next topic

Jaxdenim

May 14, 2019, 06:17 pm Last Edit: May 14, 2019, 06:20 pm by Jaxdenim
Hey Lads,

I am running a processing sketch that sends serial inputs (1,2,3,4,5,6) to Arduino. (serial : 9600)
now I'm using Arduino to receive the serials and use them to control a led matrix.

Here are the codes:

Arduino:

Code: [Select]


#include <FastLED.h>

#define LED_PIN     13
#define NUM_LEDS    36

CRGB leds[NUM_LEDS];

void setup()
{
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(9600);

  FastLED.clear();
  FastLED.show();
}

void loop()
{

  if (Serial.available() > 0)
  {
    int incomingByte = Serial.read();

    if (incomingByte > '0' && incomingByte < '7')
    {
      FastLED.clear();   //  <-----<<<<    USE clear
    }


    if (incomingByte == '1')
    {
      leds[0] = CRGB(255, 0, 0);
      leds[6] = CRGB(255, 0, 0);
      leds[12] = CRGB(255, 0, 0);
      leds[18] = CRGB(255, 0, 0);
      leds[24] = CRGB(255, 0, 0);
      leds[30] = CRGB(255, 0, 0);
    }
    else if (incomingByte == '2')
    {
      leds[1] = CRGB(255, 0, 0);
      leds[7] = CRGB(255, 0, 0);
      leds[13] = CRGB(255, 0, 0);
      leds[19] = CRGB(255, 0, 0);
      leds[25] = CRGB(255, 0, 0);
      leds[31] = CRGB(255, 0, 0);
    }
    else if (incomingByte == '3')
    {
      leds[2] = CRGB(255, 0, 0);
      leds[8] = CRGB(255, 0, 0);
      leds[14] = CRGB(255, 0, 0);
      leds[20] = CRGB(255, 0, 0);
      leds[26] = CRGB(255, 0, 0);
      leds[32] = CRGB(255, 0, 0);
     
    }
    else if (incomingByte == '4')
    {
      leds[3] = CRGB(255, 0, 0);
      leds[9] = CRGB(255, 0, 0);
      leds[15] = CRGB(255, 0, 0);
      leds[21] = CRGB(255, 0, 0);
      leds[27] = CRGB(255, 0, 0);
      leds[33] = CRGB(255, 0, 0);
    }
    else if (incomingByte == '5')
    {
      leds[4] = CRGB(255, 0, 0);
      leds[10] = CRGB(255, 0, 0);
      leds[16] = CRGB(255, 0, 0);
      leds[22] = CRGB(255, 0, 0);
      leds[28] = CRGB(255, 0, 0);
      leds[34] = CRGB(255, 0, 0);
    }
    else if (incomingByte == '6')
    {
      leds[5] = CRGB(255, 0, 0);
      leds[11] = CRGB(255, 0, 0);
      leds[17] = CRGB(255, 0, 0);
      leds[23] = CRGB(255, 0, 0);
      leds[29] = CRGB(255, 0, 0);
      leds[35] = CRGB(255, 0, 0);
    }

    FastLED.show();
  }
}



and Processing

Code: [Select]


float x = 100;
float y = 50;
float w = 150;
float h = 80;

import org.openkinect.freenect.*;
import org.openkinect.processing.*;
import processing.serial.*;


// The kinect stuff is happening in another class
KinectTracker tracker;
Kinect kinect;
Serial myPort;

void setup() {
  size(512, 424);
  kinect = new Kinect(this);
  tracker = new KinectTracker();
  String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw() {
 
  tracker.track();
  tracker.display();
 
  PVector v1 = tracker.getPos();
  fill(50, 100, 250, 200);
  noStroke();
  ellipse(v1.x, v1.y, 50, 50);

   
   
   
   //1
    if (v1.x > 0 && v1.x < 50 ){
    println('1');
    //rect(0,200,200,200);
    myPort.write('1'); }
    //println('0'); }
   
    //2
  if (v1.x > 50 && v1.x < 100){
    println('2');
    myPort.write('2'); }   
 //3
   if (v1.x > 100 && v1.x < 150 ){
    println('3');
    myPort.write('3'); }
   
    //4
   if (v1.x > 150 && v1.x < 200 ){
    println('4');
   myPort.write('4'); }
   
    //5
   if (v1.x > 200 && v1.x < 250 ){
    println('5');
    myPort.write('5'); }
   
    //6
   if (v1.x > 250 && v1.x < 300 ){
    println('6');
    myPort.write('6'); }
   
}



Now the problem is running this sketch being connected to pc works. Processing sends to Arduino.
But using a HC05 Bluetooth module the code's don't run anymore.

(it is connected to my computer, and port on the Arduino is also fine)

I can run the processing sketch fine. Respectivly I can also run the Arduino sketch fine. But they don't work together. It says the device is busy.
 
Anyone have any tips? What am I doing wrong here?

PaulS

Quote
But using a HC05 Bluetooth module the code's don't run anymore.
Nonsense. The code still runs. It may not send, or receive, any data, because you don't have the bluetooth devices connected/paired correctly, but that doesn't change the fact that code still runs.

Quote
It says the device is busy.
What does?

You don't have Processing and the Serial Monitor app both trying to talk to the Arduino, do you?
The art of getting good answers lies in asking good questions.

Jaxdenim

#2
May 14, 2019, 08:19 pm Last Edit: May 14, 2019, 08:38 pm by Jaxdenim
Nonsense. The code still runs. It may not send, or receive, any data, because you don't have the bluetooth devices connected/paired correctly, but that doesn't change the fact that code still runs.
What does?

You don't have Processing and the Serial Monitor app both trying to talk to the Arduino, do you?
I am not an expert on Bluetooth whatsoever. I will discribe to you what I have done so far.
Plugged in the bluetooth shield, connected it to my Mac OSX (it's called: HC-05) and in Arduino I have selected it as a port.

I am letting processing send serials to Arduino. I want to be able to do this wirelessly (so through bluetooth) .
I am not familiar with what you call a "serial monitor app" . Do I need to setup my bluetooth shield before I can use it?

If I run it with usb connected to the Mac it works fine (Port /dev/usbmodem1411) , disconnecting it and trying to use the hc05 bluetooth as a port (/dev/cu.HC-05-DevB) results in a programmer not responding

Robin2

Start with a very simple program that just sends "<hello world>" every second.

It may be easier to start with the Arduino sending to your PC to make sure that communication works. Just use a terminal emulator program on your PC to receive the message.

IIRC a HC05 cannot communicate with an Apple phone. Maybe that is also true for Apple PCs?

How is your Bluetooth device connected to your Arduino?

If you are using an Uno then it would be best to use SoftwareSerial to create a separate serial port for the HC05 so that HardwareSerial is free for uploading programs and for debug messages.

For receiving data on the Arduino have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Jaxdenim

#4
May 14, 2019, 09:48 pm Last Edit: May 15, 2019, 12:28 am by Jaxdenim
Start with a very simple program that just sends "<hello world>" every second.

It may be easier to start with the Arduino sending to your PC to make sure that communication works. Just use a terminal emulator program on your PC to receive the message.

IIRC a HC05 cannot communicate with an Apple phone. Maybe that is also true for Apple PCs?

How is your Bluetooth device connected to your Arduino?

If you are using an Uno then it would be best to use SoftwareSerial to create a separate serial port for the HC05 so that HardwareSerial is free for uploading programs and for debug messages.

For receiving data on the Arduino have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
I know that HC05 should work properly with Macs, (I am connected to my Mac)   
Here's how it is connected to my board : https://www.hackerstore.nl/PDFs/BTShield.pdf



Nick_Pyner

#5
May 15, 2019, 01:03 am Last Edit: May 15, 2019, 01:12 am by Nick_Pyner
IIRC a HC05 cannot communicate with an Apple phone. Maybe that is also true for Apple PCs?
It isn't. I understand the problem is with the phone/tablet operating system IOS, which rules out plain vanilla bluetooth, but it could be due to a sub-standard BT4 module which is niot backwards compatible. Mac desktops use a different operating system which is OK with HC-0x.

This is looking rather like the mac-grief Gonadgranny is having with the serial monitor - which should be left out of the game anyway.
https://forum.arduino.cc/index.php?topic=614852.0


Jaxdenim

#6
May 15, 2019, 02:16 am Last Edit: May 15, 2019, 02:17 am by Jaxdenim
It isn't. I understand the problem is with the phone/tablet operating system IOS, which rules out plain vanilla bluetooth, but it could be due to a sub-standard BT4 module which is niot backwards compatible. Mac desktops use a different operating system which is OK with HC-0x.

This is looking rather like the mac-grief Gonadgranny is having with the serial monitor - which should be left out of the game anyway.
https://forum.arduino.cc/index.php?topic=614852.0


Thank you Nick this was a really useful read. I am trying to make my problem a little simpler by making it just turn on 1 LED on and off by sending a 1 and a 0.

My Arduino is connected to my Mac and I have uploaded the code, I'm able to write 1 in the serial monitor that turns my led on and a 0 that turns it off.

I use "BluTerm" a freeware program to be able to communcate with the HC-05. I was able to connect it, (The led is showing bright green on my shield) but sending a 1 here does not turn the led on. It seems like I can't get it to connect.

The problem might be my setup since I am using a shield and can't seem to find any manuals of it. I will include some foto's. Does anyone know what I can try next?







Nick_Pyner

#7
May 15, 2019, 05:36 am Last Edit: May 15, 2019, 05:48 am by Nick_Pyner
I use "BluTerm" a freeware program to be able to communcate with the HC-05. I was able to connect it, (The led is showing bright green on my shield) but sending a 1 here does not turn the led on. It seems like I can't get it to connect.
Using a proper terminal like BlueTerm is a good move but, while a start, having it confirm communication with HC-05 is no more than just that, and nothing to do with Arduino per se.
Quote
The problem might be my setup since I am using a shield and can't seem to find any manuals of it. I will include some foto's.
I think you are right, but I'm surprised you can't get some joy from the supplier.
The picture shows the shield set up to use Arduino pins 6,7 for Bluetooth, thereby requiring you to use software serial. You code only uses Hardware serial, which is fine, but you still need to know what you are doing. . If you simply move the yellow jumpers along to pins 0,1 you may find this is your only problem, and it will work with the code you have already uploaded. Note that, with this setup, you need to remove those pins when you want to change the programme. I believe you can actually get by with removing just one jumper, but I don't know which.

Jaxdenim

#8
May 15, 2019, 12:08 pm Last Edit: May 15, 2019, 12:38 pm by Jaxdenim
Using a proper terminal like BlueTerm is a good move but, while a start, having it confirm communication with HC-05 is no more than just that, and nothing to do with Arduino per se.I think you are right, but I'm surprised you can't get some joy from the supplier.
The picture shows the shield set up to use Arduino pins 6,7 for Bluetooth, thereby requiring you to use software serial. You code only uses Hardware serial, which is fine, but you still need to know what you are doing. . If you simply move the yellow jumpers along to pins 0,1 you may find this is your only problem, and it will work with the code you have already uploaded. Note that, with this setup, you need to remove those pins when you want to change the programme. I believe you can actually get by with removing just one jumper, but I don't know which.
So, what you are saying is that I am using the right setup on my yellow jumpers. Its not Clear to me yet how to setup these yellow jumpers.

What does it mean that left one is on top of BT_AX and Digital at 7 and the Right jumper is on BT_TX and digital at 6? does putting it out use the pin, or does it cancel that pin out?

Also you are saying when I upload it to the module I should remove all the yellow jumpers, and place them on after the upload?

Maybe anyone have any leads on the jumper part? everyone seems to use HC05 as individual entities so having this piece complicates it.

also I don't know if this helps anyone but this pdf was on the link I bought it at: https://hackerstore.nl/PDFs/BTShield.pdf

It shows the BAUD Rate of 9600 and has some example code,
Code: [Select]


unsigned int timeout=0; unsigned char state=0;
ISR(TIMER2_OVF_vect) {
TCNT2 = 0; timeout++;
if (timeout>61) {
state=1;
timeout=0; }
}
void init_timer2(void) {
//Timer2 Service
TCCR2A |= (1 << WGM21) | (1 << WGM20);
TCCR2B |= 0x07; // by clk/1024
ASSR |= (0<<AS2); // Use internal clock - external clock not used in Arduino
TIMSK2 |= 0x01; TCNT2 = 0; sei();
}
void setup() {
//Timer2 Overflow Interrupt Enable
Serial.begin(9600);
pinMode(2,INPUT); pinMode(13,OUTPUT); attachInterrupt(0,cleantime,FALLING); init_timer2();
}
void loop() {
switch(state) {
case 0:
digitalWrite(13,LOW); break;

case 1: digitalWrite(13,HIGH); Serial.print("Hellow BT"); break;
} }
void cleantime() {
timeout=0;
state=0; }




I don't really understand what this piece of code does. or where I should place the jumpers to get this to work.

Nick_Pyner

#9
May 15, 2019, 04:56 pm Last Edit: May 16, 2019, 06:16 am by Nick_Pyner
So, what you are saying is that I am using the right setup on my yellow jumpers. Its not Clear to me yet how to setup these yellow jumpers. 
No, I'm saying the opposite. You have two options - change the code, or change the pins. I suggest the latter, and leave the code in place.  Your command in Setup:
Serial.begin(9600);
starts the hardware serial, which is on pins 0,1, and your Serial commands in the loop sends the data there, so that is where you connect Bluetooth.
Quote
What does it mean that left one is on top of BT_AX and Digital at 7 and the Right jumper is on BT_TX and digital at 6? does putting it out use the pin, or does it cancel that pin out?
Read it again, it should be Rx, not Ax. As I said, Bluetooth is connected to Arduino pins 6,7. Move Tx jumper from 6 yo 0 and Rx from 7 to 1. Now check ARDUINO pins. You will see that pin 0 is labelled Rx and pin 1 Tx. So now Tx on the shield is connected to Rx on Arduino, and tx on Arduino is connected to Rx on Bluetooth.
Quote
Also you are saying when I upload it to the module I should remove all the yellow jumpers, and place them on after the upload?
Yes.
This because the USB cable input shares the same port.
Quote
Maybe anyone have any leads on the jumper part?
Incoherent
Quote
everyone seems to use HC05 as individual entities so having this piece complicates it.
Not true. A better reason for not using that shield is its exorbitant price. Now that you have it, and have paid for it, it isn't so bad, it is foolproof, versatile, and convenient. It also has provision for peripherals. I just hope you can find plugs to go in those sockets.
Quote
also I don't know if this helps anyone but this pdf was on the link I bought it at: https://hackerstore.nl/PDFs/BTShield.pdf
This link does not refer to the shield in the photograph you posted. Further, the Bluetooth module thereon is an HC-06, not HC-05.

Jaxdenim

#10
May 15, 2019, 06:31 pm Last Edit: May 15, 2019, 07:03 pm by Jaxdenim
No, I'm saying the opposite. You have two options - change the code, or change the pins. I suggest the latter, and leave the code in place.  You command in Setup:
Serial.begin(9600);
starts the hardware serial, which is on pins 0,1, and your Serial commands in the loop sends the data there, so that is where you connect Bluetooth.Read it again, it should be Rx, not Ax. As I said, Bluetooth is connected to Arduino pins 6,7. Move Tx jumper from 6 yo 0 and Rx from 7 to 1. Now check ARDUINO pins. You will see that pin 0 is labelled Rx and pin 1 Tx. So now Tx on the shield is connected to Rx on Arduino, and tx on Arduino is connected to Rx on Bluetooth.Yes.
This because the USB cable input shares the same port. IncoherentNot true. A better reason for not using that shield is its exorbitant price. Now that you have it, and have paid for it, it isn't so bad, it is foolproof, versatile, and convenient. It also has provision for peripherals. I just hope you can find plugs to go in those sockets.This link does not refer to the shield in the photograph you posted. Further, the Bluetooth module thereon is an HC-06, not HC-05.
Thank you for elaborating! so now I have properly set up the UART pins. uploading to Arduino worked ,tested it out and without jumpers it indeed works. I have connected it to the MAC OSX.

1. The three pins on the bottom left, where it says AT mode. how would I go and work with these? is it supposed to be in the bottom 2 for AT mode or top 2? I have connected

I have included the pins on my setup as of now.
Btw here is the code I am running now:

Code: [Select]


char data;
int led = 13;

void setup(){
   pinMode(led, OUTPUT);
 Serial.begin(9600);
}

void loop(){

  data = Serial.read();

if (data == '1') {
    Serial.println("LED ON");
    digitalWrite(led, HIGH);

  }
if (data == '0') {
    digitalWrite(led, LOW);
    Serial.println("LED OFF");

  }

  delay(200);

}



sample idea just uses a 1 and 0 to turn a LED on and off, the LED works fine without the jumpers but when the jumpers are connected tx >rx, rx> tx It stopped working. (maybe an indication of something happening)

Jaxdenim

#11
May 15, 2019, 11:20 pm Last Edit: May 15, 2019, 11:23 pm by Jaxdenim
Ok for now I got this code to work.

Code: [Select]


char data;
int led = 13;

void setup(){
   pinMode(led, OUTPUT);
 Serial.begin(9600);
}

void loop(){

  data = Serial.read();

if (data == '1') {
    Serial.println("LED ON");
    digitalWrite(led, HIGH);

  }
if (data == '0') {
    digitalWrite(led, LOW);
    Serial.println("LED OFF");

  }

  delay(200);

}



I can with the proper pin setup send 1,0 to command a Single Led through bluetooth. It's defiantly a start & a very useful experience. thank you Nick for all the effort!

So I can send 1's and 0's through the cool term.

My next challenge is to make a processing code send to Arduino. and the Arduino to receive these 0's and 1's through bluetooth. will be looking into this now.

Anybody got any ideas how to tackle something like this?

Jaxdenim

#12
May 16, 2019, 12:16 am Last Edit: May 16, 2019, 12:18 am by Jaxdenim
Okay well running it through processing wasn't too hard. pressing 1 will turn on a led.
also it works through bluetooth by using  "Bluterm" but I want that to completely dissapear out of the equation, what I want to know is can processing send serials to Arduino over bluetooth?

so basically it can send a 1 wireless to a Arduino.

Code here:

Code: [Select]


import processing.serial.*;

Serial port;


String string;
void setup(){
    String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
    port = new Serial(this, portName, 9600);
    port.bufferUntil('\n');
}

void draw() {
 
  printArray(string);
}

void keyPressed() {
  if (key =='1'){port.write('1');}
    if (key=='0') {port.write('0');}
    }
   
    void serialEvent(Serial port) {
      string = port.readStringUntil('\n');}
 
 


Here is the Arduino code:

Code: [Select]


char data;
int led = 13;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
 
}

void loop() {
  if (Serial.available()>0){
    data = Serial.read();
  }
 

  if (data=='1'){
    Serial.println("HELLO");
    digitalWrite(led, HIGH);

   
    }

    else if (data=='0'){
      digitalWrite(led, LOW);
      Serial.println("BYE");}
    }




Nick_Pyner

Glad to hear of your consistent and cumulative successes, but you need to be aware of this fundamental point:

Bluetooth is simply serial without wires and operations at each end are the same as serial with wires.

Arduino converses with its serial port, and neither knows nor cares what is on the other side. I don't know anything about Processing, but you should be able to simply send the commands to a serial port with a Bluetooth dongle, in the same manner as you have already done with BlueTerm, and you are off to the races. You have done all the hard stuff, and I don't suppose you need to change any Arduino code.

Jaxdenim

Glad to hear of your consistent and cumulative successes, but you need to be aware of this fundamental point:

Bluetooth is simply serial without wires and operations at each end are the same as serial with wires.

Arduino converses with its serial port, and neither knows nor cares what is on the other side. I don't know anything about Processing, but you should be able to simply send the commands to a serial port with a Bluetooth dongle, in the same manner as you have already done with BlueTerm, and you are off to the races. You have done all the hard stuff, and I don't suppose you need to change any Arduino code.

Ok so from what I understand from this, is that basically I just need to buy a bluetooth dongle so that I can use it as a port in Arduino.

But what I don't understand is that "Blueterm" uses the max osx bluetooth to talk to Arduino. Isn't there a software solution that creates a port so that Arduino can use this?

If not, what bluetooth dongles are recommended in communicating with HC05?

Go Up