lilypad accelerometer + Arduino Duemilanove + Xbee

hi,

i want to connect the lilypad accelerometer to the Arduino Duemilanove. And then i want to send the data wirelessly.

Is it possible to connect the lilypad accelerometer to a arduino Duemilanove? Or is it only compitable with the lilypad arduino?

or is there a better option

i thought it would be a good idea, because i don't have to adjust anything and easy interface to the arduino D.

:)

Is it possible to connect the lilypad accelerometer to a arduino Duemilanove?

Yes.

Back again. I want to do this:http://i46.tinypic.com/2j67eol.jpg
(bit crappy i know, but you get the picture)

I’m going to try to get it working without the xbee first. After this i want to put the xbee between it.

I’m going to send the data tru serial to Processing and influence video (QT movie) with it.

I have this connected right now.
these are the force sensors i bought:
http://www.watterott.com/de/Drucksensor-127mm

Do i need something between the multiplexer and the force sensors? I want to have a wide range of values. It’s made for 100g till 10 kilogram. Do i need to put a capacitor between it?and how do i interper the information i get from the arduino tru serial?

my values are now like this:
205.00
743.00
817.00
279.00
1023.00
0.00
815.00
856.00
210.00

int led = 11;    //just a led
int analogIn = 0;
float analogValue;
int outputValue = 0;        // value output to the PWM (analog out)


void setup() {
  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
  
  Serial.begin(9600);
}

void loop () {
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  
  analogValue = analogRead(analogIn);
  outputValue = map(analogValue, 250, 900, 0, 255); 

  analogWrite(led, analogValue);
     if (outputValue<5) 
     analogWrite(led, 0); 
    else
     analogWrite(led, outputValue); 
 
 
 
   // print the result to the serial monitor
   Serial.print(analogValue);
   Serial.print(" : ");
   Serial.print(outputValue);
   Serial.println("----");
 
 
 
   // wait 10 milliseconds for the analog-to-digital converter
   delay(10);


}

:slight_smile:

  analogWrite(led, analogValue);

Since analogValue is upper-bounded to 1023, it's not a particularly good idea to pass that value to analogWrite, which is expecting a value in the range 0 to 255.

my values are now like this: 205.00 743.00 817.00 279.00 1023.00 0.00 815.00 856.00 210.00

Are those the analogValue values? What are you doing to the force sensor?

hi, sorry i forgot to tell this:

i' ve tested it with this script with one force sensor:

int led = 11;    //just a led
int analogIn = 0;
float analogValue;
int outputValue = 0;        // value output to the PWM (analog out)


void setup() {

  Serial.begin(9600);
}

void loop () {

  
  analogValue = analogRead(analogIn);

   Serial.print(analogValue);
Serial.println(" ");




   // wait 10 milliseconds for the analog-to-digital converter
   delay(100);


}

it looks like this with this code: 513.00 1023.00 0.00 810.00 915.00 140.00 1023.00 0.00 624.00 968.00 0.00

When i touch it, it becomes 0.

Numbers all over the place like that tell me that you don't have the force sensor wired up correctly. Can you post a picture or a schematic?

Now it's like this

arduino GRND>force sensor left pin>force sensor right pin>arduino pin 0

i've switched the cables, i get this, still shit: 12.00 800.00 544.00 557.00 926.00 89.00 956.00

or must it have a current first? Or don't i understand the principle of a force sensor. At first i just tried with a led, worked perfect. Can i break a force sensor easilly?

A force sensor is like a potentiometer, with one of the end pins missing. You need another resistor, to form a voltage divider.

+V ---\/\/\/\/----+-----\/\/\/\/----- Gnd (force | (fixed sensor) | resistor) v analog pin

The fixed resistor should be approximately the same resistance as the maximum resistance of the force sensor.

thanks, i get a nice value from 0 - 1000 right now with a 10K ohm resistor. Thanks for your help! Hopefully the 4051 will be easy. Now i can finish my test setup and start thinking about the Xbee.

UPDATE:

okay, my first test run. Everything is connected correct as far as i can see.

Hi, i have another question. You talked about high levels of analog write. So 1023 is too high, could i damage my arduino with this setup?

The output from analogRead should be mapped from the actual or expected range to the actual input range expected by digitalWrite:

int valRead = analogRead(analogPin);
int mappedVal = map(valRead, 0, 1023, 0, 255);
analogWrite(pwmPin, mappedVal);

If you KNOW that, for your sensor, the output value will never be less than, for instance, 218, and never higher than, for instance, 712, you could use these values in the map call:

