Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ADC not playing nice

Status
Not open for further replies.
MrDEB !!!! Before Jon pops a gasket..

I don't have another gasket to pop, but I appreciate the concern ;)

I am just mystified that the order of doing things is such a difficult concept to grasp.

SETALLDIGITAL sets all pins to digital*.

If you set up a pin for analog input (i.e., as an ADC input), shouldn't it be obvious that following that with SETALLDIGITAL undoes everything you just did? That's why I attempted to get MrDEB to think about what actually is going on**, but I know that's never going to happen.

*notwithstanding tumbleweed's detailed explanation.

**what is actually happening, not some blurry, hand-waving idea.
 
Although I agree with Mr tumble... I have never had much luck reading an ADC channel when the pin wasn't configured to do so..

I didn't seem to get reliable output... Possibly because the digital circuit was engaged..
 
I got my LEDs to cooperate with the ADC
now back to the timer and get that to play with the ADC
here is what I came up with and seems to work as planed. Just don't spill your coffee or ? on your keyboard when you read my code. going to use the counter in another routine
Code:
Sub Stage()
    
    ADVal_1 = ADC.Read(0)
      
        If ADVal_1<=400 Then      //pre stage
        blue1=0
        blue2=1
        counter=1
       EndIf
                If ADVal_1<=300 Then     //staged
                blue2=1
                blue1=1
                counter=2
                EndIf
                        If ADVal_1<200     //deep stage
                       Then
                        blue1=1
                        blue2=0
                        counter =3
                        EndIf     
                            
                      
       End Sub
 {
 
I would think you'd want that to be arranged in a little different order, but I guess that's just me.

I have never had much luck reading an ADC channel when the pin wasn't configured to do so..
Ian, it wouldn't surprise me in the slightest if there are chips where you have to do that.
It's always worked for me (when I did it by mistake), but then again I usually try to set them the right way around in the first place!
 
If you set a pin to output and read the value of that pin with the ADC then you will get 0 or 1023 dependent on whether the output is low or high.

Mike.
 
Code:
Sub Stage()
    
    ADVal_1 = ADC.Read(0)
      
        If ADVal_1<=400 Then      //pre stage
        blue1=0
        blue2=1
        counter=1
       EndIf
                If ADVal_1<=300 Then     //staged
                blue2=1
                blue1=1
                counter=2
                EndIf
                        If ADVal_1<200     //deep stage
                       Then
                        blue1=1
                        blue2=0
                        counter =3
                        EndIf     
                            
                      
       End Sub
 {

This code will appear to work ok, but it's going to result in (very brief) extraneous flashes. It demonstrates a really poor understanding of what's going in. Imagine that!

Say the ADC value is 100. What happens in this loop?

100 is less than 400 so
Blue1 = 0
Blue2 = 1
Immediately thereafter, 100 is less than 300 so
Blue1 = 1
Blue2 = 1

And Immediately after that, 100 is less than 200 so
Blue1 = 1
Blue2 = 0


You really want each test to respond to exactly one condition.

Use a select/case statement

Code:
Select ADCVal_1
    Case > 400
         'do nothing 

   Case > 300
        Blue1 = 0
        Blue2 = 1 

   Case > 200
       Blue1 = 1
       Blue2 = 1

    Case <=200
        Blue1 = 1
       Blue2 = 0

End Select

In a select/case statement, the first case statement that is TRUE is the only one that will be executed. All others are ignored. The key thing to remember is the order. Each test must be arranged so that only the desired condition can trigger it.

Believe me, I am well aware that MrDEB will likely ignore this. I post it in the hopes that others may benefit from it.
 
am aware of using a SELECT CASE tried that
in this project the player can turn on one or two LEDS by rotating the pot.
will try your suggestion
thanks
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top