Aye, matey, code for diving from Davey Jones' locker ...
(Sorry, couldn't help myself!)
I think you may have mismatches between variable types.
Try looking at those, make sure everything involved in the math functions is defined as a float for instance.
/*
By David Jones July 26, 2012
Take readings from a pressure sensor, using a 595 shiftout program, displayed on
3 4" Kingbright LEDs for my flow lab. Used sink register TLC5916IN
(or 595-TLC5916IN ) with 9V DC power supply (for the LEDs, sensor, Arduino).
Used a 330ohm resistor for LEDs and for sensor (4-20mA).
To check the calibration of the sensor open file
Smooth readingsfrom pressure sensor, using David Mellis' smoothing program.
http://www.arduino.cc/en/Tutorial/Smoothing
Breaking down digits by EmilyJane
ShiftOut from Carlyn Maw and Tom Igoe
http://arduino.cc/en/Tutorial/ShiftOut
*/
int LatchPin = 8;
int ClockPin = 12;
int DataPin = 11;
int inputPin = A0;
int j = 1;
int toShift = 0;
char m[3];
byte data;
byte dataArray[10];
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
float Gain = 157.1653543;
float Offset = -204.3149606;
void setup() {
pinMode(LatchPin, OUTPUT);
Serial.begin(9600);
dataArray[0] = 0x3F; // 0
dataArray[1] = 0x06; // 1
dataArray[2] = 0x5B; // 2
dataArray[3] = 0x4F; // 3
dataArray[4] = 0x66; // 4
dataArray[5] = 0x6D; // 5
dataArray[6] = 0x7D; // 6
dataArray[7] = 0x07; // 7
dataArray[8] = 0x7F; // 8
dataArray[9] = 0x6F; // 9
// initialize all the readings to 0:
for (int i = 0; i < numReadings; i++)
readings[i] = 0;
// blink displays for n times d delay
int n = 2;
int d = 500;
for (int x = 0; x < n; x++){
digitalWrite(LatchPin, 0);
shiftOut(DataPin, ClockPin, 0);
shiftOut(DataPin, ClockPin, 0);
shiftOut(DataPin, ClockPin, 0);
digitalWrite(LatchPin, 1);
delay(d);
digitalWrite(LatchPin, 0);
shiftOut(DataPin, ClockPin, 255);
shiftOut(DataPin, ClockPin, 255);
shiftOut(DataPin, ClockPin, 255);
digitalWrite(LatchPin, 1);
delay(d);
}
}
void loop()
{
// subtract the last reading:
total = total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// convert from average digital reading to voltage and then to Pressure
int Pressure = ((average * (5.0 / 1023.0))*Gain) + Offset;
Serial.println(Pressure);
// the following code by EmilyJane of arduino.cc forums
// modified to fit this situation (3 Leds
int k = 100;
for (int i = 2; i >= 0; i--)
{
m[i] = Pressure / k;
Pressure = Pressure - (m[i] * k);
k = k / 10;
}
// end
digitalWrite(LatchPin, 0);
shiftOut(DataPin, ClockPin, dataArray[m[0]]);
shiftOut(DataPin, ClockPin, dataArray[m[1]]);
shiftOut(DataPin, ClockPin, dataArray[m[2]]);
digitalWrite(LatchPin, 1);
}
// the heart of the program (aka shiftOut
void shiftOut(int myDataPin, int myClockPin, byte myDataOut)
{
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut?
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}