Right now I have 2 ADC signals hooked up to AD0 and AD1 on my first Xbee. I have the second xbee connected to my arduino uno and am trying to read the values using the rx and tx pins. I have code that works perfectly for the first ADC value (analog value on AD0).
float temp;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() >= 20)
{
if(Serial.read() == 0x7E)
{
for (int i = 1; i < 11; i++)
{
byte discardByte = Serial.read();
}
float analogMSB = Serial.read();
float analogLSB = Serial.read();
float analogReading = analogLSB + (analogMSB * 256);
temp = analogReading / 1023 * 3.32;
Serial.print("----------------------------------------------------");
Serial.print('\n');
Serial.print("Sensor One Reading:");
Serial.print('\t');
Serial.print("Sensor One Reading:");
Serial.print('\n');
Serial.print(temp);
Serial.print('\t');
//Serial.print(SecondSensorVal);
Serial.print('\n');
}
}
}
Now I also need to get the reading from AD1, but I'm not sure how to do it. If someone could help me out here that would be great.
I figured it out.
New code:
float firstSensVal;
float secondSensVal;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() >= 20)
{
if(Serial.read() == 0x7E)
{
for (int i = 1; i < 11; i++)
{
byte discardByte = Serial.read();
}
float firstAnalogMSB = Serial.read();
float firstAnalogLSB = Serial.read();
float firstAnalogReading = firstAnalogLSB + (firstAnalogMSB * 256);
firstSensVal = firstAnalogReading / 1023 * 3.32;
float secondAnalogMSB = Serial.read();
float secondAnalogLSB = Serial.read();
float secondAnalogReading = secondAnalogLSB + (secondAnalogMSB * 256);
secondSensVal = secondAnalogReading / 1023 * 3.32;
Serial.print("----------------------------------------------------");
Serial.print('\n');
Serial.print("Sensor One Reading:");
Serial.print('\t');
Serial.print("Sensor Two Reading:");
Serial.print('\n');
Serial.print(firstSensVal);
Serial.print('\t');Serial.print('\t');Serial.print('\t');
Serial.print(secondSensVal);
Serial.print('\n');
}
}
}
system
September 4, 2015, 10:15pm
3
float firstAnalogMSB = Serial.read();
float firstAnalogLSB = Serial.read();
float firstAnalogReading = firstAnalogLSB + (firstAnalogMSB * 256);
Serial.read() does not return a float. The math operation does not create a float. The first two variables should be uint8_t. The third should be int.
firstSensVal = firstAnalogReading / 1023 * 3.32;
When firstAnalogReading becomes an int, the 1023 needs to become 1023.0, to force floating point arithmetic to be used.