int mappedVal = map(valRead, 218, 712, 0, 255);

The analogWrite command will not damage the chip or the pin if a value higher than 255 is supplied as the second argument. The actual behavior could be determined, but it is subject to change, and it's current behavior for out-of-bounds input should not be relied on.

okay, i now get a nice reading of the first force sensor. So how do i make the 4051 switch between values? It’s pretty difficult to understand. i’ve read allot allready about it. But if i look at the example. I see that it switches to all analog in’s on the 4051 and then starts over again. (atleast that’s what i think :slight_smile: ) Or are they selected by the digital in’s?

int led = 11;    //just a led
int analogIn = 0;
float analogValue;
int outputValue = 0;        // value output to the PWM (analog out)


void setup() {
  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
  
  Serial.begin(9600);
}

void loop () {
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  
  analogValue = analogRead(analogIn);
 
  int mappedVal = map(analogValue, 218, 712, 0, 255);

  analogWrite(led, analogValue);
     if (outputValue<5)
     analogWrite(led, 0);
    else
     analogWrite(led, outputValue);



   // print the result to the serial monitor
   Serial.print(analogValue);
   Serial.print(" : ");
   Serial.print(outputValue);
   Serial.println("----");



   // wait 10 milliseconds for the analog-to-digital converter
   delay(10);


}

with this code i only get a reading from S1. What should i do now to get all the readings?

with this?

void loop () {

  for (count=0; count<=7; count++) {
    row = bin[count];      
    r0 = row & 0x01;
    r1 = (row>>1) & 0x01;
    r2 = (row>>2) & 0x01;
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    digitalWrite(4, r2);
    //Serial.println(bin[count]);
    delay (1000);
  }

thank you for helping me PaulS

Hi, i'm back again. I am now trying to send data from one arduino to the other tru two xbees. But what i get on the other side is wrong. Could somebody check my code? my setup is: send: freeduino> xbee shield> XBee 1mW Wire Antenna> lilypad acc. meter

receive: arduino>xbee shield

Receive

// RECIEVER

byte incomingByte, sensor1, sensor2,sensor3;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// led pins

pinMode (5, OUTPUT);
pinMode (6, OUTPUT);
pinMode (7, OUTPUT);
delay(1000);

}

void loop() {
if (Serial.available()) {   // are there any bytes available on the serial port ???

// assign bytes to the var [ch65533]incomingByte[ch65533]
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess   :)

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();

}

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();

}

if ((int(incomingByte) == 256)) {
sensor2 = Serial.read();

}
Serial.print("Sensor 1 = ");
Serial.print(int(sensor1));
Serial.println(" ");
Serial.print("  Sensor 2 = ");
Serial.print(int(sensor2));
Serial.println(" ");
Serial.print("  Sensor 3 = ");
Serial.print(int(sensor2));
Serial.println(" ");
}

analogWrite (5, sensor1);
analogWrite (6, sensor2);
analogWrite (7, sensor3);
}

send

// SENDER

int analogValue2, analogValue3, analogValue4, val2,val3, val4;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
analogValue2 = analogRead(2); //X
analogValue3 = analogRead(4); //Y
analogValue4 = analogRead(5); //Z

// remap values

val2 = map(analogValue2, 0, 1023, 253, 0);  // 254 and 255 for SYNC
val3 = map(analogValue3, 0, 1023, 253, 0);
val4 = map(analogValue4, 0, 1023, 253, 0);

Serial.print(254, BYTE); //SYNC char
Serial.print(val2, BYTE);

Serial.print(255, BYTE); //SYNC char
Serial.print(val3, BYTE);

Serial.print(256, BYTE); //SYNC char
Serial.print(val4, BYTE);



delay(150);
}
if ((int(incomingByte) == 256)) {

The value returned by Serial.read is a byte in an int variable. The reason that is done is so that -1 can be returned if there is no data. Otherwise, the value is in the range 0 to 255. Last time I checked, 256 was not in that range.

aah, yes, i've changed that to this:

// RECIEVER

byte incomingByte, sensor1, sensor2,sensor3;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println("Ready!");

// led pins
//pinMode (5, OUTPUT);
//pinMode (5, OUTPUT);
//pinMode (6, OUTPUT);

delay(1000);

delay(100);

}

