LED compass


for school project, making led compass, in sense have compass sensor, accelerometer, , ring of charlieplexed leds on sleeve of sweatshirt.  i want led closest north lit up, when board held level.  the problem slightest variations in roll or pitch throw off compass readings much, starting seem infeasible.  my current source code follows.
code: [select]

#include <wire.h>
#include <charlieplex.h>
#define number_of_pins 4

int valy;                                        // variable y data stored  
int valx;                                        // variable x data stored
int accely = 1;                                  // setup pin assignment y data
int accelx = 2;                                  // setup pin assignment x data
int hmc = 0x42;                                  
int slaveaddress;                                // calculated in setup() function
byte headingdata[2];                            
int i, headingvalue;
byte pins[] = {5,6,7,8};
                                                             
charlieplex charlieplex = charlieplex(pins,number_of_pins);      //initialize object

charliepin led1 = { 0 , 3 };   //led1 indicated current flow 0 3
charliepin led2 = { 3 , 2 };
charliepin led3 = { 3 , 0 };
charliepin led4 = { 3 , 1 };
charliepin led5 = { 2 , 3 };
charliepin led6 = { 2 , 0 };
charliepin led7 = { 2 , 1 };
charliepin led8 = { 1 , 2 };
charliepin led9 = { 1 , 0 };
charliepin led10 = { 1 , 3 };
charliepin led11 = { 0 , 2 };
charliepin led12 = { 0 , 1 };

void setup()
{
 slaveaddress = hmc >> 1;   // shift device's documented slave address (0x42) 1 bit right
 serial.begin(9600);
 
 wire.begin();
}
void loop()
{
 wire.begintransmission(slaveaddress);        // send "a" command compass, requests current heading data
 wire.send("a");                              // "get data" command
 wire.endtransmission();          
 delay(10);                                   // compass needs @ delay catch up

 wire.requestfrom(slaveaddress, 2);        // request 2 byte heading (msb comes first)
 i = 0;                                    // read 2 heading bytes, msb first
 while(wire.available() && < 2)          // example: heading of 1345 134.5 degrees
 {
   headingdata[i] = wire.receive();
   i++;
 }
  {
     valy = analogread(accely);
     valx = analogread(accelx);
   }
 headingvalue = headingdata[0]*256 + headingdata[1];  // put msb , lsb together
 serial.print("current heading: ");
 serial.print(int (headingvalue / 10));     // whole number part of heading
 serial.print(".");
 serial.print(int (headingvalue % 10));     // fractional part of heading
 serial.println(" degrees");
 serial.print(valy);                        // accel y data
 serial.print(" ");
 serial.println(valx);                      // accel x data
 delay(500);
 {
   charlieplex.charliewrite(led1,high);
 }
}



is there way me use accelerometer smooth out compass readings?  essentially, need compass have 12 degrees of resolution, lighting 1 of twelve leds accordingly.  
any other advice or criticism more welcome.

thanks in advance help!

try make sooth function , calculate average of many measurements
something that
code: [select]

#define n 10

int valx;
int temp=0;

for(int i=0;i<n;i++){
  temp+=analogread(accelx);
}

valx=temp/n;





Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > LED compass


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums