I'm using QT to read value from a photo-resistor connected to the Arduino board, I successfully read the value and emit it, as the following:
void Dialog::handleReadyRead(){
QString temp;
temp = serial.readAll();
serialBuffer.append(temp);
int serPos;
double tempValue;
double previousValue = tempValue;
while ((serPos = serialBuffer.indexOf('\n')) >= 0)
{
bool ok;
previousValue = tempValue;
tempValue = QString::fromLatin1(serialBuffer.left(serPos)).toDouble(&ok);
if (ok){
emit newData(tempValue, previousValue);
}
serialBuffer = serialBuffer.mid(serPos+1);
}
}
However, for some reason I need to get the previous value. When I do previousValue = tempValue
, it prints out some weird values (sometimes it is indeed the previous value, but for most time it just print out 0 or some number really close to 0). I would like to know what had happened here and how could I fix it?
Sample wrong output may be like:
399
399
399
399
399
399
399
399
399
399
399
399
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1.49189e-154
1.49189e-154
0
0
0
399 is the correct value while all 0s are not.