wokwi / wokwi-features

Wokwi Feature requests & Bug Reports
https://wokwi.com
69 stars 10 forks source link

Both potentiometers: Initiation bug #766

Closed Pilot65 closed 2 months ago

Pilot65 commented 2 months ago

bugreport potentiometer.zip

See attachement for Wokwi project.

` void setup() { Serial.begin(9600); Serial.println("Ready..."); //while (!Serial); // This does not help //delay(500); // This DOES fix the problem, but it suggests that there is a bug somwhere } //---------------

/* The problem is that for the first few readings from the potentiometer, it outputs its default value (0), before outputting it set value. Try setting the knob to max (1023) and restart... there is a number of erratic value "0" readings before the correct output is presented.

The number of erratic readings differs for each time the sketch is restarted !.

Tried changing the default value to 1023, and now the problem is "mirrored".

The above bug is the same for both potentiometers.

The potentiometer should NEVER output anything other than its actual position value */

void loop() { int RawValue = analogRead(0); Serial.println(RawValue);

delay(20); }`

Koepel commented 2 months ago

Confirmed, it is a bug.

My Wokwi project to test: https://wokwi.com/projects/394419928243853313 Result:

First 10 values
0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 743
8: 743
9: 743

The first values are either 0 or the value as set in diagram.json for the potentiometer. The last values of 743 is the position of the knob that was previous set. The time it takes to get the value of the knob position is always the same time in my sketch. Changing the sketch to measure the time with millis() made the problem disappear.

Only tested with a Arduino Uno board. When testing with more potentiometers or with a slide potentiometer, the problem still occurs.

With my test project on my computer the problem happens in more than 50% of the runs, when I click Stop - Start - Stop - Start.

Pilot65 commented 2 months ago

I forgot to mention that when I removed the potentiometer and linked AN0 directly to 5V ther was no problem, again indicating that the potentiometer (and not the ADC) is slow to 'wake up' on startup.

urish commented 2 months ago

Thanks for the detailed bug report + easy reproduction from @Koepel.

I pushed a fix - can you please give it another go and report if it's working correctly now?

Koepel commented 2 months ago

My test is now running perfect, everything is as expected.