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.

Multiple switches on one analog input

Status
Not open for further replies.

Winch

Member
Hello a question,
I want to connect five push buttons to one analog input.
I was previously convinced that the code below should suffice for this problem.
But somehow he doesn't want to use the < , > code properly?
In the manual states that this should work with the CASe function.
Six standard comparison operators are available: =, <>, >, >=, <, <=. Multiple comma separated conditions can be used with CASE statements, also.
I use a display where I can read the results neatly. But I don't see any response in the result when pressing the switch.
I see the value of the analog input vary nicely but the number of the relevant switch remains and does not respond.
When I compile I don't get any errors so it seems to work fine.
Does anyone have a tip or idea?

Code:
Buttons:
    Adcin 0, Switch

Select Case Switch
    Case < 1023, > 1015  'Analog value = 1023
        Button = 1  'System is in idle mode
    Case < 729, > 709  'Analog value = 719
        Button = 2  'Switch Select
    Case < 482, > 462  'Analog value = 472
        Button = 3  'Switch Left
    Case < 304, > 284  'Analog value = 294
        Button = 4  'Switch Down
    Case < 142, > 122  'Analog value = 132
        Button = 5  'Switch Up
    Case 0  'Analog value = 0
        Button = 6  'Switch Right
    EndSelect


'=======01234567890123456789=======
Lcdcmdout LcdClear
Lcdcmdout LcdLine1Home
Lcdout #Button
Lcdcmdout LcdLine2Home
Lcdout #Switch

WaitMs 200
Return
 
I'd try using an it - else if - else - endif type structure instead..

The oshonsoft manual does not seem to give give any examples of multiple conditions or how they combine when used, so it's safer to stick with a direct comparison sequence.
 
You don't really need the pair of values in your case/select logic.

Select Switch

Case > 1015 'idle

Case > 709 'Switch 1 pressed

Case > 462 'Switch 2 pressed

Case > 284 'Switch 3 pressed







End Select


Case/Select tests until a condition is met, then exits. So if the value is greater than 1015, no button pressed and the statement exited.

If the value is less than 1015, but more than 709, button 1 is pressed, and the statement is exited.

And so on.
 
Thanks for your reply.
I've already looked at this, but then I get stuck on how best to approach this?
Importantly, I want to have the detection between two values. And I don't know how to approach this at "if else"
I wonder how you deal with two values where the result should lie between?
 
If the design is as you say when each button is pressed you get the values stated.. I did one similar to visitor, but I often get bitten, so I'll test my thought process first..

Why do you need to know > 1015 --1023 < as long as your voltage is above 1000 it will sense the button..

You are not trying to detect more than one button at a time are you..
 
Ok I will try this further.
Have used this before, but there is something wrong in my program, maybe the problem is somewhere else? Thanks I'll investigate further.

You don't really need the pair of values in your case/select logic.

Select Switch

Case > 1015 'idle

Case > 709 'Switch 1 pressed

Case > 462 'Switch 2 pressed

Case > 284 'Switch 3 pressed







End Select


Case/Select tests until a condition is met, then exits. So if the value is greater than 1015, no button pressed and the statement exited.

If the value is less than 1015, but more than 709, button 1 is pressed, and the statement is exited.

And so on.
 
Space the bands far enough apart, and it's not a problem. If you expect an ADC value of say 500, even with drift and component tolerances, it won't be too far from the number.

If you want to eliminate some regions, just do this:

Case > 1015 'no button pressed.

Case > 729 'dead zone, no action

Case > 709 'button 1 pressed

Case > 482 'dead zone, no action

Case > 462 'button 2 pressed

and so on.


Remember, as soon as a test is true, case/select exits. (Arduino land results are not the same, but for BASIC languages,this should be true).
 
If the design is as you say when each button is pressed you get the values stated.. I did one similar to visitor, but I often get bitten, so I'll test my thought process first..

Why do you need to know > 1015 --1023 < as long as your voltage is above 1000 it will sense the button..

You are not trying to detect more than one button at a time are you..

yes i only detect one button at a time!
 
Honestly I think what my problem is!
The intention is to press in and to come to rest when released.
That is not the case with this code.
This code does what it's supposed to do, indeed that's right!

Code:
Buttons:
    Adcin 0, Switch
    
Select Case Switch
    Case > 1015  'Analog value = 1023
        Button = 1  'System is in idle mode
    Case > 709  'Analog value = 719
        Button = 2  'Switch Select
    Case > 462  'Analog value = 472
        Button = 3  'Switch Left
    Case > 284  'Analog value = 294
        Button = 4  'Switch Down
    Case > 122  'Analog value = 132
        Button = 5  'Switch Up
    Case 0  'Analog value = 0
        Button = 6  'Switch Right
EndSelect

'=======01234567890123456789=======
Lcdcmdout LcdClear
Lcdcmdout LcdLine1Home
Lcdout #Button
Lcdcmdout LcdLine2Home
Lcdout #Switch

WaitMs 200
Return

I'll have to solve this in another way.
I have to think about that.
 
IMG20210706220450.jpg
 
In the meantime, I got what I wanted to achieve.
My problem lay in another piece of software that I overlooked.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top