I so I have an accelerometer attached to a lilypad and am sending that data back over Xbee to control musical motifs...all this works fine. I wanted to add a string of LED's on a PWM output and just control the output via change in accelerometer data. This also works. I have this whole setup working on a seperate lilypad (for the arms) but I can't seem to get it working on the leg lilypad. If I disconnect and swap the rx/tx lines connecting the xbee the LEDs work but the xbee doesn't communicate (for obvious reasons) but then when I put the lines back in their original configuration the LED's won't light up via PWM and if I hardwire them just to ground and v+ on the lilypad board they turn on but the embedded notification LED (pin 13) flicker's rapidly and no data is sent over Xbee. I thought perhaps its a current draw thing? But like i said this whole config works perfectly fine on another lilypad. is this something where my lilypad is just fried? I'm going to try and operate a relay this morning with the system and see if that helps any.
Thanks!
Code is below as well as an attached visual diagram of the system sans LEDs.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX
//Analog read pins
const int right_xPin = 0;
const int right_yPin = 1;
const int right_zPin = 2;
const int left_xPin = 3;
const int left_yPin = 4;
const int left_zPin = 5;
int left_led = 9;
int right_led = 10;
int led = 13;
//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 265;
int maxVal = 402;
//to hold the caculated values
double left_x;
double left_y;
double left_z;
double right_x;
double right_y;
double right_z;
double left_sum;
int left_sum_prev = 0;
double right_sum;
int right_sum_prev = 0;
void setup(){
Serial.begin(57600);
pinMode(led, OUTPUT);
pinMode(right_led, OUTPUT);
pinMode(left_led, OUTPUT);
}
void loop(){
//read the analog values from the accelerometer
int left_xRead = analogRead(left_xPin);
int left_yRead = analogRead(left_yPin);
int left_zRead = analogRead(left_zPin);
//convert read values to degrees -90 to 90 - Needed for atan2
int left_xAng = map(left_xRead, minVal, maxVal, -90, 90);
int left_yAng = map(left_yRead, minVal, maxVal, -90, 90);
int left_zAng = map(left_zRead, minVal, maxVal, -90, 90);
//Caculate 360deg values like so: atan2(-yAng, -zAng)
//atan2 outputs the value of -? to ? (radians)
//We are then converting the radians to degrees
left_x = RAD_TO_DEG * (atan2(-left_yAng, -left_zAng) + PI);
left_y = RAD_TO_DEG * (atan2(-left_xAng, -left_zAng) + PI);
left_z = RAD_TO_DEG * (atan2(-left_yAng, -left_xAng) + PI);
//Output the caculations
//read the analog values from the accelerometer
int right_xRead = analogRead(right_xPin);
int right_yRead = analogRead(right_yPin);
int right_zRead = analogRead(right_zPin);
//convert read values to degrees -90 to 90 - Needed for atan2
int right_xAng = map(right_xRead, minVal, maxVal, -90, 90);
int right_yAng = map(right_yRead, minVal, maxVal, -90, 90);
int right_zAng = map(right_zRead, minVal, maxVal, -90, 90);
//Caculate 360deg values like so: atan2(-yAng, -zAng)
//atan2 outputs the value of -? to ? (radians)
//We are then converting the radians to degrees
right_x = RAD_TO_DEG * (atan2(-right_yAng, -right_zAng) + PI);
right_y = RAD_TO_DEG * (atan2(-right_xAng, -right_zAng) + PI);
right_z = RAD_TO_DEG * (atan2(-right_yAng, -right_xAng) + PI);
//sum of all angles
int right_sum = right_x + right_y + right_z;
//difference in angle sum from previous loop
int right_diff = right_sum - right_sum_prev;
//strobe statement, if the difference is over the 255 pwm ceiling then strobe the LEDs
if (right_diff > 255)
{
analogWrite(right_led, 255);
delay(50);
analogWrite(right_led, 0);
}
//else write the sum difference as a PWM value to the LED
else {
analogWrite(right_led, abs(right_diff) );
}
int left_sum = left_x + left_y + left_z;
int left_diff = left_sum - left_sum_prev;
if (left_diff > 255)
{
analogWrite(left_led, 255);
delay(50);
analogWrite(left_led, 0);
}
else
{ analogWrite(left_led, abs(left_diff) );
}
//set the prev sum for next loop iteration
right_sum_prev = right_sum;
left_sum_prev = left_sum;
//Output the caculations
Serial.print("LeftFoot ");
Serial.print(left_x);
Serial.print(" ");
Serial.print(left_y);
Serial.print(" ");
Serial.println(left_z);
Serial.print("RightFoot ");
Serial.print(right_x);
Serial.print(" ");
Serial.print(right_y);
Serial.print(" ");
Serial.println(right_z);
//just here to slow down the serial output - avoid data collision with prime numbers
delay(19);
}