void loop() {
if (Serial.available()) {   // are there any bytes available on the serial port ???

// assign bytes to the var [ch65533]incomingByte[ch65533]
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess   :)

if ((int(incomingByte) == 253)) {
sensor1 = Serial.read();

}

if ((int(incomingByte) == 254)) {
sensor2 = Serial.read();

}

if ((int(incomingByte) == 255)) {
sensor3 = Serial.read();

}

Serial.print(int(sensor1));
  Serial.print("\t");

Serial.print(int(sensor2));
  Serial.print("\t");
Serial.print(int(sensor3));
  Serial.print("\t");
Serial.println(" ");
}
//analogWrite (5, sensor1);
//analogWrite (6, sensor2);
//analogWrite (6, sensor3);
}
int analogValue0, analogValue1, analogValue2, val0,val1, val2;
const int xPin = 0;                   // x-axis of the accelerometer
const int yPin = 1;                   // y-axis
const int zPin = 2;                   // z-axis (only on 3-axis models)

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(zPin, INPUT);
}

void loop()
{
// read analog input
analogValue0 = analogRead(xPin);
analogValue1 = analogRead(yPin);
analogValue2 = analogRead(zPin);

val0 = map(analogValue0, 0, 1023, 252, 0);  // 253, 254 and 255 for SYNC
val1 = map(analogValue1, 0, 1023, 252, 0);
val2 = map(analogValue2, 0, 1023, 252, 0);
// remap values

Serial.print(253, BYTE); //SYNC char
Serial.print(val0 , BYTE);

Serial.print(254, BYTE); //SYNC char
Serial.print(val1, BYTE);

Serial.print(255, BYTE); //SYNC char
Serial.print(val2, BYTE);

delay(150);
}

somehow it doesn't get the data correctly. I've used the xbee's out of the box. I've read that the standaard settings is enough for xbee's

but still no correct signal

i get this:

2540 152 0 2540 152 0 2300 152 0 1520 152 0 2540 134 0 2540 158 0 2540 152 0 2540 24 0 2540 255 0 2540 24 0 2540 255 0 2540 134 0 2540 158 0 2540 152 0

when i print within the if statement, i see that it sticks at sensor 1

help??

The Serial.available function returns an integer value that tells you how many bytes are available to read, not a boolean that says that you can read as much as you want.

When used the way that you are using it, there will be at least one byte to read.

So, you proceed to read two bytes.

If it were me, I’d use a different method to send the data. On the sender, I’d send something like “<750, 600, 54>” for the three sensor readings (no mapping, and send the data as text).

Then, on the receiver, look for the start of packet marker (<) and read into a character array until the end of packet marker (>) arrives.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273230655/2#2

Then, use strtok to parse the string to get the 3 values as individual strings, and then use atoi to convert the string to an integer.

hi, i’m going further with this project. I’m skipping the multiplexer for a better time. The data from the sender:

int xpin = 0; // select the input pin for the sensor
int ypin = 1; // select the input pin for the sensor
int zpin = 2; // select the input pin for the sensor



void setup(){

  pinMode(1, OUTPUT);    // s0
  pinMode(1, OUTPUT);    // s1
  pinMode(2, OUTPUT);    // s2
  
Serial.begin(19200);
}

void loop () {
  //XYZ as
  xval = analogRead(xpin); // read the value from the x axis, 0-1023
yval = analogRead(ypin); // read the value from the y axis, 0-1023
zval = analogRead(zpin); // read the value from the z axis, 0-1023

  displayvalues();
  
  delay (100);
}




void displayvalues(){

Serial.print("<");
Serial.print(xval); 
 Serial.print(",");
 Serial.print(yval); 
 Serial.print(",");
  Serial.print(zval);
   Serial.print(",");
 Serial.print(analogRead(3)); 
    Serial.print(",");
 Serial.print(analogRead(4));
    Serial.print(",");
 Serial.print(analogRead(5));
 Serial.print(">");

}

As you said… i must send it as normal ASCII text. I did this with this script. The serial says that the data is coming inside when i connect the arduino.

receive code

char inData[10];
int index;
boolean started = false;
boolean ended = false;
void setup(){

Serial.begin(19200);
}
void loop()
{
   while(Serial.available() > 0)
   {
     //Serial.println("ready");
       char aChar = Serial.read();
Serial.println(aChar);
       if(aChar == '<')
       {
     Serial.println("haakje");
           started = true;
           index = 0;
           inData[index] = ',';
       }
       else if(aChar == '>')
       {
           ended = true;
       }
       else if(started)
       {
           inData[index] = aChar;
           index++;
           inData[index] = ',';
          }
   }
  

   if(started && ended)
   {
       // Convert the string to an integer
       int inInt = atoi(inData);
          Serial.println(inInt);
       // Use the value

       // Get ready for the next time
       started = false;
       ended = false;

       index = 0;
       inData[index] = ',';
  
 }
}

