i have different code work separately 1 reads numbers accelerometer , other displays code 7 segment display 4 digit led. wanted see if can me put 2 codes together. trying determine max acceleration , display on led.
any useful!!!!
thanks in advance
the accelerometer code is:
#include <wire.h> //wire library enables i2c communication
// slave address
#define lsm303_acc (0x32 >> 1)
//the 1 removes last bit of 8 bit address 7 bit main address. 8th bit reading or writing (1 or 0 tells read or write respectively)
//used register addresses (from datasheet)
#define out_x_l_a 0x28
#define out_x_h_a 0x29
#define out_y_l_a 0x2a
#define out_y_h_a 0x2b
#define out_z_l_a 0x2c
#define out_z_h_a 0x2d
//reads value designated register address on lsm303
byte lsm303_read(byte address) {
byte temp; //blank variable
wire.begintransmission(lsm303_acc); //designates slave (slave starts listening, other slaves keep ignoring)
wire.write(address); //designate register (every i2c slave has registers (deals register)
wire.requestfrom(lsm303_acc, 1); //read register! (see current value is)
while(!wire.available()); //do nothing until data available
temp = wire.read(); //turn blank variable data point
wire.endtransmission();
return temp; //return variable data point
}
//function writes value control register. (one write) out register (only reading)
//writes given value given address
void lsm303_write(byte address, byte data) {
wire.begintransmission(lsm303_acc); //designates slave
wire.write(address); //designates register
wire.write(data); //gives register value (write register instead reading it) change register holding
wire.endtransmission();
}
void setup() {
// put setup code here, run once:
wire.begin();
serial.begin(9600);
delay(300);
lsm303_write(0x20, 0b10010111); // low power mode, 5.376 khz, accel axes on
}
//conversionfactor according datasheet
const double scale = 2;
const double conversionfactor = double(scale / pow(2, 15)) * 9.81;
void loop() {
// put main code here, run repeatedly:
// create variable each axis hold unitless acceleration values
// least significant bit , significant bit , combine them integer value
// reflects acceleration of axis
int z = (lsm303_read(out_x_l_a) << | lsm303_read(out_x_h_a); //int turns 16 bit integer
int y = (lsm303_read(out_y_l_a) << | lsm303_read(out_y_h_a);
int x = (lsm303_read(out_z_l_a) << | lsm303_read(out_z_h_a);
double accelx = (double)x*conversionfactor; //double means decimal. convert unitless values m/s^2
double accely = (double)y*conversionfactor;
double accelz = (double)z*conversionfactor;
serial.print("x:\t"); serial.print(accelx);
serial.print("\ty:\t"); serial.print(accely);
serial.print("\tz:\t"); serial.println(accelz);
delay(100);
}
and led code
int apin = 2; //
int bpin = 3; //
int cpin = 4; //
int dpin = 5; //
int epin = 6; //
int fpin = 7; //
int gpin = 8; //
int gnd1 = 9; //
int gnd2 = 10; //
int gnd3 = 11; //
int gnd4 = 12; //
int num; //
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int dtime = 4;
void setup()
{
pinmode(apin, output);
pinmode(bpin, output);
pinmode(cpin, output);
pinmode(dpin, output);
pinmode(epin, output);
pinmode(fpin, output);
pinmode(gpin, output);
pinmode(gnd1, output);
pinmode(gnd2, output);
pinmode(gnd3, output);
pinmode(gnd4, output);
serial.begin(9600);
}
void loop()
{
digitalwrite( gnd1, high);
digitalwrite( gnd2, high);
digitalwrite( gnd3, high);
digitalwrite( gnd4, high);
if (serial.available() > 0)
{
num = serial.parseint();
serial.println(num);
dig1 = num / 1000;
num = num - (dig1 * 1000);
dig2 = num / 100;
num = num - (dig2 * 100);
dig3 = num / 10;
dig4 = num - (dig3 *10);
}
digitalwrite( gnd4, low); //digit 4
picknumber(dig4);
delay(dtime);
digitalwrite( gnd4, high);
digitalwrite( gnd3, low); //digit 3
picknumber(dig3);
delay(dtime);
digitalwrite( gnd3, high);
digitalwrite( gnd2, low); //digit 2
picknumber(dig2);
delay(dtime);
digitalwrite( gnd2, high);
digitalwrite( gnd1, low); //digit 1
picknumber(dig1);
delay(dtime);
digitalwrite( gnd1, high);
}
void picknumber(int x){
switch(x){
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
case 4: four(); break;
case 5: five(); break;
case 6: six(); break;
case 7: seven(); break;
case 8: eight(); break;
case 9: nine(); break;
default: zero(); break;
}
}
void clearleds()
{
digitalwrite( 2, low); // a
digitalwrite( 3, low); // b
digitalwrite( 4, low); // c
digitalwrite( 5, low); // d
digitalwrite( 6, low); // e
digitalwrite( 7, low); // f
digitalwrite( 8, low); // g
}
void one()
{
digitalwrite( apin, low);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, low);
}
void two()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, low);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, low);
digitalwrite( gpin, high);
}
void three()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, high);
}
void four()
{
digitalwrite( apin, low);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void five()
{
digitalwrite( apin, high);
digitalwrite( bpin, low);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void six()
{
digitalwrite( apin, high);
digitalwrite( bpin, low);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void seven()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, low);
}
void eight()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void nine()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void zero()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, low);
}
any useful!!!!
thanks in advance
the accelerometer code is:
#include <wire.h> //wire library enables i2c communication
// slave address
#define lsm303_acc (0x32 >> 1)
//the 1 removes last bit of 8 bit address 7 bit main address. 8th bit reading or writing (1 or 0 tells read or write respectively)
//used register addresses (from datasheet)
#define out_x_l_a 0x28
#define out_x_h_a 0x29
#define out_y_l_a 0x2a
#define out_y_h_a 0x2b
#define out_z_l_a 0x2c
#define out_z_h_a 0x2d
//reads value designated register address on lsm303
byte lsm303_read(byte address) {
byte temp; //blank variable
wire.begintransmission(lsm303_acc); //designates slave (slave starts listening, other slaves keep ignoring)
wire.write(address); //designate register (every i2c slave has registers (deals register)
wire.requestfrom(lsm303_acc, 1); //read register! (see current value is)
while(!wire.available()); //do nothing until data available
temp = wire.read(); //turn blank variable data point
wire.endtransmission();
return temp; //return variable data point
}
//function writes value control register. (one write) out register (only reading)
//writes given value given address
void lsm303_write(byte address, byte data) {
wire.begintransmission(lsm303_acc); //designates slave
wire.write(address); //designates register
wire.write(data); //gives register value (write register instead reading it) change register holding
wire.endtransmission();
}
void setup() {
// put setup code here, run once:
wire.begin();
serial.begin(9600);
delay(300);
lsm303_write(0x20, 0b10010111); // low power mode, 5.376 khz, accel axes on
}
//conversionfactor according datasheet
const double scale = 2;
const double conversionfactor = double(scale / pow(2, 15)) * 9.81;
void loop() {
// put main code here, run repeatedly:
// create variable each axis hold unitless acceleration values
// least significant bit , significant bit , combine them integer value
// reflects acceleration of axis
int z = (lsm303_read(out_x_l_a) << | lsm303_read(out_x_h_a); //int turns 16 bit integer
int y = (lsm303_read(out_y_l_a) << | lsm303_read(out_y_h_a);
int x = (lsm303_read(out_z_l_a) << | lsm303_read(out_z_h_a);
double accelx = (double)x*conversionfactor; //double means decimal. convert unitless values m/s^2
double accely = (double)y*conversionfactor;
double accelz = (double)z*conversionfactor;
serial.print("x:\t"); serial.print(accelx);
serial.print("\ty:\t"); serial.print(accely);
serial.print("\tz:\t"); serial.println(accelz);
delay(100);
}
and led code
int apin = 2; //
int bpin = 3; //
int cpin = 4; //
int dpin = 5; //
int epin = 6; //
int fpin = 7; //
int gpin = 8; //
int gnd1 = 9; //
int gnd2 = 10; //
int gnd3 = 11; //
int gnd4 = 12; //
int num; //
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int dtime = 4;
void setup()
{
pinmode(apin, output);
pinmode(bpin, output);
pinmode(cpin, output);
pinmode(dpin, output);
pinmode(epin, output);
pinmode(fpin, output);
pinmode(gpin, output);
pinmode(gnd1, output);
pinmode(gnd2, output);
pinmode(gnd3, output);
pinmode(gnd4, output);
serial.begin(9600);
}
void loop()
{
digitalwrite( gnd1, high);
digitalwrite( gnd2, high);
digitalwrite( gnd3, high);
digitalwrite( gnd4, high);
if (serial.available() > 0)
{
num = serial.parseint();
serial.println(num);
dig1 = num / 1000;
num = num - (dig1 * 1000);
dig2 = num / 100;
num = num - (dig2 * 100);
dig3 = num / 10;
dig4 = num - (dig3 *10);
}
digitalwrite( gnd4, low); //digit 4
picknumber(dig4);
delay(dtime);
digitalwrite( gnd4, high);
digitalwrite( gnd3, low); //digit 3
picknumber(dig3);
delay(dtime);
digitalwrite( gnd3, high);
digitalwrite( gnd2, low); //digit 2
picknumber(dig2);
delay(dtime);
digitalwrite( gnd2, high);
digitalwrite( gnd1, low); //digit 1
picknumber(dig1);
delay(dtime);
digitalwrite( gnd1, high);
}
void picknumber(int x){
switch(x){
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
case 4: four(); break;
case 5: five(); break;
case 6: six(); break;
case 7: seven(); break;
case 8: eight(); break;
case 9: nine(); break;
default: zero(); break;
}
}
void clearleds()
{
digitalwrite( 2, low); // a
digitalwrite( 3, low); // b
digitalwrite( 4, low); // c
digitalwrite( 5, low); // d
digitalwrite( 6, low); // e
digitalwrite( 7, low); // f
digitalwrite( 8, low); // g
}
void one()
{
digitalwrite( apin, low);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, low);
}
void two()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, low);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, low);
digitalwrite( gpin, high);
}
void three()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, high);
}
void four()
{
digitalwrite( apin, low);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void five()
{
digitalwrite( apin, high);
digitalwrite( bpin, low);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void six()
{
digitalwrite( apin, high);
digitalwrite( bpin, low);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void seven()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, low);
}
void eight()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void nine()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void zero()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, low);
}
so combine usual way:
stuff before setup goes together
stuff in setup() goes together
stuff in loop() goes together
functions stay separate
no code exist outside of function (and setup() , loop() functions)
clear duplicate uses of pins, variable names.
confirm end functionality.
your turn ...
stuff before setup goes together
stuff in setup() goes together
stuff in loop() goes together
functions stay separate
no code exist outside of function (and setup() , loop() functions)
clear duplicate uses of pins, variable names.
confirm end functionality.
code: [select]
#include <wire.h> //wire library enables i2c communication
// slave address
#define lsm303_acc (0x32 >> 1)
//the 1 removes last bit of 8 bit address 7 bit main address. 8th bit reading or writing (1 or 0 tells read or write respectively)
//used register addresses (from datasheet)
#define out_x_l_a 0x28
#define out_x_h_a 0x29
#define out_y_l_a 0x2a
#define out_y_h_a 0x2b
#define out_z_l_a 0x2c
#define out_z_h_a 0x2d
int apin = 2; //
int bpin = 3; //
int cpin = 4; //
int dpin = 5; //
int epin = 6; //
int fpin = 7; //
int gpin = 8; //
int gnd1 = 9; //
int gnd2 = 10; //
int gnd3 = 11; //
int gnd4 = 12; //
int num; //
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int dtime = 4;
//reads value designated register address on lsm303
byte lsm303_read(byte address) {
byte temp; //blank variable
wire.begintransmission(lsm303_acc); //designates slave (slave starts listening, other slaves keep ignoring)
wire.write(address); //designate register (every i2c slave has registers (deals register)
wire.requestfrom(lsm303_acc, 1); //read register! (see current value is)
while(!wire.available()); //do nothing until data available
temp = wire.read(); //turn blank variable data point
wire.endtransmission();
return temp; //return variable data point
}
//function writes value control register. (one write) out register (only reading)
//writes given value given address
void lsm303_write(byte address, byte data) {
wire.begintransmission(lsm303_acc); //designates slave
wire.write(address); //designates register
wire.write(data); //gives register value (write register instead reading it) change register holding
wire.endtransmission();
}
void setup() {
// put setup code here, run once:
wire.begin();
serial.begin(9600);
delay(300);
lsm303_write(0x20, 0b10010111); // low power mode, 5.376 khz, accel axes on
pinmode(apin, output);
pinmode(bpin, output);
pinmode(cpin, output);
pinmode(dpin, output);
pinmode(epin, output);
pinmode(fpin, output);
pinmode(gpin, output);
pinmode(gnd1, output);
pinmode(gnd2, output);
pinmode(gnd3, output);
pinmode(gnd4, output);
// serial.begin(9600);
}
/***this code can't floating out ***/
//conversionfactor according datasheet
const double scale = 2;
const double conversionfactor = double(scale / pow(2, 15)) * 9.81;
/********************/
void loop() {
// put main code here, run repeatedly:
// create variable each axis hold unitless acceleration values
// least significant bit , significant bit , combine them integer value
// reflects acceleration of axis
int z = (lsm303_read(out_x_l_a) << | lsm303_read(out_x_h_a); //int turns 16 bit integer
int y = (lsm303_read(out_y_l_a) << | lsm303_read(out_y_h_a);
int x = (lsm303_read(out_z_l_a) << | lsm303_read(out_z_h_a);
double accelx = (double)x*conversionfactor; //double means decimal. convert unitless values m/s^2
double accely = (double)y*conversionfactor;
double accelz = (double)z*conversionfactor;
serial.print("x:\t"); serial.print(accelx);
serial.print("\ty:\t"); serial.print(accely);
serial.print("\tz:\t"); serial.println(accelz);
delay(100);
digitalwrite( gnd1, high);
digitalwrite( gnd2, high);
digitalwrite( gnd3, high);
digitalwrite( gnd4, high);
if (serial.available() > 0)
{
num = serial.parseint();
serial.println(num);
dig1 = num / 1000;
num = num - (dig1 * 1000);
dig2 = num / 100;
num = num - (dig2 * 100);
dig3 = num / 10;
dig4 = num - (dig3 *10);
}
digitalwrite( gnd4, low); //digit 4
picknumber(dig4);
delay(dtime);
digitalwrite( gnd4, high);
digitalwrite( gnd3, low); //digit 3
picknumber(dig3);
delay(dtime);
digitalwrite( gnd3, high);
digitalwrite( gnd2, low); //digit 2
picknumber(dig2);
delay(dtime);
digitalwrite( gnd2, high);
digitalwrite( gnd1, low); //digit 1
picknumber(dig1);
delay(dtime);
digitalwrite( gnd1, high);
}
void picknumber(int x){
switch(x){
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
case 4: four(); break;
case 5: five(); break;
case 6: six(); break;
case 7: seven(); break;
case 8: eight(); break;
case 9: nine(); break;
default: zero(); break;
}
}
void clearleds()
{
digitalwrite( 2, low); // a
digitalwrite( 3, low); // b
digitalwrite( 4, low); // c
digitalwrite( 5, low); // d
digitalwrite( 6, low); // e
digitalwrite( 7, low); // f
digitalwrite( 8, low); // g
}
void one()
{
digitalwrite( apin, low);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, low);
}
void two()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, low);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, low);
digitalwrite( gpin, high);
}
void three()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, high);
}
void four()
{
digitalwrite( apin, low);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void five()
{
digitalwrite( apin, high);
digitalwrite( bpin, low);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void six()
{
digitalwrite( apin, high);
digitalwrite( bpin, low);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void seven()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, low);
digitalwrite( epin, low);
digitalwrite( fpin, low);
digitalwrite( gpin, low);
}
void eight()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void nine()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, low);
digitalwrite( fpin, high);
digitalwrite( gpin, high);
}
void zero()
{
digitalwrite( apin, high);
digitalwrite( bpin, high);
digitalwrite( cpin, high);
digitalwrite( dpin, high);
digitalwrite( epin, high);
digitalwrite( fpin, high);
digitalwrite( gpin, low);
}
your turn ...
Arduino Forum > Using Arduino > Programming Questions > display accelerometer code to LED
arduino
Comments
Post a Comment