Go Down

Topic: Force values not displaying on 4digit 7 segment LED display (Read 218 times) previous topic - next topic

aaronfu17

Hi Guys, I would really appreciate some help on this code I have. I am trying to write a code that allows me to calculate the force value using an accelerometer (lms303) and sending that value to a 4 digit 7 segment LED display

The values display in the Serial Monitor but when I hook up the arduino/accelerometer and LED display, the LED display only blinks "0 0 0 0". The values are not showing up there. Could anyone please help? Thank you greatly in advance.


Code: [Select]
#include <Wire.h> //Wire library enables I2C communication

// Slave Address
#define LSM303_ACC (0x32 >> 1)
//the 1 removes the last bit of the 8 bit address to get the 7 bit main address. The 8th bit is for reading or writing (1 or 0 tells it to 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 from designated register address on the 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 with certain register)
Wire.requestFrom(LSM303_ACC, 1); //read from the register! (see what the current value is)
while(!Wire.available()); //do nothing until data is available
temp = Wire.read(); //turn blank variable into data point
Wire.endTransmission();

return temp; //return variable that is now data point
}

//Function writes value to a control register. (One that you write) Out register (only for reading)
//Writes the given value to the 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 that value (write to the register instead reading from it) change what the register is holding
Wire.endTransmission();
}

void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
delay(300);
LSM303_write(0x20, 0b10010111); // low power mode, 5.376 kHz, all 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 be floating out by itself like this ***/
//ConversionFactor according to datasheet
const double SCALE = 2;
const double conversionFactor = double(SCALE / pow(2, 15)) * 9.81*70*4.6;
/********************/

void loop() {
// put your main code here, to run repeatedly:
// Create a variable for each axis to hold unitless acceleration values
// Get the least significant bit and most significant bit and combine them to get an integer value
// That reflects the acceleration of that axis
int z = (LSM303_read(OUT_X_L_A) << 8 | LSM303_read(OUT_X_H_A)); //int turns 16 bit into integer
int y = (LSM303_read(OUT_Y_L_A) << 8 | LSM303_read(OUT_Y_H_A));
int x = (LSM303_read(OUT_Z_L_A) << 8 | LSM303_read(OUT_Z_H_A));
double accelX = (double)x*conversionFactor; //double means decimal. Convert unitless values into 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);
}

PaulMurrayCbr

I would insert
Code: [Select]
dig1=1;dig2=2;dig3=3;dig4=4;

in front of the stuff that writes out the digits.

If you still get a 0,0,0,0 , then your problem is the stuff that works out how to display the digits.
If you get a 1,2,3,4, then your problem is the bit that works out what digits to display.

What does Serial.parseInt do? Will it just sit there waiting until you have typed in a number? How does it know when you have finished typing? If you type '11', how does it know that you mean 11, and not that you are trying to type in 114?
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

Go Up