Posts with «freetronics eleven» label

Bluetooth Tutorial 1


Introduction:
The bluetooth shield used in this project is a great way to detach the Arduino from your computer. What is even better, is that the shield allows you to control your arduino from your mobile phone or other bluetooth enabled device through simple Serial commands. In this tutorial we will connect a Grove Chainable RGB LED to the bluetooth shield directly, and send simple commands using the Bluetooth SPP app on a Samsung Galaxy S2 to change the colour of the LED (Red , Green and Blue)



Parts Required:
Freetronics Eleven or any compatible Arduino.
Bluetooth shield
Grove Chainable RGB LED
Grove Wire connectors




The Video:





The Arduino Sketch:








Arduino Code:
You can download the Arduino IDE from this site.


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This project combines the code from a few different sources.
This project was put together by ScottC on the 15/01/2013
http://arduinobasics.blogspot.com/

Bluetooth slave code by Steve Chang - downloaded from :
http://www.seeedstudio.com/wiki/index.php?title=Bluetooth_Shield

Grove Chainable RGB code can be found here :
http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED#Introduction

*/

#include <SoftwareSerial.h> //Software Serial Port

#define uint8 unsigned char
#define uint16 unsigned int
#define uint32 unsigned long int

#define RxD 6 // This is the pin that the Bluetooth (BT_TX) will transmit to the Arduino (RxD)
#define TxD 7 // This is the pin that the Bluetooth (BT_RX) will receive from the Arduino (TxD)

#define DEBUG_ENABLED 1


int Clkpin = 9; //RGB LED Clock Pin (Digital 9)
int Datapin = 8; //RGB LED Data Pin (Digital 8)

SoftwareSerial blueToothSerial(RxD,TxD);

/*----------------------SETUP----------------------------*/
void setup() {
Serial.begin(9600); // Allow Serial communication via USB cable to computer (if required)
pinMode(RxD, INPUT); // Setup the Arduino to receive INPUT from the bluetooth shield on Digital Pin 6
pinMode(TxD, OUTPUT); // Setup the Arduino to send data (OUTPUT) to the bluetooth shield on Digital Pin 7
pinMode(13,OUTPUT); // Use onboard LED if required.
setupBlueToothConnection(); //Used to initialise the Bluetooth shield

pinMode(Datapin, OUTPUT); // Setup the RGB LED Data Pin
pinMode(Clkpin, OUTPUT); // Setup the RGB LED Clock pin

}

/*----------------------LOOP----------------------------*/
void loop() {
digitalWrite(13,LOW); //Turn off the onboard Arduino LED
char recvChar;
while(1){
if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
recvChar = blueToothSerial.read();
Serial.print(recvChar); // Print the character received to the Serial Monitor (if required)

//If the character received = 'r' , then change the RGB led to display a RED colour
if(recvChar=='r'){
Send32Zero(); // begin
DataDealWithAndSend(255, 0, 0); // first node data
Send32Zero(); // send to update data
}

//If the character received = 'g' , then change the RGB led to display a GREEN colour
if(recvChar=='g'){
Send32Zero(); // begin
DataDealWithAndSend(0, 255, 0); // first node data
Send32Zero(); // send to update data
}

//If the character received = 'b' , then change the RGB led to display a BLUE colour
if(recvChar=='b'){
Send32Zero(); // begin
DataDealWithAndSend(0, 0, 255); // first node data
Send32Zero(); // send to update data
}
}

//You can use the following code to deal with any information coming from the Computer (serial monitor)
if(Serial.available()){
recvChar = Serial.read();

//This will send value obtained (recvChar) to the phone. The value will be displayed on the phone.
blueToothSerial.print(recvChar);
}
}
}


//The following code is necessary to setup the bluetooth shield ------copy and paste----------------
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();
}


//The following code snippets are used update the colour of the RGB LED-----copy and paste------------
void ClkProduce(void){
digitalWrite(Clkpin, LOW);
delayMicroseconds(20);
digitalWrite(Clkpin, HIGH);
delayMicroseconds(20);
}

void Send32Zero(void){
unsigned char i;
for (i=0; i<32; i++){
digitalWrite(Datapin, LOW);
ClkProduce();
}
}