i receive this when converted to char:

à
?
à
?
ø
þ
?
f
?
þ
?
à
?
à
?

am i sending/receiving it wrong? Must i send it like char?

i’m trying it now tru processing.So it’s arduino>xbee xbee>arduino>processing

It works without cable perfectly, i was expecting it to work with too, but it isn’t
sender:

int xpin = 0; // select the input pin for the sensor
int ypin = 1; // select the input pin for the sensor
int zpin = 2; // select the input pin for the sensor
int xval = 0;
int yval = 0;
int zval = 0;

void setup(){

  pinMode(1, OUTPUT);    // s0
  pinMode(1, OUTPUT);    // s1
  pinMode(2, OUTPUT);    // s2
  
Serial.begin(19200);
}

void loop () {
  //XYZ as
  xval = analogRead(xpin); // read the value from the x axis, 0-1023
yval = analogRead(ypin); // read the value from the y axis, 0-1023
zval = analogRead(zpin); // read the value from the z axis, 0-1023

  displayvalues();
  
  delay (100);
}




void displayvalues(){

Serial.print("<");
Serial.print(xval); 
 Serial.print(",");
 Serial.print(yval); 
 Serial.print(",");
  Serial.print(zval);
   Serial.print(",");
 Serial.print(analogRead(3)); 
    Serial.print(",");
 Serial.print(analogRead(4));
    Serial.print(",");
 Serial.print(analogRead(5));
 Serial.print(">");

}

reciever

void setup(){

Serial.begin(19200);
}

void loop()
{
   while(Serial.available() > 0)
   {
     
     Serial.print(Serial.read());
   }
}

in processing

import java.io.*;
import processing.serial.*;
import processing.video.*;

Movie myMovie;

char[] inData =new char[0];
int index;
boolean started = false;
boolean ended = false;
String[] dataSplit = new String[0];
char[] dataChar = new char[0];
String[] x = new String[0];

// The serial port:
Serial myPort;

void setup() {
  size(400, 400);
  frameRate(30);
  println(Serial.list());
  myMovie = new Movie(this, "cube.mov");
  myMovie.loop();
  myPort = new Serial(this, Serial.list()[0], 19200);
  if(myMovie.available()) {
    myMovie.read();
  }
}

void draw() {
  while (myPort.available() > 0) {
    int inByte = myPort.read();

    char charData1 = char(inByte);
    if (charData1 == '>'){
    }
    else{
      if(charData1 == '<'){
      }
      else{
        dataChar = append(dataChar, char(charData1)) ;
      }
    }
    if (charData1 == '>'){
      String str = new String(dataChar);
      String[] p = split(str, "<");
      x = split(str, ",");
      println(x);
      dataChar = new char[0];
    }
    image(myMovie, 0, 0);
  }

  float movieDuration = myMovie.duration();

  float minVal = 250;
  float splitMov = movieDuration/minVal;
  if (x.length >4){
    int pres1 = int(x[3]);
    int pres2 = int(x[4]);
    int pres3 = int(x[5]);
    int rot1 = int(x[0])-int(minVal);
    int rot2 = int(x[1])-int(minVal);
    int rot3 = int(x[2])-int(minVal);
    float speed = minVal/4;

    if (pres1 > int(10)){
      myMovie.jump(int(splitMov*rot1));
    }
    if (pres2 > int(10)){
      myMovie.speed(random(2));
    }
    if (pres3 > int(10)){         
      myMovie.frameRate(int(splitMov*rot3));

    }
    x = new String[0];
  }



}

what the processing sketch does is receive information. Putting it into an String array and makes it react to the sensors :slight_smile:

could somebody tell my why the xbee doesn’t send/receive the text as normal text, but fucks it up?

SOLVED!

i’ve solved the xbee problem by changing the bitrate to 9600 and changing the , to spaces. For insurance i’ve changed the < and > to L and H. It works perfectly!
sender

int xpin = 0; // select the input pin for the sensor
int ypin = 1; // select the input pin for the sensor
int zpin = 2; // select the input pin for the sensor
int xval = 0;
int yval = 0;
int zval = 0;

void setup(){

  pinMode(1, OUTPUT);    // s0
  pinMode(1, OUTPUT);    // s1
  pinMode(2, OUTPUT);    // s2
  
Serial.begin(9600);
}

