Friday, April 12, 2013

Process of getting attiny85 to work with nRF24L01 on RF24 libraries

While getting Arduino or Raspberry Pi to talk to nRF24L01 was fun and challenging, trying to make it work for a 8-pin attiny85 is a whole different experience... without serial monitor or printf debugging to the display, after the firmware was flash into the attiny85, you could only guess what is happenning or not happening when nothing was received on the nRF24L01 receiver side...

Initially, I just assume that since the Atmel attiny85 does not have hardware SPI, getting nRF24L01 talking to the attiny was not so possible... but after a few searches and reading up some blogs and forums, things seems to look brighter...

attiny85 pinout

The second challenge was the usable pins on the attiny85, with 8 pins and two used by power and ground, only six pins are available... and the nRF24L01 radios uses the usual SPI - MOSI, MISO, CLK plus another two pins for SS/CSN and CE leaving only the reset pin unused.

The third challenge was size of the firmware, unlike Arduino UNO using atmega328 with 32Kb of flash, the attiny85 only have 8Kb of flash, so optimizing the code was really important, and this is the largest flash size of the attiny x5 series. See this link for the attiny comparison chart.

With the help of Alex from Insidegadgets.com, I start from a scaled down mirf codes ( mirf was the initial libraries for nRF24L01 written in C for the AVR ) using notepad, WinAVR and avrdude to compile and flash the firmware into the attiny85. Then I was using a ported version to Arduino IDE that makes things much more easier as Arduino IDE can still support USBtinyISP, the programmer I was using to flash firmware into the attiny85.

After a few unsuccessful retries, I decide to use a different approach and found on Arduino forum that someone had modified the Arduino SPI and mirf into ther attiny85 version called SPI85 and mirf85. After matching all the channels, data rate and CRC, I manage to get some packets on Serial Monitor with a mirf library on the Arduino UNO.

Since all my libraries are using RF24 for both the Arduino & Raspberry Pi, having a mirf library isn't too much useful for me. I went through all the codes and default settings and managed to configure the mirf to be compatible with RF24 library.

attiny85 nRF24L01 USBtinyISP


Here are a summary of differences between both the mirf and RF24 library :-

Data rate :
mirf : 2Mbps as it uses the default registry setting of the nRF24L01
RF24 : configurable via setDataRate()

CRC length :
mirf : 8bit CRC length
To make it work for the RF24, change the following header files directly :-
mirf85.h
#define mirf_CONFIG ((1<<EN_CRC) | (1<<CRCO) )

RF24 : configurable via setCRCLength()

Node addressing :
mirf : serv1 or clie1 ( 5 bytes in ascii format )
To talk to RF24 receiver, use the following format,  :-

    byte RADDR[] = {0xe2, 0xf0, 0xf0, 0xf0, 0xf0};
    byte TADDR[] = {0xe3, 0xf0, 0xf0, 0xf0, 0xf0};


RF24: f0f0f0f0e2 format ( 5 bytes in hex format )

Optional settings :-

Enable Dynamic Payload :
I had my RF24 receiver with dynamic payload enabled so that I only need to set the payload size on transmitter only and can use different payload length instead of the max length.. set the below to mirf codes to enable dynamic payload.

mirf :
Mirf.configRegister( FEATURE, 1<<EN_DPL );
Mirf.configRegister( DYNPD, 1<<DPL_P0 | 1<<DPL_P1 | 1<<DPL_P2 | 1<<DPL_P3 | 1<<DPL_P4 | 1<<DPL_P5 );

I've compiled all the SPI85 and mirf85 files at github repo at the summary links below.

Summary Links :-

- https://github.com/stanleyseow/arduino-nrf24l01
- Insidegadgets.com
- Arduino forum




Thursday, April 11, 2013

The Problem ( and Solutions ) With Arduino Interrupts

If you have ever tried to port an Arduino project that uses interrupts from one board type to another, you have probably experienced frustration, this includes the sample code throughout RCArduino.


The following post examines the differences in interrupts between the popular boards, the Arduino UNO, Micro, Mega, Mini and Leonardo. Based on this information we can modify sketches to run on the full range of 8-bit Arduinos.

The 8-bitArduino boards are based on one of three related chips -

ATMega328 - UNO and Mini
ATMega32u4 - Leonardo and Micro
ATMega2560 - Mega

Each of these chips supports two types of interrupts -

