Go Down

Topic: [SOLVED] Why does analogRead need idle time? (Read 305 times) previous topic - next topic

davepl

Sep 14, 2018, 09:31 pm Last Edit: Sep 15, 2018, 12:41 am by AWOL
I've written a fairly involved spectrum analyzer.  A previous version can be seen here:

https://www.youtube.com/watch?v=UnCylsS1w8k

Of all the things to have trouble with, I'm having trouble with with the analogRead function and my brightness knob!  Not the sampling, that all works great.

The value on the knob (ie: the analogRead pin value) swings around somewhat wildly if I do not allocate enough idle time in my loop().

If I delay(10) it'll work, but I actually need those cycles, so would prefer not to do that. 

Anyone know how I can get a reliable pot value without idle time?  I've tried adcStart/adcEnd directly, same result.

davepl

Here's a similar one from another posting that you might wish to consider.  It exhibits the same problem:  the displayed value will be effectively random.  Not like a floating pin, but much more random.

I'm guessing -his- problem is the same one - he didn't allow for any idle time, so he (or she) doesn't get a valid read.  But why is that?  Is it reading too frequently?  Or what's the issue?

void setup()
{
  pinMode(sensor,INPUT);
  Serial.begin(115200);
}

void loop()
{
  gas_value=analogRead(sensor);
  Serial.println(gas_value);
}

AWOL

Code: [Select]
Arduino: 1.8.5 (Linux), Board: "Arduino/Genuino Uno"

foobar110.ino: In function 'void setup()':
foobar110:3: error: 'sensor' was not declared in this scope
   pinMode(sensor,INPUT);
                ^
foobar110.ino: In function 'void loop()':
foobar110:9: error: 'gas_value' was not declared in this scope
   gas_value=analogRead(sensor);
    ^
foobar110:9: error: 'sensor' was not declared in this scope
   gas_value=analogRead(sensor);
                                      ^
exit status 1
'sensor' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


That, and no schematic.

Time-wasting.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

westfw

I imagine it might have something to do with the impedence of your knob being too high to charge the S&H cap fully when you change channels before the actual conversion.

davepl

I didn't realize one must prepare a full demo package complete with the schematic in order to ask a fundamental question amongst you immortals. 

Ever been to the real world?  Amusing.

AWOL

#5
Sep 14, 2018, 11:42 pm Last Edit: Sep 15, 2018, 03:48 pm by AWOL
Quote
Ever been to the real world? 
Yeah. Went out once. The graphics weren't that good.

How about you?
Done much electronics?
Real electronics?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

davepl

Nope, I'm a software guy.  If you want to have a pissing match, it'll have to be in code, not hardware.  But I have work to do so I'd prefer you demonstrate your might to the next lonely noob that wanders through and maybe allow someone to answer my actual question?

Back to the problem at hand, here's a repro case; I'm sure it'll be easy for you now that you have the code.  I await your epic wisdom.  Unless it's all just bluster from mom's basement, I guess.

Code: [Select]
void InputLoop(void *)
{
   for (;;)
   {
       float raw = analogRead(BRIGHTNESS_PIN); // Brightness is logrithmic, so we do the opposite; log(2.52) makes the knob roughly linear
       Serial.println(raw);
       raw = mapFloat(raw, 0, MAX_ANALOG_IN, 1.5, 10);     // 1.5 mapped serves as a decent minimum brightness (as low as it will go but NOT off, by design)
       raw = roundf(raw);                                  // Rounding to an int gives it discrete "steps"
       gBrightness = min(255,powf(raw, 2.52f));            // , based on my experimentation
       delay(100);
   }
}


In the above case, the values displayed are (for example, from the log):

2114.00
136.00
1.00
2079.00
2097.00
1.00
1.00
1.00
1.00
65.00
1.00
271.00
830.00
1344.00
1.00
1.00
591.00
2096.00
1.00
1138.00
1.00
312.00
1737.00
1.00
382.00
2097.00
1212.00
1972.0


Delta_G

I didn't realize one must prepare a full demo package complete with the schematic in order to ask a fundamental question amongst you immortals.  

