Wednesday, May 28, 2008

TouchShield 2-Axis Accelerometer Graphing

Here is the TouchShield and Arduino reading and graphing the output of a Parallax dual-axis accelerometer. This is first time I've played with one these things. They're so cool!




Drop in the graph-002 TouchShield library to give graphing functionality.


Here is the TouchShield code (touch):

#include
#include "math.h"
#include "string.h"

unsigned char x,xl,xh,i,yl,yh;
unsigned int adc;
unsigned int xacc;
unsigned int yacc;

char out[4];
COLOR blue = {0,0,255};
COLOR black = {0,0,0};
COLOR green = {0,255,0};
COLOR yellow = {255,255,0};

float scale = 0.00488;
float result,sum;


barGraph myBarX = barGraph("Xaccel",2,10,120,30);
barGraph myBarY = barGraph("Yaccel",2,70,120,30);


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

myBarX.setColor(green); myBarX.setPrecision(0); myBarX.setMinMax(0,400);
myBarX.refresh();

myBarY.setColor(yellow); myBarY.setPrecision(0); myBarY.setMinMax(0,400);
myBarY.refresh();

}


POINT p;

void loop()
{

xh = Serial.read();
delay(1);
xl = Serial.read();

delay(1);

yh = Serial.read();
delay(1);
yl = Serial.read();

adc = (xh << adc =" (yh">




And here is arduino (base):

#include

#define rxPin 3
#define txPin 2
#define xacc_pin 7
#define yacc_pin 6

unsigned int xacc,yacc;
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
unsigned char x;

void setup()
{
pinMode(xacc_pin,INPUT);
pinMode(yacc_pin,INPUT);
pinMode(rxPin,INPUT);
pinMode(txPin,OUTPUT);
mySerial.begin(9600);
Serial.begin(9600);
}

void loop()
{
xacc=0;
yacc=0;

delay(5);


while(digitalRead(xacc_pin) == LOW){;}
while(digitalRead(xacc_pin) == HIGH){;}
while(digitalRead(xacc_pin) == LOW){;}

while(digitalRead(xacc_pin) == HIGH)
xacc++;


mySerial.print((unsigned char)(xacc>>8));
delay(5);
mySerial.print((unsigned char)xacc);

delay(5);


while(digitalRead(yacc_pin) == LOW){;}
while(digitalRead(yacc_pin) == HIGH){;}
while(digitalRead(yacc_pin) == LOW){;}

while(digitalRead(yacc_pin) == HIGH)
yacc++;


mySerial.print((unsigned char)(yacc>>8));
delay(5);
mySerial.print((unsigned char)yacc);
Serial.print(yacc);
Serial.print("\r\n");


}



Feel free to change the graphing colors or graph style to make your application unique.

2 comments:

Aaron, Vicki Tunell said...

I'm having trouble using serial functions in the touchscreen. I'd like to send a string from the arduino, but the touchscreen won't receive a string, it automatically expects a number.
Plus, there's no API shown for the Serial system (i want to write from the touchpad, but get errors when i use Serial.write()

Unknown said...

Nice work.

Im a noob and I dont understand what you mean with "Drop in the graph library"

Where to drop?

Thanks