1) External Interrupts
These are flexible easy to use interrupts which can be triggered by rising, falling or changing signals. The disadvantage is that there are a limited number available on each chip type.

If we want to access more interrupts we need to look at the next type -

2) Pin Change Interrupts
The underlying chip in your Arduino supports a second type of interrupt however these interrupts are not directly supported by Arduino and need to be accessed through an additional library.

Differences In External Interrupts

The external interrupts are associated with specific digital pins on each chip type, the following table taken from the attachInterrupt reference page lists the available external interrupts and the associated Arduino pin on each chip -


Boardint.0int.1int.2int.3int.4int.5
Uno, Ethernet,
Mini
23
Mega25602321201918
Leonardo, Micro3201


The Arduino team have hidden some of the differences between the ATMega328 and ATM2560 so that attaching INT0 using the attachInterrupt function will attach an interrupt to digital pin 2 on both chips even though on the Mega digital pin2 is actually INT4.

The same logic has not been carried across to the ATMega32u4 Based leonardo. Notice how int0 and int1 are actually reversed on the Leonardo, this will be a major trap for people who are porting code from the UNO.


Does the Leonardo have four external interrupts ?
While the Leonardo appears to have 4 external interrupts, int2 and int3 are attached to digital pins 0 and 1 which are almost always reserved for serial input/output. So yes there are four interrupts, but two of them are only available by disabling serial functionality.

Differences In Pin Change Interrupts
On the Arduino UNO, pin change interrupts can be used to enable interrupts on any of the Arduino PINs to give access to a total of 19 interrupts (13 digital pins and 6 Analog pins).

I initially assumed that this was also possible on the Mega, Micro and Leonardo as well. It isn't.

Pin change interrupts are supported on the following Leonardo/Micro pins - 8,9,10 and 11.

Pin change interrupts are supported on Arduino Mega pins 10,11,12,13,14,15 and analog pins 6 to 15

Interrupts and RCArduino
These differences between the Arduino platforms will have been responsible for some of the difficulty that users have had in porting RCArduino code to Minis, Micros, Leonardos and Megas.

The good news is that now we have a full understanding of the inconsistencies between the different devices there should be no problem in modifying the sample sketches to run on them.

If your having trouble with a sample sketch, get in touch, in the meantime I will be updating some of the sketches to work across multiple boards.

Stay tuned

Duane B

Sunday, April 7, 2013

Serial Communication Tutorial (Part 3)





In the previous two parts of the this tutorial, we went through a number of simple sketches to get you acquainted with the way that the Arduino handles various data types when passed through the Serial COM port. Here are the main themes from part ONE:
  • Stage One:  Echoing data with the Arduino
  • Stage Two: Using Delimiters to split data.
  • Stage Three:Arduino Maths, simple addition
  • Stage Four:Sending a double to an Arduino, and then doubling it.
  • Stage Five:Sending Sensor data from the Arduino to the Serial Monitor

Here are the main themes from Part TWO:

  • Stage Six:......A simple Processing Sketch
  • Stage Seven: Arduino and Processing join forces for more fun
  • Stage Eight: A simple project that shows Serial communication from Arduino to Processing

In Part Three - we will reverse the direction of communication and get Processing to send data to the Arduino via a USB cable,

  • Stage Nine: A simple processing sketch that switches an LED on the Arduino
  • Stage Ten:  A processing sketch that reads from a text file
  • Stage Eleven: A processing sketch that reads data from a text file and sends to the Arduino
  • Stage Twelve: A processing sketch that trasmits data from a file to another Arduino via an XBee module.



Stage Nine - Using your computer to switch an LED

In this stage we create a simple Arduino sketch which will receive a simple command from the Processing Sketch to switch an LED. The Processing sketch will allow you to turn an LED on/off by clicking on the Processing Application window. It will detect the press of the mouse, and will send a command to the Arduino via the USB Serial COM port.

Parts Required:
  • Arduino UNO or compatible board
  • USB cable
  • Arduino and Processing IDE (on computer)
Arduino Logo The 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
style="color: rgb(0, 128, 0);">/* ===================================================
style="color: rgb(0, 128, 0);">Simple number reader: Written by ScottC - 07 Apr 2013
style="color: rgb(0, 128, 0);"> Arduino Version: 1.04
style="color: rgb(0, 128, 0);">======================================================*/

style="color: rgb(0, 128, 0);">// The onboard LED is on pin # 13
style="color: rgb(43, 145, 175);">int onboardLED = 13;


style="color: rgb(43, 145, 175);">void setup() {
style="color: rgb(0, 128, 0);">// Begin Serial communication
Serial.begin(9600);

style="color: rgb(0, 128, 0);">//Set the onboard LED to OUTPUT
pinMode(onboardLED, OUTPUT);
}

style="color: rgb(43, 145, 175);">void loop(){
style="color: rgb(0, 128, 0);">/* Read serial port, if the number is 0, then turn off LED
style="color: rgb(0, 128, 0);"> if the number is 1 or greater, turn the LED on. */
style="color: rgb(0, 0, 255);">while (Serial.available() > 0) {
style="color: rgb(43, 145, 175);">int num=Serial.read()-style="color: rgb(163, 21, 21);">'0';
style="color: rgb(0, 0, 255);">if(num<1){
digitalWrite(onboardLED, LOW); style="color: rgb(0, 128, 0);">//Turn Off LED
} style="color: rgb(0, 0, 255);">else{
digitalWrite(onboardLED, HIGH); style="color: rgb(0, 128, 0);">//Turn On LED
}
}
}


Processing icon The 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
style="color: rgb(0, 128, 0);">/*===========================================================
style="color: rgb(0, 128, 0);"> Toggle Switch: Send Number to Arduino
style="color: rgb(0, 128, 0);"> Written by Scott C on 07 Apr 2013
style="color: rgb(0, 128, 0);"> Processing Version: 2.0b8
style="color: rgb(0, 128, 0);">=============================================================*/

import processing.serial.*;

Serial comPort;
boolean ledState=style="color: rgb(0, 0, 255);">false; style="color: rgb(0, 128, 0);">//LED is off

style="color: rgb(43, 145, 175);">void setup(){
style="color: rgb(0, 128, 0);">//Open COM Port for Communication
comPort = style="color: rgb(0, 0, 255);">new Serial(style="color: rgb(0, 0, 255);">this, Serial.list()[0], 9600);
background(255,0,0); style="color: rgb(0, 128, 0);">//Start with a Red background
}

style="color: rgb(43, 145, 175);">void draw(){
}


style="color: rgb(43, 145, 175);">void mousePressed() {
style="color: rgb(0, 128, 0);">//Toggle led ON and OFF
ledState=!ledState;

style="color: rgb(0, 128, 0);">//If ledState is True - then send a value=1 (ON) to Arduino
style="color: rgb(0, 0, 255);">if(ledState){
background(0,255,0); style="color: rgb(0, 128, 0);">//Change the background to green

style="color: rgb(0, 128, 0);">/*When the background is green, transmit
style="color: rgb(0, 128, 0);"> a value=1 to the Arduino to turn ON LED */
comPort.write(style="color: rgb(163, 21, 21);">'1');
}style="color: rgb(0, 0, 255);">else{
background(255,0,0); style="color: rgb(0, 128, 0);">//Change background to red
comPort.write(style="color: rgb(163, 21, 21);">'0'); style="color: rgb(0, 128, 0);">//Send "0" to turn OFF LED.
}
}




The Video


Stage Ten: Reading from a Text File

We are now going to give the Arduino a rest (for a moment) and concentrate on a Processing Sketch that will read from a text file. Once we learn this skill, we can then build this Processing functionality into our Arduino Projects. Reading from a text file in Processing is actually quite easy if you use the loadStrings()method. However, it is best if you make things easy for yourself by using delimiters. The most common delimitter is a "comma". The comma allows the computer to group information according to your needs.
  • 11,22,33,44,55,66
  • 1,1,2,2,3,3,4,4,5,5,6,6
  • 112,223,334,455,566
The examples above contain the same numbers but are delimitted in different ways.
We are going to import a few different numbers/letters and store them in an array. We will then iterate through the array to display the values within.
So let us now create the text file. Copy and paste the following text into notepad and save the file, but remember where you save it, because we will need to know location and the name of the file in order to read from in.

NotepadIconCopy and Paste into Notepad:
100,200,A,B,C,10.2,0.1,wx,yz,arduinobasics





Save the file
I am going to call my file data.txt, and will be saving it to my D drive, so the file will be located here:
  • D:/data.txt

We will now create the processing sketch to read the text file and display the data on the screen.
We will use the comma delimiters to separate the data so that it displays in the following way:




