Author Topic: Help with analog input, multiple buttons dilemma  (Read 23090 times)

0 Members and 1 Guest are viewing this topic.

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #25 on: June 03, 2012, 05:09:15 pm »
1) Clearly know when the timer starts
2) warnings about finishing (10 mins-5 mins, 10 sec?)
3) Some "mode" like if you hold the same button that started the timer (sensed thanks to the induction sensor) led blinks and you can reset or change the time? something like that.

1) Turn the led on
2) Blink the led
3) You will not be able to detect the button when the relay is powered

So, just one led -> one I/O port saved  :)

Why not? I have tested it :)
My website: http://ried.cl
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #26 on: June 03, 2012, 06:54:16 pm »
Looking at your schematics in your first post the relais bridges the button when it is activated, so you can detect a button press when the thing is 'off' but not when it is 'on'.

Keyboard error: Press F1 to continue.
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #27 on: June 03, 2012, 07:07:46 pm »
Another thing is... Do you really need that resolution in timing? I mean, if you would be ok with 2 hour steps (2-4-6-8...) it would be easy to hold the button and let the LED flash every 0.5 seconds or so, and every flash is 2 hours, then you would not need the dipswitch at all.
Just count the flashes, if you want 10 hours release the button after 5 flashes and the relais switches on for 10 hours.
I can't imagine that you are going to change the switch every time when you want to use it anyway.
Keyboard error: Press F1 to continue.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #28 on: June 03, 2012, 08:39:19 pm »
Looking at your schematics in your first post the relais bridges the button when it is activated, so you can detect a button press when the thing is 'off' but not when it is 'on'.

That's why I have a current sensor :) (the coil) so current will pass thru the bridge if the button is down (I tested it and it works... most of the time because the resolution of the coil is low, so if I don't have anything connected I almost don't sense the 220V->5V transformer)

I think you are right about the buttons, maybe even the same "start" button can be used to program the timer holding it down led starts to blink (Setting time in a sucession, very fast blink 10 mins-30-1 hr, slow blinks adding 1 hour until you release the button) and when you release it starts... But I had to test TerminalJack's circuit and code first so then I will decide then how to implement it.
My website: http://ried.cl
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #29 on: June 03, 2012, 08:57:40 pm »
That's why I have a current sensor :) (the coil) so current will pass thru the bridge if the button is down (I tested it and it works... most of the time because the resolution of the coil is low, so if I don't have anything connected I almost don't sense the 220V->5V transformer)

So exactly what current is going to flow when you press the button when the relais already closed the circuit?
Maybe I'm missing something....
Keyboard error: Press F1 to continue.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #30 on: June 03, 2012, 10:02:33 pm »
Probably the half of the one passing thru the relay, but I am not sure, if I read very fast the coil I get like 0,0,0,1,100,140,100,1,0,0,0,... so I save only the >0 values and I connected my dremel to it:

Code: [Select]
0,0,1,0,0,0,1,... nothing pressed
100,1,100,100... button pressed

