0

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.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Ruofan Liu
  • 37
  • 5
  • Your variable tempValue is never issued a value to begin with, then you try assigning previousValue to tempValue. – Zypps987 Mar 17 '17 at 19:34
  • this still happened when I initialize it with a 0, and it does not give mw a correct value when I do double tempValue = QString::fromLatin1(serialBuffer.left(serPos)).toDouble(); – Ruofan Liu Mar 17 '17 at 19:44
  • Is there ever a situation where all 0s are not the correct output? What happens when the photoresistor detects nothing? – Zypps987 Mar 17 '17 at 19:48
  • Thanks. the photoresistor should always have a non-zero value in this case as it can always detect the light from the window, this value should change dramatically when I use a laser pointer to shine on it. – Ruofan Liu Mar 17 '17 at 19:50
  • What's the rest of your code look like? – Zypps987 Mar 17 '17 at 19:58
  • What Im doing is to make a realtime plot tool using QT, in addition, I am trying to measure the duration when the laser pointer is shining on the photoresistor, and thats why I need previous value. – Ruofan Liu Mar 17 '17 at 20:04
  • I also noticed your bool ok is not set to anything either. – Zypps987 Mar 17 '17 at 20:07
  • I changed it to false as an initial value and it still doesn't work – Ruofan Liu Mar 17 '17 at 20:08
  • What exactly is emit? Is this your own defined class? And I assume you only want to output values when both tempValue and previousValue are not empty or 0? – Zypps987 Mar 17 '17 at 20:13
  • what emit does is that whoever there is a data coming in , it will emit the data. I have another function that receives the emit data – Ruofan Liu Mar 17 '17 at 20:19

2 Answers2

0

I'm assuming whenever (true != ok) your value in invalid. The problem might be there, you keep that value while you should discard it.

What about you do

bool ok;
double parsed = QString::fromLatin1(serialBuffer.left(serPos)).toDouble(&ok);
if (ok) {
    previousValue = tempValue;
    tempValue = parsed;
    emit newData(parsed, previousValue);      
    }

It might be relevant to rename tempValue to currValue in that example.

AlexG
  • 1,091
  • 7
  • 15
  • Thanks a lot. What is "parsed" here? is it a new variable other than tempValue? – Ruofan Liu Mar 17 '17 at 19:24
  • Yes, parsed is a new variable. the idea is that you want to discard the value whenever 'ok' is false. First of all, your current code doesn't set 'previousValue' or 'currentValue' = 0, which means the first values you get are most likely garbage. The second issue was 'previousValue = currentValue' even though the value isn't valid (you assign it before checking 'ok'). – AlexG Mar 18 '17 at 13:27
0

Try this

double tempValue = 0;
double previousValue = 0;

while ((serPos = serialBuffer.indexOf('\n')) >= 0)
{
    tempValue = QString::fromLatin1(serialBuffer.left(serPos)).toDouble(&ok);

    if (previousValue != 0 && tempValue != 0){
        emit newData(tempValue, previousValue);
    }

    previousValue = tempValue
    serialBuffer = serialBuffer.mid(serPos+1);
}
Zypps987
  • 404
  • 6
  • 21
  • Thanks. I kinda see whats happening here, but the problem is that 0 is not something from the resistor, it's just abnormal values caused by initializations(I found that later on). this part doesn't work. – Ruofan Liu Mar 17 '17 at 20:28
  • Ahh ok good to know you solved it. Next time though post all the code relevant to your problem :) But nonetheless my answer fixes the problem with the code you posted if that helps. – Zypps987 Mar 17 '17 at 20:50
  • just found out what the problem but still not able to solve it... thanks anyway – Ruofan Liu Mar 17 '17 at 20:51