Wednesday, 21 May 2008

TouchShield: Serial Communications

Overview,
This shows you how to send the state of a pin from the Arduino to the TouchShield and display it's state on the OLED.

With the help of LadyAda's softwareSerial fix, I was able to communicate with the TouchShield via serial. This allows me to let the Arduino do the hard stuff like signal processing, freeing up the TouchShield to display the data and handle touch events.





Arduino,
Sending a message....

First, fix software serial bit timing by downloading ladyada's softwareSerial2

Define constants and display global variables

#include "AFSoftSerial.h"
#define RXPIN 3

#define TXPIN 2
AFSoftSerial mySerial = AFSoftSerial(RXPIN,TXPIN);


The next block of code is the setup routine. This is where I'll make the Arduino's pin an input and set the baud rate to 9600.

void setup()
{

pinMode(RXPIN, INPUT);
pinMode(TXPIN, OUTPUT);
mySerial.begin(9600);

pinMode(9,INPUT);
}


In the main loop, I read the state of the pin and Transmit a byte to the TouchShield.

void loop()
{
mySerial.print((unsigned char)digitalRead(9));
delay(100);
}

TouchShield,
Reading a byte and displaying the state...

Initialize the TouchShield's serial to 9600. Remember, the Arduino has a small delay before it exits it's bootloader.

COLOR blue = {0,0,255};
COLOR black = {0,0,0};

void setup()
{
Serial.begin(9600);
}

void loop()
{

if (Serial.read() )
lcd_puts("HIGH",60,60,blue,black);
else
lcd_puts("LOW ",60,60,blue,black);

}



As you can see, because the communications interface is simple, you can create more complicated code elsewhere ;-)

0 comments:

Post a Comment