uint8 TakeAntiCode(uint8 dat){
uint8 tmp = 0;
if ((dat & 0x80) == 0){
tmp |= 0x02;
}

if ((dat & 0x40) == 0){
tmp |= 0x01;
}

return tmp;
}

// gray data
void DatSend(uint32 dx){
uint8 i;
for (i=0; i<32; i++){
if ((dx & 0x80000000) != 0){
digitalWrite(Datapin, HIGH);
} else {
digitalWrite(Datapin, LOW);
}

dx <<= 1;
ClkProduce();
}
}

// data processing
void DataDealWithAndSend(uint8 r, uint8 g, uint8 b){
uint32 dx = 0;

dx |= (uint32)0x03 << 30; // highest two bits 1,flag bits
dx |= (uint32)TakeAntiCode(b) << 28;
dx |= (uint32)TakeAntiCode(g) << 26;
dx |= (uint32)TakeAntiCode(r) << 24;

dx |= (uint32)b << 16;
dx |= (uint32)g << 8;
dx |= r;

DatSend(dx);
}

The code above was formatted using hilite.me

Notes:
You don't need to download a library to get this project running. But if you plan to use bluetooth shields to get 2 Arduinos to communicate to each other, then I would advise that you download the library files (which are just examples) from the Seeedstudio site : here.

Visit this site to setup your phone or laptop for bluetooth communication to the shield - here

The app used on my Samsung Galaxy S2 phone was "Bluetooth SPP"

You will initially need to enter a pin of '0000' to establish a connection to the Bluetooth shield - which will appear as "SeeedBTSlave" or whatever text you place on line 90 of the Arduino code above.





Warning !

Not all phones are compatible with the bluetooth shield.
If you have used this shield before - please let me know what phone you used - so that we can build a list and inform others whether their phone is likely to work with this project or not. Obviously - those phones that do not have bluetooth within - will not work :).
And I have not tried any other apps either

I got it to work very easily with my Samsung Galaxy S2 using the free Bluetooth SPP app from the google play store.

This was fun, but I want to make my own app !
Have a look at my latest 4-part tutorial which takes you step-by-step through the process of building your own app using the Processing/Android IDE.
You can build your own GUI interface on your Android Phone and get it to communicate via Bluetooth to your Arduino/Bluetooth Shield. Click on the links below for more information:




 
 



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
Have a look at my videos on my YouTube channel.


 
 

 
 
 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

Grove OLED 96x96 Slideshow

This project makes use of the Grove OLED 96x96 display to present a mini-slideshow.  Pictures on your computer are transferred to the OLED via a Processing script, and will cycle through them as many times as you choose.

Video:



Parts required:


Software required:


Sketch:
















Arduino Sketch:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// OLED Slideshow: Arduino Sketch written by ScottC 21/07/2012

#include <Wire.h>
#include <SeeedGrayOLED.h> //From http://garden.seeedstudio.com/images/c/c4/SeeedGrayOLED.zip
#include <avr/pgmspace.h>

int counter=0;

void setup()
{
//Allow communication to OLED
Wire.begin();

//Allow Serial communication between Freetronics Eleven and Computer
Serial.begin(28800);

//Initialise the OLED
SeeedGrayOled.init();
//Clear the OLED display
SeeedGrayOled.clearDisplay();
//Set to vertical mode - horizontal mode doesn't work with this example
SeeedGrayOled.setVerticalMode();

}

void loop(){
//Listen for Serial comunication
while (Serial.available()>0) {
// Read data and send directly to the OLED
sendMyData(Serial.read());
counter++;

//When counter reaches 4608 pixels, the picture is complete.
if(counter>4607){
//Insert delay to allow viewing of picture.
delay(4000);
Serial.println("End of Transmission");

//Reset the counter for the next picture
counter=0;
}
}
}

// This function was adapted from the SEEED Gray OLED driver so that
// character bytes could be sent directly to the OLED.
void sendMyData(unsigned char Data){
Wire.beginTransmission(SeeedGrayOLED_Address); // begin I2C transmission
Wire.send(SeeedGrayOLED_Data_Mode); // data mode
Wire.send(Data);
Wire.endTransmission();
}