Processing icon The 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
style="color: rgb(0, 128, 0);">/* =============================================================
style="color: rgb(0, 128, 0);"> ReadTextFile and Display on Screen:
style="color: rgb(0, 128, 0);"> Written by ScottC on 15th April 2013 using
style="color: rgb(0, 128, 0);"> Processing version 2.0b8
style="color: rgb(0, 128, 0);">
style="color: rgb(0, 128, 0);"> Website: http://arduinobasics.blogspot.com/
style="color: rgb(0, 128, 0);"> Projects: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
style="color: rgb(0, 128, 0);">
style="color: rgb(0, 128, 0);"> References:
style="color: rgb(0, 128, 0);"> Displaying Text: http://processing.org/reference/text_.html
style="color: rgb(0, 128, 0);"> Reading Text File: http://processing.org/reference/loadStrings_.html
style="color: rgb(0, 128, 0);">=================================================================*/

style="color: rgb(43, 145, 175);">void setup(){
size(180,250);
background(255);

style="color: rgb(0, 128, 0);">/* Read the text file */
String[] lines = loadStrings(style="color: rgb(163, 21, 21);">"D:/data.txt");
style="color: rgb(43, 145, 175);">int numOfLines=lines.length;
style="color: rgb(0, 0, 255);">for(style="color: rgb(43, 145, 175);">int i=0;i<numOfLines;i++){

style="color: rgb(0, 128, 0);">/* Split the data based on a "," delimiter. */
String[] data = splitTokens(lines[i], style="color: rgb(163, 21, 21);">",");
style="color: rgb(43, 145, 175);">int dataCount = data.length;

style="color: rgb(0, 0, 255);">for(style="color: rgb(43, 145, 175);">int j=0; j<dataCount; j++){
style="color: rgb(0, 128, 0);">/* Set the size and colour of the text */
textSize(16);
fill(100,100,255,50+(j*20));

style="color: rgb(0, 128, 0);">/* Display the text on the screen */
text(data[j],10,16+(16*j));
}
}
}


style="color: rgb(43, 145, 175);">void draw(){
}

The code above has the ability to display data from multiple lines within the text file, however for simplicity, I have chosen to use a single line. If I wanted to display more than one line, I would have to change the "for-loops".



Stage Eleven: Read Text File and send to Arduino

In stage 10 we used the Processing  programming language to import a line of data from a text file, break-up the line into pieces (based on comma delimiters) and then displayed the data on the Computer Screen. We will now use this knowledge and take it one step further. We will create a text file, import the data using processing, but this time we will send the data to the Arduino. Meanwhile the Arduino will be waiting patiently for this data, and once it receives the data, it will react according to our needs. We are going to keep this simple. The goal is to send two different letters from the Computer to the Arduino. One letter will turn an LED on, and the other letter will turn the LED off. We will also send an integer to tell the Arduino how long to keep the LED on or off.

GOAL:  Turn an LED on and off by reading a text file.
Our first step in this process is to create a text file that will store our important data. We will store two variables in this file. The first variable will be used to tell the Arduino whether we want to turn the LED on or whether we want to turn the LED off. We will use the letter "O" to turn the LED on, and use the letter "X" to turn the LED off.
The second variable will be a time based variable. It will be used to tell the Arduino "how long" to keep the LED on or off. We will store this variable as an integer and will represent time in "milliseconds".
  • 1000 milliseconds = 1 second
It makes sense to keep these two variables as a pair, however we will separate them using a comma delimitter. We will separate each command by putting the variables on a new line. Copy and paste the following data into notepad (or equivalent text editor), and save the file to your harddrive. I have saved this file as

  • D:/LEDdata.txt

NotepadIconText File Data:Here is the data to put into your text file (notepad):
X,50
O,45
X,40
O,35
X,30
O,25
X,20
O,15
X,10
O,5
X,10
O,15
X,20
O,25
X,30
O,35
X,40
O,45
X,50
O,55
X,60
O,65
X,70
O,75
X,80
O,85
X,90
O,95
X,100
O,200
X,200
O,200
X,500
O,500
X,500
O,500
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,200
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,200
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,200
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20
O,20
X,20



We will now set up the Arduino to accept data from the Computer and react to the Letters
  • "O" to turn the LED on
  • "X" to turn the LED off
  • "E" will be used to test for a successful Serial connection.
We will also get the Arduino to interpret the second variable which will be used to set the amount of time to keep the LED on or off.


Arduino LogoArduino Code:
 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
