I'm trying get the serial monitor to only print values when they change; however, my code doesn't seem to be working. It only stops printing when the value is 0. When its above 0, it just keeps printing the same value over again, unless the value is 0. Could anyone suggest a way to fix my code?
int potPin = A0;
void setup() {
Serial.begin(115200);
pinMode(potPin, INPUT);
}
int conversionOutRecord = 0;
void loop() {
int potVal = analogRead(potPin);
int conversion = map(potVal, 0, 1023, 0, 100);
int conversionOut = map(conversion, 0, 100, 0, 255);
analogWrite(3, conversionOut);
if (conversionOutRecord != potVal) {
Serial.print("Pot Value:");
Serial.print(conversion);
Serial.print("%");
Serial.println("");
Serial.print("Conversion Out:");
Serial.print(conversionOut);
Serial.println("");
conversionOutRecord = potVal;
}
}
Why do you declare a new variable each time though loop(), the value of which will replace that previously copied from potVal ?
Either declare the variable globally then just use it or declare it in loop() as static so that its value is preserved between iterations of loop()
That was dumb of me, thanks guys!
Doing that and replacing the potVal in the if statement did the trick. Sorry for wasting your time with something so basic! After about a month of Arduino, you'd think I'd have that down by now!!!
When you have problems with a program that does not behave in the way that you think it should when encountering a test (typically if or while) then it is helpful to print the values being tested to see whether they are what you expect.
UKHeliBob:
When you have problems with a program that does not behave in the way that you think it should when encountering a test (typically if or while) then it is helpful to print the values being tested to see whether they are what you expect.
I tend to do that with analog inputs; never really occurred to me to do it on the programming side... I'll keep it in mind. Thanks.
One sketch for 0-100% (101 values) and one for 0-255 (256 values).
Both with deadband and printing once.
Leo..
// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground
int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;
void setup() {
Serial.begin(115200); // set serial monitor to this baud rate, or change the value
}
void loop() {
// read input twice
rawValue = analogRead(A0);
rawValue = analogRead(A0); // double read
// ignore bad hop-on region of a pot by removing 8 values at both extremes
rawValue = constrain(rawValue, 8, 1015);
// add some deadband
if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
oldValue = rawValue;
// convert to percentage
potPercentage = map(oldValue, 8, 1008, 0, 100);
// Only print if %value changes
if (oldPercentage != potPercentage) {
Serial.print("Pot percentage is: ");
Serial.print(potPercentage);
Serial.println(" %");
oldPercentage = potPercentage;
}
}
}
// converts the position of a 10k lin(B) pot to a byte (0-255)
// pot connected to A0, 5volt and ground
int rawValue; // raw reading
int oldValue; // for deadband
byte Byte; // final byte
byte oldByte; // for printing
void setup() {
Serial.begin(9600);
}
void loop() {
// rawValue = analogRead(A0); // dummy read, if needed
rawValue = analogRead(A0); // read pot
if (rawValue < (oldValue - 2) || rawValue > (oldValue + 2)) { // add some deadband
oldValue = rawValue; // update value
Byte = oldValue >> 2; // convert 10-bit to 8-bit
if (oldByte != Byte) { // only print if value changes
Serial.print("Byte is: ");
Serial.println(Byte);
oldByte = Byte; // update value
}
}
}
constrain() is a function to keep a value within two boundaries.
Double-read is sometimes added to eleminate crosstalk if more than one analogue input, or a pot value >10k is used.
It helps the A/D get rid of a previous value from an other input faster, at the cost of some loop time (~100us).
Not really needed here.
Comparing a new value to the old value is to see if the value has changed.
The value is only printed once, if it has changed.
Leo..
Wawa: constrain() is a function to keep a value within two boundaries.
Double-read is sometimes added to eleminate crosstalk if more than one analogue input, or a pot value >10k is used.
It helps the A/D get rid of a previous value from an other input faster, at the cost of some loop time (~100us).
Not really needed here.
Comparing a new value to the old value is to see if the value has changed.
The value is only printed once, if it has changed.
Leo..