void loop () {
  //XYZ as
  xval = analogRead(xpin); // read the value from the x axis, 0-1023
yval = analogRead(ypin); // read the value from the y axis, 0-1023
zval = analogRead(zpin); // read the value from the z axis, 0-1023

Serial.print("L");

Serial.print(xval); 
 Serial.print(" ");
 Serial.print(yval); 
 Serial.print(" ");
  Serial.print(zval);
   Serial.print(" ");
 Serial.print(analogRead(3)); 
    Serial.print(" ");
 Serial.print(analogRead(4));
    Serial.print(" ");
 Serial.print(analogRead(5));
 Serial.println("H");
  
  delay (100);
}

receiver

void setup(){

  Serial.begin(9600);
}
void loop()
{
  while(Serial.available() > 0)
  {

    Serial.print(char(Serial.read()));

  }
}

receiving information in Processing

import java.io.*;
import processing.serial.*;
import processing.video.*;

Movie myMovie;

char[] inData =new char[0];
int index;
boolean started = false;
boolean ended = false;
String[] dataSplit = new String[0];
char[] dataChar = new char[0];
String[] x = new String[0];

// The serial port:
Serial myPort;

void setup() {
  size(320, 200);
  frameRate(30);
  println(Serial.list());
  myMovie = new Movie(this, "cube.mov");
  myMovie.loop();
  myPort = new Serial(this, Serial.list()[2], 9600);
  if(myMovie.available()) {
    myMovie.read();
  }
}

void draw() {
  while (myPort.available() > 0) {
    int inByte = myPort.read();
//println(inByte);
    char charData1 = char(inByte);
 //   println(charData1);
    if (charData1 == 'H'){
    }
    else{
      if(charData1 == 'L'){
      }
      else{
        dataChar = append(dataChar, char(charData1)) ;
      }
    }
    if (charData1 == 'H'){
      String str = new String(dataChar);
      String[] p = split(str, "L");
      x = split(str, " ");
     // println(x);
      dataChar = new char[0];
    }
   
  }

}

My project is allmost finished. I’ll post the pictures here.

Hi everyone, this is my first post, sorry to bump an old thread! This may seem strange but I’m in a situation where everything is working, but I would like to ask WHY!

I have an accelerometer hooked up to an Arduino + XBee, which is communicating to another Ard+Xbee successfully. I had similar problems, in that the numbers from the sensor were not actually being transmitted, but were being converted into characters of some sort and then sent. Anyway, I finally managed to get it working great using PaulS’s idea of sending a string, and then using strtok to split it up, and then atoi to convert the string to a usable number.

I’m just confused as to how the char *token bit works. Is it an array? What does the asterisk denote? Do I need the brkb variable also? What job is that doing?

Here’s my code.

SENDING

int x, y, z; 

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



void loop() 
{
  
  // get sensor readings from 0-1023 - accelerometer seems
  // to return values between 270 and 420
  x = analogRead(0);
  y = analogRead(1);
  z = analogRead(2);
    
  // print to the serial port with special things in between
  // so that the receiver knows where to split them
  Serial.print("<");
  Serial.print(x);
  Serial.print(",");
  Serial.print(y);
  Serial.print(",");
  Serial.print(z);
  Serial.println(">");

  delay (500);
  
}

RECEIVING

char inData[13]; // array that will contain string characters
char inChar; // where to store the character that is read
int index = 0; // index for array, where to store things
boolean started = false;
boolean ended = false;


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




void loop()
{
   while(Serial.available() > 0) // don't read unless you know there's data in there
  {
      inChar = Serial.read(); //read a character

      if(inChar == '<') // the beginning of string
      {
        started = true;
        index = 0;
        inData[index] = '\0';
      }
      
      else if(inChar == '>')  // the end of string
      {
        ended = true;
      }
      
      else if(started)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
  }
  
  if(started && ended)  // if both are true then process string
  {
    
    // char indata[] contains the string
    // strtok_r splits the string into separate bits according to the delimiter
    // atoi then turns ascii strings into integers
    
    char *token;
    char *brkb; // do i need this?
    
    token = strtok_r(inData, "," ,&brkb);
    int x = atoi(token);
    Serial.print("xval is ");
    Serial.print(x);
    Serial.print(" twice that is "); // just to prove they are integers now
    Serial.println(x*2);
    
    token = strtok_r(NULL, "," ,&brkb);
    int y = atoi(token);
    Serial.print("yval is ");
    Serial.println(y);
    
    token = strtok_r(NULL, "," ,&brkb);
    int z = atoi(token);
    Serial.print("zval is ");
    Serial.println(z);
    
      
      // get ready for next time round
      started = false;
      ended = false;
      index = 0;
      inData[index] = '\0';
      
      
    }
  }

I should mention I am a novice in all respects when it comes to this stuff, but I’m really getting into it!