style="color: rgb(0, 128, 0);">/* Read TextFile Data: Written by ScottC on 24 April 2013
style="color: rgb(0, 128, 0);"> Arduino IDE version: 1.0.4
style="color: rgb(0, 128, 0);"> http://arduinobasics.blogspot.com.au/2013/04/serial-communication-tutorial-part-3.html
style="color: rgb(0, 128, 0);">*/

style="color: rgb(0, 128, 0);">/* Global Variables */
byte byteRead; style="color: rgb(0, 128, 0);">//Used to receive data from computer.
style="color: rgb(43, 145, 175);">int timeDelay; style="color: rgb(0, 128, 0);">//time that the LED is On or Off
style="color: rgb(43, 145, 175);">int maxtimeDelay=10000; style="color: rgb(0, 128, 0);">//Maximum time delay = 10 seconds
style="color: rgb(43, 145, 175);">int ledPin=13; style="color: rgb(0, 128, 0);">//LED connected to pin 13 on Arduino UNO.

style="color: rgb(43, 145, 175);">void setup() {
style="color: rgb(0, 128, 0);">//Set pin 13 (ledPin) as an output
pinMode(ledPin, OUTPUT);
style="color: rgb(0, 128, 0);">// Turn the Serial Protocol ON
Serial.begin(9600);
}

style="color: rgb(43, 145, 175);">void loop() {
style="color: rgb(0, 128, 0);">/* check if data has been sent from the computer: */
style="color: rgb(0, 0, 255);">if (Serial.available()) {
style="color: rgb(0, 128, 0);">/* read the most recent byte */
byteRead = Serial.read();

style="color: rgb(0, 0, 255);">switch (byteRead) {
style="color: rgb(0, 0, 255);">case 69: style="color: rgb(0, 128, 0);">//This is an enquiry, send an acknowledgement
Serial.println(style="color: rgb(163, 21, 21);">"A");
style="color: rgb(0, 0, 255);">break;
style="color: rgb(0, 0, 255);">case 79: style="color: rgb(0, 128, 0);">//This is an "O" to turn the LED on
digitalWrite(ledPin, HIGH);
style="color: rgb(0, 0, 255);">break;
style="color: rgb(0, 0, 255);">case 88: style="color: rgb(0, 128, 0);">//This is an "X" to turn the LED off
digitalWrite(ledPin, LOW);
style="color: rgb(0, 0, 255);">break;
style="color: rgb(0, 0, 255);">case 46: style="color: rgb(0, 128, 0);">//End of line
style="color: rgb(0, 128, 0);">//Make sure time delay does not exceed maximum.
style="color: rgb(0, 0, 255);">if(timeDelay > maxtimeDelay){
timeDelay=maxtimeDelay;
}
style="color: rgb(0, 128, 0);">//Set the time for LED to be ON or OFF
delay(timeDelay);
Serial.println(style="color: rgb(163, 21, 21);">"S");
timeDelay=0; style="color: rgb(0, 128, 0);">//Reset timeDelay;
style="color: rgb(0, 0, 255);">break;
style="color: rgb(0, 0, 255);">default:
style="color: rgb(0, 128, 0);">//listen for numbers between 0-9
style="color: rgb(0, 0, 255);">if(byteRead>47 && byteRead<58){
style="color: rgb(0, 128, 0);">//number found, use this to construct the time delay.
timeDelay=(timeDelay*10)+(byteRead-48);
}
}
}
}

Our next step is to import the data in the text file into Processing and then send the data to the Arduino. You may want to review Stage 10 of this tutorial for another example of importing text file data into Processing. You may also want to review stage 7 which shows how to receive data from an Arduino.
We will import all of the data from the file when we push a button on the Processing Window, and send this data to the Arduino via the USB cable that is connected to the computer. We are going to use the same COM port that the Computer uses to upload Arduino Sketches, therefore it is important that you close the Arduino Serial Monitor before you run the processing sketch, otherwise you will get an error which states that the COM port is not available.


Processing iconProcessing Code:
 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
style="color: rgb(0, 128, 0);">/* TextFile Data sender (Stage 11) 
style="color: rgb(0, 128, 0);"> Written by ScottC on 24th April 2013
style="color: rgb(0, 128, 0);"> using Processing Version 2.0b8
style="color: rgb(0, 128, 0);">
style="color: rgb(0, 128, 0);"> The full tutorial can be found here:
style="color: rgb(0, 128, 0);"> http://arduinobasics.blogspot.com/2013/04/serial-communication-tutorial-part-3.html
style="color: rgb(0, 128, 0);">*/

import processing.serial.*;

Serial comPort; style="color: rgb(0, 128, 0);">//The com port used between the computer and Arduino
style="color: rgb(43, 145, 175);">int counter=0; style="color: rgb(0, 128, 0);">// Helps to keep track of values sent.
style="color: rgb(43, 145, 175);">int numItems=0; style="color: rgb(0, 128, 0);">//Keep track of the number of values in text file
String comPortString; style="color: rgb(0, 128, 0);">//String received From Arduino
String textFileLines[]; style="color: rgb(0, 128, 0);">//Array of text file lines
String lineItems[]; style="color: rgb(0, 128, 0);">//Array of line items

style="color: rgb(43, 145, 175);">void setup(){
comPort = style="color: rgb(0, 0, 255);">new Serial(style="color: rgb(0, 0, 255);">this, Serial.list()[0], 9600); style="color: rgb(0, 128, 0);">//Setup the COM port
comPort.bufferUntil(style="color: rgb(163, 21, 21);">'\n'); style="color: rgb(0, 128, 0);">//Generate a SerialEvent when a newline is received
background(255,0,0); style="color: rgb(0, 128, 0);">//Start with a Red background
}

style="color: rgb(0, 128, 0);">/* Draw method is not used in this sketch */
style="color: rgb(43, 145, 175);">void draw(){
}

style="color: rgb(0, 128, 0);">//When the mouse is pressed, write an "E" to COM port.
style="color: rgb(0, 128, 0);">//The Arduino should send back an "A" in return. This will
style="color: rgb(0, 128, 0);">//generate a serialEvent - see below.
style="color: rgb(43, 145, 175);">void mousePressed() {
comPort.write(style="color: rgb(163, 21, 21);">"E");
}

style="color: rgb(43, 145, 175);">void serialEvent(Serial cPort){
comPortString = cPort.readStringUntil(style="color: rgb(163, 21, 21);">'\n');
style="color: rgb(0, 0, 255);">if(comPortString != null) {
comPortString=trim(comPortString);

style="color: rgb(0, 128, 0);">/*If the String received = A, then import the text file
style="color: rgb(0, 128, 0);"> change the background to Green, and start by sending the
style="color: rgb(0, 128, 0);"> first line of the text file to the Arduino */
style="color: rgb(0, 0, 255);">if(comPortString.equals(style="color: rgb(163, 21, 21);">"A")){
textFileLines=loadStrings(style="color: rgb(163, 21, 21);">"D:/LEDdata.txt");
background(0,255,0);
sendLineNum(counter);
}

style="color: rgb(0, 128, 0);">/*If the the String received = S, then increment the counter
style="color: rgb(0, 128, 0);"> which will allow us to send the next line in the text file.
style="color: rgb(0, 128, 0);"> If we have reached the end of the file, then reset the counter
style="color: rgb(0, 128, 0);"> and change the background colour back to red. */
style="color: rgb(0, 0, 255);">if(comPortString.equals(style="color: rgb(163, 21, 21);">"S")){
counter++;
style="color: rgb(0, 0, 255);">if(counter > (textFileLines.length-1)){
background(255,0,0);
counter=0;
} style="color: rgb(0, 0, 255);">else {
sendLineNum(counter);
}
}
}
}


style="color: rgb(0, 128, 0);">/*The sendLineNum method is used to send a specific line
style="color: rgb(0, 128, 0);"> from the imported text file to the Arduino. The first
style="color: rgb(0, 128, 0);"> line item tells the Arduino to either switch the LED on or off.
style="color: rgb(0, 128, 0);"> The second line item, tells the Arduino how long to keep the
style="color: rgb(0, 128, 0);"> LED on or off. The full-stop is sent to the Arduino to indicate
style="color: rgb(0, 128, 0);"> the end of the line. */

style="color: rgb(43, 145, 175);">void sendLineNum(style="color: rgb(43, 145, 175);">int lineNumber){
lineItems=splitTokens(textFileLines[lineNumber],style="color: rgb(163, 21, 21);">",");
comPort.write(lineItems[0]);
comPort.write(lineItems[1]);
comPort.write(style="color: rgb(163, 21, 21);">".");
}


Stage Twelve: To be continued..


I need to finish my XBee tutorial before doing this stage. But am just about to start studying again. So this stage probably won't get completed for another couple of months. But I hope there is enough content to keep you satisfied for the time being.