I am really not an expert, my first guess was this maybe produce something bad (I don't  feel comfortable with the AC current behaviour, so thinking on spliting the cable makes me think about some power factor issues, but since the wire is so short +50hz nothing bad happened :D)
« Last Edit: June 03, 2012, 10:04:47 pm by Erwin Ried »
My website: http://ried.cl
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #31 on: June 04, 2012, 12:16:37 am »
Your circuit ROCKS! haha worked like  a charm, very stable, for now I am doing:
http://youtu.be/z5L22bE3DlU

Code: [Select]
void setup() {               
  Serial.begin(9600); 
  delay(2000);
}
long total;
void loop() {
  Serial.print("Result: ");
  total = analogRead(1);
 
  for(int i = 0; i<1000; i++)
  {
    total+=analogRead(1);
  }
  Serial.println(total/1000);
  delay(1000);
}

I used a slightly bigger input resistor 10K (to keep low values lower otherwise the range was very small 1000-600, now goes from 1000 to 100 so at least 9 bits in range). Also the switch resistor I end using are 400k, 200k, 100k, 47k, 22k, 10k, 4.7k, 2.2k, now my idea is to quick parse all the analog values onto a 255 values array with the differences and as the processing time does not matter  I can  check every row of the table  and choose the nearest value before testing few times.

You can get rid of all that charging/discharging stuff.  I don't know why they did that.  You will have something like the attached for your circuit.  If you are reading 3 switches then use 2K, 4K and 8K for the resistor values.  The capacitor doesn't need to be nearly as big as what you used.  10nF to 100nF should be fine.

All you'll have to do then is configure the pin as analog and read the value.  To figure out which switches are closed you'd just do like they did in their code.  (Sorta.  They have a bug.)

For three switches you'd say:

Code: [Select]
uint8_t SwitchSettings = ((10240000 / AdcValue - 10000) * 8 + 5000) / 10000;
AdcValue is the value returned from analogRead();

The template for this that will work with any number of ADC bits and any number of switches (up to eight) is:

Code: [Select]
uint8_t SwitchSettings = ((<10000 * 2^NumAdcBits> / AdcValue - 10000) * <2^NumSwitches> + 5000) / 10000;
Just fill in the information in brackets.  The bug they have is that they are multiplying by 63 when they should be using 64.  This is the number of switches or 2^6.

I'm going to play around with the circuit some more tomorrow.  I want to create some graphs to see how the nominal node voltages are distributed and see if the above equation is appropriate.  It may need some tweaking.  I'll let you know what I find out.

Edit: Fixed template code.
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #32 on: June 04, 2012, 12:47:58 am »
Cool!  I like the idea of a look-up table but you may still have to use the formula to determine the switch positions on the ATtiny since you may not have room for a look-up table.  The table is nice, though, since it will let you use fairly arbitrary resistor values.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #33 on: June 04, 2012, 12:59:56 am »
Cool!  I like the idea of a look-up table but you may still have to use the formula to determine the switch positions on the ATtiny since you may not have room for a look-up table.  The table is nice, though, since it will let you use fairly arbitrary resistor values.

OH, you are right I have to test if it will fit in the flash memory, but at least making the lookup table allows me to re-use the data to make a fine tuned formula (with lagrange or a similar procedure).
My website: http://ried.cl
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #34 on: June 04, 2012, 04:04:23 am »
The table was looking very promising but a collision appeared :P so I think I will tweak the resistors a bit until I get a better range for the higher ones, probably I should start with 470 ohm or 1k

My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #35 on: June 04, 2012, 05:54:22 am »
It probably won't matter to you since you aren't using the same circuit but I made a simple tweak to the equation.

Code: [Select]
uint8_t SwitchSettings = ((<10000 * 2^NumAdcBits> / (AdcValue + 1) - 10000) * <2^NumSwitches> + 5000) / 10000;
This is just to ensure that the number of hits on either side of each nominal ADC value is nearly equal.  What I mean by nominal ADC value is the ADC value as calculated for each switch position.

If you do use that then you'll also need to ensure you don't use any AdcValue that is less than the smallest nominal ADC value.  Otherwise you'll get bogus results from the equation.  You can calculate this smallest nominal and plug it into your code as a constant.

Here's what I have in Perl.  Note that the function calculates $SmallestNominal at runtime but the code for you microcontroller won't have to do that since you'll know everything at compile time.

Code: [Select]
sub PollSwitch($$$)
{
    my ($ADCValue, $ADCBits, $NumSwitches) = @_;

    my $pv = ParallelValue(2**$NumSwitches - 1, $NumSwitches); # Value for all switches closed.
    my $SmallestNominal = int (2**$ADCBits * $pv / (1000 + $pv) + 0.5);

    $ADCValue = $SmallestNominal if $ADCValue < $SmallestNominal;

    return int ((2**$ADCBits / ($ADCValue + 1) - 1.0) * 2**$NumSwitches + 0.5);
}

Anyway, good luck with your circuit and thanks for bringing the Asuro circuit to my attention.  I was actually looking for a circuit to convert DIP switch settings to analog just a month or two ago.  The best I came up with at the time was an R2R network so it's good to know about this circuit.
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #36 on: June 04, 2012, 08:02:59 am »
The table was looking very promising but a collision appeared :P so I think I will tweak the resistors a bit until I get a better range for the higher ones, probably I should start with 470 ohm or 1k


I think your AnalogRead should be slowly decreasing in value but in the table the values are all over the place...
Keyboard error: Press F1 to continue.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #37 on: June 04, 2012, 05:12:49 pm »
The table was looking very promising but a collision appeared :P so I think I will tweak the resistors a bit until I get a better range for the higher ones, probably I should start with 470 ohm or 1k


I think your AnalogRead should be slowly decreasing in value but in the table the values are all over the place...

You think the values should be decreasing based on what? :P what about the button are you convinced?
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #38 on: June 04, 2012, 05:27:42 pm »
Those numbers don't make sense to me either.

No offense but I think you are fighting a losing battle by trying to get 8-bits of information out of that circuit using a 10-bit ADC.

You might consider using a 14-pin ATtiny.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #39 on: June 04, 2012, 07:02:25 pm »
Those numbers don't make sense to me either.

No offense but I think you are fighting a losing battle by trying to get 8-bits of information out of that circuit using a 10-bit ADC.

You might consider using a 14-pin ATtiny.

hahaha we will see soon :P
My website: http://ried.cl
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #40 on: June 04, 2012, 07:11:27 pm »
Those numbers don't make sense to me either.

No offense but I think you are fighting a losing battle by trying to get 8-bits of information out of that circuit using a 10-bit ADC.


Exactly the point I am trying to make, but like he says: we'll see.
Keyboard error: Press F1 to continue.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #41 on: June 04, 2012, 07:52:24 pm »
I guess there's nothing wrong with trying.  Even if you try and fail you always learn something.

You might write a little program to analyze your resistor network.  Kind of like the Perl script I wrote.  You can then plug in the exact values of the resistors you're using just to see if they will even work in theory.  If they don't then there's there no reason to even build it.  If your program spits out raw X, Y data (for switch setting and node voltage/ADC value) then you can use something like Open Office to graph it out.  This can give you a lot of insight into how your network will behave for each of the switch positions.

Also, you should be able to find a ton of Arduino code out there that will oversample.  What you're doing now isn't oversampling.  When you oversample you will get more bits of data.

 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #42 on: June 04, 2012, 08:18:32 pm »
Sure, I need some free time to finish my tweaks. I need to reach at least 2-4 decimals between any possible combination. Right now I think it is going very well (the battle to squeeze 8 bits)

I made a macro to capture the values quickly into the spreadsheet, for sure I am going a little far than required for this thing (an atmega168 would be better, +1 usd) but this is just fun to do and also I didn't found any info about 8 bits in a 10 bit adc, so that adds more fun :)
My website: http://ried.cl
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #43 on: June 04, 2012, 09:12:22 pm »
The problem with what you are trying to do is that you need high accuracy resistors, if you want to do 8 bits you need better than 0.4%.
Something to read: http://en.wikipedia.org/wiki/Resistor_ladder

I'm not trying to criticise what you are doing, and you can only learn from trying, but don't be disappointed if it's not going to work this way.
Keyboard error: Press F1 to continue.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #44 on: June 05, 2012, 03:58:12 am »
The problem with what you are trying to do is that you need high accuracy resistors, if you want to do 8 bits you need better than 0.4%.
Something to read: http://en.wikipedia.org/wiki/Resistor_ladder

I'm not trying to criticise what you are doing, and you can only learn from trying, but don't be disappointed if it's not going to work this way.

I know, thanks anyway.

Playing with some values for like 2 hours I think the best combination I can make is http://screencast.com/t/B3Tujz8l 7k in the cap and start by 560k as the highest switch (to get near the 0,0048828125 V resolution of the atmega analog input) for sure I will feel safer using 7 bits and not 8 bits :P so I will make the circuit soon, I need to see how the non-virtualized ADC behaves!

BTW, I will certainly not be disapointed by a failure ;D this small experiment teached me a lot already
My website: http://ried.cl
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5171
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #45 on: June 05, 2012, 06:47:04 pm »
Keep in mind that your resistors MUST have an exact 2:1 ratio (ok... 1:2:4:8:16:32:64:128), otherwise your output will not be a steady rise or fall when changing the switches.
Keyboard error: Press F1 to continue.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #46 on: June 05, 2012, 08:30:10 pm »
I think he's trying to make use of the other half of the ADC range.  That and maybe stretch out the range for each of the combinations at the lower end. 

See the attached chart to see what I'm describing.  This is for a 4-position DIP switch with 2K/4K/8K/16K resistors.  The blue line is the switch settings as a function of ADC value.  Where the orange line intersects the blue line is the nominal ADC value for that switch combination.

Notice how the entire bottom half of the ADC range (0 thru 511) is unused.  Also, notice how the steps are narrower toward the left.

I've tried to think of how to make use of the lower half of the ADC range.  One way would be to tie the commons on each of the switches (which are now connected to ground) to a negative voltage, equal in magnitude to Vcc.  The problem with this is if you don't already have this voltage available then it is probably just as easy to use an R-2R network.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #47 on: June 05, 2012, 08:50:56 pm »
I think he's trying to make use of the other half of the ADC range.  That and maybe stretch out the range for each of the combinations at the lower end. 

See the attached chart to see what I'm describing.  This is for a 4-position DIP switch with 2K/4K/8K/16K resistors.  The blue line is the switch settings as a function of ADC value.  Where the orange line intersects the blue line is the nominal ADC value for that switch combination.

Notice how the entire bottom half of the ADC range (0 thru 511) is unused.  Also, notice how the steps are narrower toward the left.

I've tried to think of how to make use of the lower half of the ADC range.  One way would be to tie the commons on each of the switches (which are now connected to ground) to a negative voltage, equal in magnitude to Vcc.  The problem with this is if you don't already have this voltage available then it is probably just as easy to use an R-2R network.

Exactly, I tested this again today, and result was much better with the circuit simulated, I had in total 8 collisions, in the 255 possible combinations. Range was like 10% wider (238 to 1024).

I am tweaking for the last time today with other values (I am trying to find the exact 2:1 ratio per resistor with http://www.sengpielaudio.com/calculator-paralresist.htm) as PA0PBZ commented this is also important.
« Last Edit: June 05, 2012, 08:56:33 pm by Erwin Ried »
My website: http://ried.cl
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 202
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #48 on: June 07, 2012, 06:39:13 pm »
The dilemma still continues 8)

Today I decided to look your perl app TerminalJack505, executed fine in an online interpreter (I don't ever used perl or saw perl before) and I played a bit with it, but I am a very visual person so I thought it was a nice idea to make a generic helper (from 1 to 32 resistors, but I need to do some low priority threading in the future because over 16 resistors updates are very slow because how much combinations you can do) for the dilemma, so I coded a small helper:


Now I am ready to continue ;D and I think this thread will be very helpful for other people in the future!

As far I can see, there is no a perfect solution, I mean, if you try to use a wider range you lose "distance" between outputs, also the "sweet" spots are very picky so if you go a little far or near you impact the whole system easily. So for now I will test some circuit keeping the min distance above 4 mV and try to keep the center of the outputs in the middle and I think that will work. http://screencast.com/t/hxrMsxin
« Last Edit: June 07, 2012, 06:45:40 pm by Erwin Ried »
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #49 on: June 07, 2012, 08:54:49 pm »
No, I don't think there is a perfect solution--one where all the outputs are equidistant.  This would be a linear function, obviously, as all the switch setting values wold lie on the same line. 

You can solve the linear system for individual resistors.  For example, for a 4-position switch, using a single unit value for the resistor tied to Vcc, the resistor values (multiples of the unit value resistor) 2.86415, 6.69925, 14.28358 and 29.11765 all fall on the line described by the equation...

y = -x/33 + 31

where y is the switch settings (0 to 15) and x is the ADC value (>= 528.)

The problem is that the switch combinations (due to the parallel values of the resistors) don't give the appropriate value for y.

I think the lesson I have learned is that the circuit is good for only 4 or 5 switches when you have a 10-bit ADC.  The R-2R circuit might give you 7 or 8 with a 10-bit ADC.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf