Thanks guys, ok - bad example.
Here is my code, but got to warn you it isn't pritty :
#include <stdlib.h>
char buffer1[] = "0";
char buffer2[] = "000";
int Sensor_In[] = {0,1,2,3}; // Sensor input pins
int PWM_Out[] = {9,10,11}; // PWM Pins
int i = 0; // Used for loops
int iAPin[] = {0,0,0,0};
int iPercent = 0;
int iPWM = 0;
int iReceived = 0;
int iValue=0;
int iSpan[]={0,0,0,0};
int iTemp=0;
int c=0;
int iTank=0;
float fPWM = 0.0;
float fPercent = 0.0;
#define OFFSET 118
#define CPI 15
#define SAMPLE_RATE 6
#define SAMPLE_TIME 10
#define REPORT_TIME 2000
// #define HIGH_SATURATION 949
// #define LOW_SATURATION 49
void setup()
{
for(i=0; i<4; i++)
{
pinMode(Sensor_In[i],INPUT);
}
for(i=9; i<12; i++)
{
pinMode(PWM_Out[i],OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
// Listen for Incoming commands
if(Serial.available() > 0)
{
Serial.println("Incomeing Data ");
c = Serial.read();
if(c >= '0' && c <= '3') // Tank Number
{
buffer1[1] = (char)c;
iTank = atoi(buffer1);
}
while(iReceived < 3)
{
c = Serial.read();
if(c >= '0' and c <= '9')
{
buffer2[iReceived++] = (char)c;
}
else
iReceived++;
}
iValue = atoi(buffer2);
Serial.print("Tank Size ");
Serial.println(iValue);
iReceived = 0;
if(iValue > 59 && iValue < 630) // Tank size, Calculate iSpan
{
iSpan[iTank] = (iValue*CPI)/10;//-OFFSET;
Serial.print("Span ");
Serial.println(iSpan[iTank]);
}
}
// Data Start
iTemp=0;
for(i=0; i<4; i++)
{
// Print Raw Counts
iAPin[i] = Sample(i);
Serial.print(iAPin[i]);
Serial.print(",");
// Convert to Inches
iTemp = CtoI(iAPin[i]);
Serial.print(iTemp);
Serial.print(",");
// Convert to Percent
if(iSpan[i] > 0)
{
iTemp = CtoP(iAPin[i],iSpan[i]);
Serial.print(iTemp);
}
else
{
Serial.print(0);
}
if(i != 3)
Serial.print(",");
}
// Data End
Serial.println(";");
delay(REPORT_TIME);
}
// Sample
int Sample(int PinNum)
{
int s=0;
int iLoop = 0;
for(iLoop=0; iLoop<SAMPLE_RATE; iLoop++)
{
s = s+analogRead(Sensor_In[PinNum]);
delay(SAMPLE_TIME);
}
if(s > 0)
s=s/SAMPLE_RATE;
else
s=0;
return s;
}
// Convert to Inches
int CtoI(int Value)
{
float fInches = 0.0;
if(Value <= OFFSET)
return 0;
fInches = ((float)Value-(float)OFFSET)/(float)CPI;
fInches = fInches*10.0;
return (int)fInches;
}
// Convert to Percent 00.0 presision expressed as 000
int CtoP(int Value,int Span)
{
Serial.print("Span = ");
Serial.println(Span);
float fPercent = 0.0;
fPercent = (((float)Value-(float)OFFSET)/Span)*1000.0;
//if(fPercent < 0.0)
// fPercent = 0.0;
//if(fPercent > 1000.0)
// fPercent = 1000.0;
return (int)fPercent;
}
For now I am just getting it to test my ideas, then I will go in and clean it up.
Everything works except the CtoP() function, (and if you are wondering why I am multiplying by 1000 instead of 100 is so the display panel will show "0.0%" to "100.0%") .
The line that calls the function iTemp = CtoP(iAPin,iSpan); half works, the function will use the new value of iAPin but not the new value of iSpan*, after it is first set.*
The data sent to is 4 digits, the first being the tank # the next 3 being the Tank size in inches, so If I set tank 0 to be 60.0 inches, I send "0600". iSpan is set correctly and I get the correct % for thank 0, Span = 900. but if I change the inches of the tank to say 30.0" I send "0300" but this time the iSpan[0] is now set to 450, but when the CtoP() is called, Span is still set at 900.
Hope that made sense.