Ever been to the real world?  Amusing.
But you're making an extraordinary claim.  You claim that without a delay in your loop your analog readings bounce.  You've got an idea about what you think is causing it.  But you're wrong.  If you want to get to why you are seeing what you are seeing then show us what we're working with.  Because when I do analog read with my Arduino it doesn't behave that way.  So it MUST be something to do with either your setup or your code.  

Now does it make sense?  In the real world?  
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Delta_G

#8
Sep 14, 2018, 11:51 pm Last Edit: Sep 14, 2018, 11:53 pm by Delta_G
You posted between me writing that and getting it posted.  I see the code you're showing.  And no, that isn't some problem with the code.  You have something wrong with the way it is wired.  If the circuit is good then that code shouldn't have any problem reading steady. 

So if you want help with it we're back to asking for a schematic.

Please understand that we've solved these problems over and over and over again for people.  We kind of know what sort of information is going to make it easy to solve.  Believe me, it's not a pissing match.  Please check the attitude, get over what one poster or another says and get focused on fixing stuff.  If you keep jockying with other posters then I'll find another thread to help on and you can sit here and rot with them.  If you're really here for help then get over that stuff and stay on track. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

davepl

Attitude?  Stay on track? 

You mean the part where I asked a nice question and you guys acted like superior arses?  I'd rather debug it than accept your help even if there was a CHANCE you had an idea on this one, but you don't, and I have no interest in putting up with the pedantic nonsense.


DrAzzy

#10
Sep 14, 2018, 11:57 pm Last Edit: Sep 15, 2018, 12:06 am by DrAzzy
The fact that adding a delay changes it is weird - when I read the topic, I assumed this was the usual behavior where you need to do a few analogRead()'s to fully charge/discharge the S/H cap. But the S/H cap is only connected during the conversion process.

To your point of why are we asking for all this information - it's because when we read potentiometers, we don't use a delay and we get a few LSB's of jitter, that's it. So the next step is to determine what you are doing differently from what we are doing that is resulting in your problem. And instead we get attitude, which isn't really appropriate when we're the one's helping you for free - as to why we're so short with people who don't include code and schematic, it's because we get very very tired of people posting questions without enough information for us to help them (even though there's a sticky thread with forum guidelines saying to post schematic and code when asking a question).

Almost every question posted (95%+ it's a rare treat to find one that doesn't) requires the code and/or schematic to solve - yet people almost never post them right off. And it's not infrequent for the code and/or schematic to reveal a fundamental misunderstanding not reflected in the text of the post, the resolution of which solves the problem.

The person with the gas sensor getting effectively random values - I'm going to guess that's a hardware problem "effectively random" (instead of "near the value but with too much jitter") being the giveaway for a floating pin (ie, I would venture a guess that his analog pin is not connected to anything)
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

AWOL

#11
Sep 15, 2018, 12:04 am Last Edit: Sep 15, 2018, 12:34 am by AWOL
It's not "being superior", or "a pissing contest" or "being an arse", it's the scientific method.

Being presented with an observation or claim, and being given the means to reproduce or refute it.

Maybe it is different in your world.

"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Delta_G

#12
Sep 15, 2018, 12:09 am Last Edit: Sep 15, 2018, 12:12 am by Delta_G
Attitude?  Stay on track? 

You mean the part where I asked a nice question and you guys acted like superior arses?  I'd rather debug it than accept your help even if there was a CHANCE you had an idea on this one, but you don't, and I have no interest in putting up with the pedantic nonsense.


Where did I act like some superior ass?  I just asked you to ignore the rest of the noise if you want me to work with you.  I will take that as a no. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

davepl

No, Delta_G is fine, it's AWOL that's the problem.  I'd report him to a moderator, but...  Anyway, my apologies to Delta_G, who was helping.

Anyway, I solved my issue (and the schematic didn't change).  I assume it's specific to the ESP32 so not of general interest anyway.  PM me if you run into and need the solution.

AWOL

#14
Sep 15, 2018, 12:36 am Last Edit: Sep 15, 2018, 03:47 pm by AWOL
Quote
I assume it's specific to the ESP32
And this inconsequntial nugget we find out in reply #13.


Start here - it'll save us all a lot of time.

Quote
You're running my code right now.
I find that statement quite disquieting.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up