// This function was adapted from the SEEED Gray OLED driver so that
// commands could be sent directly to the OLED.
// NOT USED IN THIS EXAMPLE ***********************
void sendMyCommand(unsigned char Cmd){
Wire.beginTransmission(SeeedGrayOLED_Address); // begin I2C communication
Wire.send(SeeedGrayOLED_Command_Mode); // Set OLED Command mode
Wire.send(Cmd);
Wire.endTransmission(); // End I2C communication
}




Processing Sketch:
  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* OLED Slideshow: Processing Sketch Written by ScottC 21/7/2012 

References:
Getting Pixels: http://www.learningprocessing.com/examples/chapter-15/example-15-7/
Greyscale conversion = http://www.openprocessing.org/sketch/60336
*/


import processing.serial.*; /* Needed for Serial Communication */

/* Global variables */
Serial comPort;
String [] comPortList;
String comPortString;
PImage img;
char[] tempGrey=new char[4609];
int startOffset=0;
ArrayList picNames;
int curLoop=1;
int totalPics=0;
int curPicNum=0;
boolean toggleSend=true;
boolean sendBouncer=true;

//Change maxLoops to a number > 1 if you want the pictures to loop.
int maxLoops=1;


void setup() {
//The size of the display is critical (must match the OLED)
size(96, 96);
//setup Serial
comPortList = Serial.list();
if(comPortList.length>0){
//baud rates greater than 28800 may produce unexpected results
comPort = new Serial(this, comPortList[0], 28800);
comPort.bufferUntil('\n');
} else{
println("NO COM PORTS AVAILABLE");
}

//Create an Array of pictures
picNames=new ArrayList();
picNames.add("Picture1.bmp");
picNames.add("Picture2.bmp");
picNames.add("Picture3.bmp");
picNames.add("Picture4.bmp");
// for more pictures just keep adding them to the list.
//The actual pictures must be located in the data folder of this project.
//Select Sketch/Add File to add the files to this folder.
//Make sure that the name of pictures match the names above.

//Get the total number of pictures added
totalPics=picNames.size();
}

void draw(){
if(toggleSend && sendBouncer){

// Debugging code: print("STARTED:");
// Debugging code: println(picNames.get(curPicNum));

sendImage((String)picNames.get(curPicNum)); //Send the picture to the OLED
toggleSend=false; //temporarily stop sending any more pictures until authorised
curPicNum++; //increment in preparation for the next picture

if(curPicNum==totalPics){
curPicNum=0; //go back to the first picture
curLoop++; //increment the loop counter
}
if(curLoop>maxLoops){
sendBouncer=false; //Stop any further looping
println("ANIMATION COMPLETE");
}
}
}


void sendImage(String imgName){
img = loadImage(imgName);
image(img,0,0,width,height);
loadPixels();
int counter=0;
for (int x = 0; x < width; x=x+2) {
for (int y = 0; y < height; y++) {
counter++;
int PixLoc = x + y*height; // this reads down then across2.
//Left pixel nibble
int Pix1=(round((red(pixels[PixLoc])*0.222+green(pixels[PixLoc])*0.707+blue(pixels[PixLoc])*0.071)))/16;
//Right pixel nibble
int Pix2=(round((red(pixels[PixLoc+1])*0.222+green(pixels[PixLoc+1])*0.707+blue(pixels[PixLoc+1])*0.071)))/16;
//Shift the byte <<4 for the left pixel nibble
int PixShift1=Pix1<<4;
//Combine both nibbles to form a byte
int PixFin = PixShift1+Pix2;
byte PixByteFin=byte(PixFin);
//Assign this byte to the tempGrey array
tempGrey[counter] = char(PixByteFin);
}
}
sendSerial(tempGrey); //Send the image data through the Serial COM Port/
}


//This function will send the byte/Char array to the Freetronics
//Eleven or Arduino.
void sendSerial(char[] Data){
for(int i=0; i<4608; i++){
//Needs an offset to get picture to align to screen properly
//only needs to do this once.
if(startOffset==0){
i=i+6;
startOffset++;
}
//Send the picture data to the Freetronics Eleven / Arduino
comPort.write(Data[i]);
}
}


//This function will wait for a response from the Freetronics
//Eleven or Arduino before sending any further pictures.
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
println(inString);
toggleSend=true; // Allow the next picture to be sent
}
}


Please note: that you must use the Arduino IDE version 023 until Seeed Studio update their driver for this OLED. Their current driver is not compatible with later versions of Arduino IDE.