Go Down

Topic: Printout analogValue (Read 193 times) previous topic - next topic

badz

Hi. I want to read out an analog Signal and show the Value on my TFT Display. If I Overwrite the Value too often, it flickers a bit. So i wanted to compare my Value, and just Overwrite after 5% of the last Value.
How could i for example compare my actualValue with my lastValue +- 5%, and print the Value out?

Hope you can help im a newbie, and thanks in advance :D

MorganS

I guess you could do that. It seems like you are halfway there because you've got names for your variables already and a good idea of the logic required.

There's two other ways to attack this problem:

1. Only print to the TFT 2 or 5 times per second. Study the blink-without-delay example for an idea on how to do this.

2. Only print digits which have changed. Then you don't waste a lot of time printing the first few digits which don't change very often. This can make the whole program much more responsive.
"The problem is in the code you didn't post."

UKHeliBob

Quote
How could i for example compare my actualValue with my lastValue +- 5%, and print the Value out?
Create 2 new variables, let's call then upperValue and lowerValue.  Calculate lastValue + 5% and save it to upperValue.  Calculate lastValue - 5% and save it to lowerValue

Then
Code: [Select]
if (actualValue <= lowerValue || actualValue >= upperValue)
{
  //print actualValue here
  lastValue = actualValue;
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MarkT

I'd vote for updating screen at a fixed rate indepedent of reading the analog pin.  You don't want the
screen update to be flooded with updates just because an input is changing rapidly, it just needs
to be timely enough for a human to read.

And you don't want to fail to update indefinitely because the value has only change 1%, which is what your
proposed scheme would do.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

UKHeliBob

Quote
I'd vote for updating screen at a fixed rate indepedent of reading the analog pin.
Good idea.

Stand by for questions why the program is unresponsive and the revelation that delay() has been used to achieve the periodic update followed by queries about how to use millis() and posting of links to the BlinkWithoutDelay example, Using millis() for timing.  A beginners guide and Several things at the same time

Hopefully I have short circuited the process.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

badz

#5
Oct 13, 2017, 06:38 am Last Edit: Oct 13, 2017, 06:45 am by badz
Hey. Thanks, With the advice i came a bit further. But now i have the Problem, that i cant compare my float Values. no match for 'operator<=' (operand types are 'String' and 'float')

Heres my programm:


Code: [Select]


float lowerValue;
float upperValue;
float actualValue = A6;
char printout[4];
byte led = 5;//byte: 8 bit (0-255)
#include <TFT.h>
#include <SPI.h>
#define CS 9
#define DC 0
#define RESET 1
TFT screen = TFT(CS, DC, RESET);



void setup() {
// put your setup code here, to run once:




Serial.begin(9600);
Serial.println(analogRead(A6));

screen.begin();
screen.background(0, 0, 0);


screen.stroke(255, 255, 255); //WeiƟ
screen.text("VOLT", 0, 30);
screen.setTextSize(3);


screen.stroke(255, 0, 0); //Rot
screen.line(0, 40, screen.width(), 40);


}

void loop() {
// put your main code here, to run repeatedly:

if (analogRead(A6) * 1.05)
{
upperValue;
}
if (analogRead(A6) * 0.95)
{
lowerValue;
}


Serial.println((10.0 / 1023 * analogRead(A6))); //Serial Monitor



String actualValue = String(10.0 / 1023 * analogRead(A6));
actualValue.toCharArray(printout, 4);



if (actualValue <= lowerValue || actualValue >= upperValue)
{
{ screen.stroke(255, 255, 255);
  screen.text(printout, 0, 50);
}
lowerValue = actualValue;
}



}




UKHeliBob

Code: [Select]
String actualValue = String(10.0 / 1023 * analogRead(A6));Why are you using Strings to hold numerical values ?

Please post your complete program
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

badz

This is my whole program. What can i do instead. I thought i need it for the printout..

UKHeliBob

Code: [Select]
if (analogRead(A6) * 1.05)
{
upperValue;
}

What do you think these lines do ?

You are not testing whether the reading has changed significantly.    analogRead(A6) * 1.05 will always be true so the associated code block will be executed.  

However
Code: [Select]

upperValue;

does nothing anyway.

Did you read and understand reply #2 ?

Quote
I thought i need it for the printout.
Np, you can print a float value without converting it to a String.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

badz

Sorry i thoght i would understand. What should i do instead?

UKHeliBob

Sorry i thoght i would understand. What should i do instead?

Did you do what I suggested in reply #2 ?

Code: [Select]

currentValue = analogRead(analogPin);

lowerValue = previousValue * 0.95;
upperValue = previousValue * 1.05;

if currentValue < lowerValue || currentValue > upperValue)  //a significant change has occured
{
  //print currentValue here
  previousValue = currentValue  //ready for the next time a value is read
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up