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

0 Members and 1 Guest are viewing this topic.

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Help with analog input, multiple buttons dilemma
« on: May 25, 2012, 10:36:32 pm »
I tried several days to get a solution to my dilemma, maybe some help will be useful. Long history short: I have a electric thing to put towels (http://goo.gl/kCeCZ) uses little energy but montly it is more than 1 towel, so I want to add a timer like an oven to this device to push a button and program let say, 5 hours.

OK. My current idea (I know I can use a mechanical timer or something like that but I wish to make a little device with a small attiny to learn):


The sensor is an inductance sensor to detect when the momentary button is pressed (after relay is connected by the attiny so in this way the relay works like a 'life-supporting' device and the attiny can kill itself).

So the first attached photo shows the current circuit, my small relay board, inductance sensor so I need to finish the software now. Here is the problem. This attiny has 4 analogs, 2 pwm. As I don't have an HV programmer I have 3 analogs, but I am using 1 as an output (2 leds and relay), one analog input pin for the current sensor and I want to connect a 8-dip-switch in the last remaining pin.

Base delay is 10s (instant off), and every switch will add some time to have a total of 16 hours max (8 bits):


Second attached photo shows the dip switch and my test environment with an arduino, I am guessing this is possible with some combination of current dividers and maybe another idea? (attiny IO is 10 bits so I think I can reach 8 bit resolution including some noise of the reads)

Last attached photo shows the attiny in the usb programmer, it is working well but I am debugging first the logic in an arduino because it is a lot easier with the usb serial.

So, any recommendation/idea?

Thanks!
My website: http://ried.cl
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: Help with analog input, multiple buttons dilemma
« Reply #1 on: May 25, 2012, 11:57:45 pm »
If you only have one pin left, and as time is an analogue quantity, why not use a POT, rather than switches and have the ADC set the delay according to the POT setting?


Cheers
Chris


Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #2 on: May 26, 2012, 12:12:32 am »
If you only have one pin left, and as time is an analogue quantity, why not use a POT, rather than switches and have the ADC set the delay according to the POT setting?


Cheers
Chris

Well because I don't think the pot will be as precise as 8 different switches for the time. If this approach is possible I easily can have at max 256 combinations (1 sec to 16 hours with 5 mins intervals), maybe a pot will work with a wide knob and a little more than 1 degree per 5 mins but I think this can be done with the 10 bits precision of the analog leg
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #3 on: May 26, 2012, 05:39:37 am »
One way to convert the 8-position DIP switch's setting to analog is to use an R2R ladder network.  To keep your circuit as simple as possible, each position of the DIP switch would need to be single-pole, double-throw.  See the first figure on this webpage for the basic idea.  (The op amp's not necessary.)

If your DIP switch is SPST then it won't be as simple since you'll need to introduce extra circuitry.  A MCU with more I/O pins may be your best bet.  Or buy a DIP switch with SPDT switches.  They're not very common, however.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #4 on: May 26, 2012, 07:13:57 am »
One way to convert the 8-position DIP switch's setting to analog is to use an R2R ladder network.  To keep your circuit as simple as possible, each position of the DIP switch would need to be single-pole, double-throw.  See the first figure on this webpage for the basic idea.  (The op amp's not necessary.)

If your DIP switch is SPST then it won't be as simple since you'll need to introduce extra circuitry.  A MCU with more I/O pins may be your best bet.  Or buy a DIP switch with SPDT switches.  They're not very common, however.

Wow! thanks a lot, I was looking for the "theory" related with this!
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #5 on: May 26, 2012, 08:59:39 pm »
As you can see from reading the Wiki article, doing this with 8-bits is going to require fairly precise resistors.  If this is a one-off project then you can handpick the resistors.

And, as I mentioned, if you are use a DIP switch with SPST switches you'll need extra circuitry. 

Because of these factors you may be better off just using a MCU with more I/O.

Assuming you go this route then the best way that I can think of to drive the ladder using SPST switches is to use an octal buffer/driver such as the 74HC241.  (This part has a tri-state feature that you wouldn't need; you always want the output to be 0V or Vcc.)  See the attached schematic for the basic idea.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #6 on: May 27, 2012, 06:31:34 am »
As you can see from reading the Wiki article, doing this with 8-bits is going to require fairly precise resistors.  If this is a one-off project then you can handpick the resistors.

And, as I mentioned, if you are use a DIP switch with SPST switches you'll need extra circuitry. 

Because of these factors you may be better off just using a MCU with more I/O.

Assuming you go this route then the best way that I can think of to drive the ladder using SPST switches is to use an octal buffer/driver such as the 74HC241.  (This part has a tri-state feature that you wouldn't need; you always want the output to be 0V or Vcc.)  See the attached schematic for the basic idea.

So the main issue is to not left pins floating with this dip switch right? I saw a robot kit http://www.arexx.com/downloads/asuro/asuro_manual_en.pdf that detects 6 switches (so maybe 8 it is possible, I don't need high speed response in fact I can take a lot of iterations to get the data, let say 1 minute (the coil to measure the current also requires lot of iterations, it is not very precise, in my tests like 100 ms of iterations gets a precise response about if the switch is down or not). I will try sóon.
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #7 on: May 27, 2012, 06:57:57 am »
Yes, if you use the R2R ladder then each node of the ladder needs to be driven by either 0V or Vcc (5V in your case.)  There might be a better way to do it than the R2R ladder.  The schematic in your link is doing something different.  I don't know if they can discern between multiple switch combinations (which is what you need) or just a single switch.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #8 on: May 27, 2012, 07:41:37 am »
I just looked a little closer at that PDF you linked to.  They do indeed discern between multiple switch combinations.  That's interesting.  It must not be terribly accurate judging from what they say about needing to call the function several times to get a correct answer.

They must be determining the switch positions based on how the capacitor charges up.
 

Offline richcj10

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: Help with analog input, multiple buttons dilemma
« Reply #9 on: May 31, 2012, 03:11:34 pm »
Why not a voltage divider circuit and enabling less or more resistors adjusts voltages...
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #10 on: May 31, 2012, 07:38:57 pm »
Why not a voltage divider circuit and enabling less or more resistors adjusts voltages...

I went through that particular train of thought before.  I couldn't come up with a satisfactory solution.

I'd like to see a schematic for your idea.  Remember, he has an 8-position SPST DIP switch and a 10-bit ADC.  That's 256 different switch combinations and an ADC value that ranges from 0 to 1023.  Although, his application can use oversampling, so he can probably get a 12-bit ADC value at least.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #11 on: May 31, 2012, 09:45:04 pm »
I will soon test the thing in the asuro pdf, seems very smart and I think I can get the 8 bit with the SPST DIP switch, I am just sorting out some resistors to start when I have a free time window :) I will post any result, it will be very interesting to get 8 bit or 10 from switches, I can imagine this little attiny with 40 switches! HAHA

Why not a voltage divider circuit and enabling less or more resistors adjusts voltages...

I went through that particular train of thought before.  I couldn't come up with a satisfactory solution.

I'd like to see a schematic for your idea.  Remember, he has an 8-position SPST DIP switch and a 10-bit ADC.  That's 256 different switch combinations and an ADC value that ranges from 0 to 1023.  Although, his application can use oversampling, so he can probably get a 12-bit ADC value at least.
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #12 on: June 01, 2012, 01:28:31 am »
I thought you only had one pin left.  The Asuro schematic is using two pins for the 6 switches.  I haven't looked at the code but I imagine one is for ADC and the other is for discharging the cap.

What are each of the pins on your ATtiny assigned to?  Are the ISP pins dedicated to programming?  Because if they are then there are ways to use them for other purposes.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #13 on: June 01, 2012, 07:25:10 am »
I thought you only had one pin left.  The Asuro schematic is using two pins for the 6 switches.  I haven't looked at the code but I imagine one is for ADC and the other is for discharging the cap.

What are each of the pins on your ATtiny assigned to?  Are the ISP pins dedicated to programming?  Because if they are then there are ways to use them for other purposes.

No discharging pin, one pin is for interruption mode, and one is for polling via code, both can work independently. I think this design is wondeful, there is a small sleep time in the code to allow the cap discharge so probably all is very open to tweaking.

Right now:
1 pin is for the relay
2 pins are for a dual color led (red-green), sounds overkill wasting a pin in this but I want to use this device and know what is doing without a screen
1 analog input is for the induction sensor coil
gnd, vcc and reset other 3 pins (reset can be an extra IO but it is not very convenient)
1 pin left is for the 8 switches
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #14 on: June 01, 2012, 07:31:29 am »
Ah!  I see.  I'll have to look at the code.  It sounds interesting.
 

Offline SeanB

  • Super Contributor
  • ***
  • Posts: 16362
  • Country: za
Re: Help with analog input, multiple buttons dilemma
« Reply #15 on: June 01, 2012, 03:11:43 pm »
You can drive the 2 leds with one pin, just put a pair of resistors across the supply to maker a half volt rail ( use 1k or so and high efficiency leds) and connect the leds in anti parallel in series with the micro output and the resistors. You can have off, red, green or orange by either tristate, high, low or oscillating the pin at 100Hz or so.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #16 on: June 02, 2012, 12:26:43 am »
You can drive the 2 leds with one pin, just put a pair of resistors across the supply to maker a half volt rail ( use 1k or so and high efficiency leds) and connect the leds in anti parallel in series with the micro output and the resistors. You can have off, red, green or orange by either tristate, high, low or oscillating the pin at 100Hz or so.

I tried first with a led with 2 colors and 2 leads but it was not working properly for some reason (I am very noob in electronics), I think the "fake ground" that the tiny makes is very weak or something like that I can't explain. The first simple circuit here: http://www.neufeld.newton.ks.us/electronics/?p=151 is the one you say?
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #17 on: June 02, 2012, 02:52:54 am »
I've been analyzing the Asuro circuit.  I wrote hacked-out a Perl script to determine the node voltages for each of the switch positions and calculated what the ADC value would be and used this value with the PollSwitch() function to see what it returns.  (I had to fix in bug in their calculation.)

I does indeed work.  If everything is nearly perfect, that is.  Nice thing about your case is that you're not building thousands of boards with the circuit so you can tweak the component values and code to suit your needs.

You will need at least 12 bits of resolution to use it with an 8-position switch.  That's assuming everything else is perfect--the MCU's ADC, the resistor values, etc.  Because of the circuit's design, only the top half of the ADC values are used.  That's basically throwing away half of the possible values.  Yes, that's just one bit but it is the most significant bit!  If you're going to throw a bit away throw away the least significant.

If you can free that pin up by driving the LEDs with just one pin then it will help you a lot since you can then divide the 8 switches into 2 groups of four which will be much more reliable.

I've attached the Perl script I used if you want to double-check my analysis or do your own.  Tweaking the values of the various parameters can be very informative.  I've also included the script's output for a couple of test cases: 6 switches, 10-bit ADC; 8 switches 10-bit ADC; 8 switches 12-bit ADC; 4 switches, 10-bit ADC.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #18 on: June 02, 2012, 05:09:52 pm »
I've been analyzing the Asuro circuit.  I wrote hacked-out a Perl script to determine the node voltages for each of the switch positions and calculated what the ADC value would be and used this value with the PollSwitch() function to see what it returns.  (I had to fix in bug in their calculation.)

I does indeed work.  If everything is nearly perfect, that is.  Nice thing about your case is that you're not building thousands of boards with the circuit so you can tweak the component values and code to suit your needs.

You will need at least 12 bits of resolution to use it with an 8-position switch.  That's assuming everything else is perfect--the MCU's ADC, the resistor values, etc.  Because of the circuit's design, only the top half of the ADC values are used.  That's basically throwing away half of the possible values.  Yes, that's just one bit but it is the most significant bit!  If you're going to throw a bit away throw away the least significant.

If you can free that pin up by driving the LEDs with just one pin then it will help you a lot since you can then divide the 8 switches into 2 groups of four which will be much more reliable.

I've attached the Perl script I used if you want to double-check my analysis or do your own.  Tweaking the values of the various parameters can be very informative.  I've also included the script's output for a couple of test cases: 6 switches, 10-bit ADC; 8 switches 10-bit ADC; 8 switches 12-bit ADC; 4 switches, 10-bit ADC.

Very nice, so 10 bit will not be enough for 8 switches :o
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #19 on: June 02, 2012, 07:52:00 pm »
Very nice, so 10 bit will not be enough for 8 switches :o

Nope.  Afraid not.  You might be able to get the resolution you need using oversampling.  On an ATtiny that may be a trick.

Can you can describe how the LED is going to be used and how it is wired?
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #20 on: June 02, 2012, 10:49:49 pm »
Hey thanks a lot you are always very helpful, I think Dave should send you some gifts haha! Well about the device, maybe it is a good idea to drop 1 led or retry to drive both with 1 leg, I have some light sequences in mind, but not a final diagram of all the states, 3 color seems just fine to me to achieve: 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.

Nevertheless I think I didn't get what the asuro code is doing:
http://pastebin.com/3QF8C19S

Can you explain the sequence in easy words? With the 4.7 uF cap it seems like a duracell :P the values drop very slowly (I had only 3 switches in the protoboard right now), I tried lowering the capacitor but 0.47 uF also is too high and with very small caps (tested a 33 pF) the value it is very oscillating, I am using wiring library for the arduino

Code: [Select]
  pinMode(A0, OUTPUT);
  digitalWrite(A0, HIGH);
  delay(10);
  digitalWrite(A0, LOW);
  pinMode(A0, INPUT);
  return analogRead(A0);
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #21 on: June 03, 2012, 05:14:04 am »
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.
« Last Edit: June 03, 2012, 08:01:12 am by TerminalJack505 »
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #22 on: June 03, 2012, 06:38:52 am »
WOW! thanks a lot again! I will assemble that tomorrow! great help! I hope the device works as I expect and my towels stop smelling 8) this was the final step besides programming the attiny
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #23 on: June 03, 2012, 07:56:53 am »
Oops.  Just notices I had an error.  I should have said...

Code: [Select]
uint8_t SwitchSettings = ((<10000 * 2^NumAdcBits> / AdcValue - 10000) * <2^NumSwitches> + 5000) / 10000;
...for the generic equation.

Right now my code is still in Perl so I haven't tried that in C yet.  Here's what I have in Perl:

Code: [Select]
return int ((2**$ADCBits / $ADCValue - 1.0) * 2**$NumSwitches + 0.5);
And when you convert that to do everything in integer math like they did in the Asuro code it should like the C code above.
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #24 on: June 03, 2012, 09:27:12 am »
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  :)
Keyboard error: Press F1 to continue.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • 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
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • 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.
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • 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: 206
  • 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
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • 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: 206
  • 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: 206
  • 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: 206
  • 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: 206
  • 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.
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • 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: 206
  • 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: 206
  • 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
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • 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: 206
  • 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
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • 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: 206
  • 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
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • 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: 206
  • 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: 206
  • 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.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #50 on: July 03, 2012, 04:49:10 am »
Hey! at the end I was not able to squeeze 8 bits using resistors and with the 10 bit analog pin as you predicted. Also the coil to detect when the button was down was not reliable, I don't think if it is the analog reference or what, but setting the reference to the internal 1.1V was very sensitive but consumption was not stable enought using the 220V-5V power adaptor as source (with independent supply, sensing the button was very stable).

So in conclusion (like to sum up all this topic) first I decided to take the idea proposed here about using a button to set the timer, and I use the eeprom to save the previous value, so in conclusion it works very nice:


About the switches, the best combination for 8 switches provided a nearly 1 decimal value of difference, having perfect resistors:
http://screencast.com/t/k3pt0pumD2s

But I was unable to replicate the same perfection in real life, always had more than 8 collisions. For 7 resistors, the minimum difference was 2, so in conclusion 6 switches is the safest option:
http://screencast.com/t/xUsaeI9ef

Those values were found by a solver in excel after a lot of hours of processing, so probably they are nearly perfect solutions for the problem.
« Last Edit: July 03, 2012, 04:51:09 am 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 #51 on: July 03, 2012, 05:11:16 am »
Glad to see you finished the project.  Even if the DIP switch idea didn't work out.
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #52 on: July 03, 2012, 06:40:21 pm »
Hi,

Good to see that you have a working product, thanks for posting the results.
Also nice to see that the things I pointed out came to be a reality.
That's probably only because I tried the same stuff and failed learned from that  ;)
Happy toweling!
Keyboard error: Press F1 to continue.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #53 on: July 07, 2012, 03:22:47 pm »
Hehe, well, I didn't understand a lot the resistor ladder things in first place, then realized about the limitations, but a lot later (that's why I was trying to do it anyway) but always there is something new to learn :)

May I ask you both a final question, I am getting some irregularities in the behaviour (timer usually works but sometimes it just hangs) SO, I am thinking about adding some caps to smooth the input, I measured the voltage of the cheap 220V to 5V adaptor I used, and for a 30 mins timeout period looks like this:


As you can see at startup Voltage reaches 10V (I think this is bad for the uC probably) sometimes I get a reset at the start, then it is a bit noisy but near the second 1400 there is a small drop (I turn both leds on 10 mins near the end of timeout). Also the timer in the Attiny need some calibration, it is fairy unprecise = +/-11%


Will this smoothing caps in parallel fix the issue? what do you think?

Or should I replace the supply for another one... like a more expensive one? (this one is very cheap, like 1.6 usd)
« Last Edit: July 07, 2012, 03:26:59 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 #54 on: July 07, 2012, 07:03:25 pm »
The caps won't hurt, obviously.  I assume you're using one of those wall adapters with a switching regulator in the plug.  If so then the caps are pretty much necessary since the voltage source is so far away from the load.  I don't know if they'll bypass that 10V spike or not, though.

The timer inaccuracy sounds like a software issue.  Off the top of my head, I think the internal RC oscillators should be something like +/- 2% accurate.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #55 on: July 07, 2012, 07:28:08 pm »
The caps won't hurt, obviously.  I assume you're using one of those wall adapters with a switching regulator in the plug.  If so then the caps are pretty much necessary since the voltage source is so far away from the load.  I don't know if they'll bypass that 10V spike or not, though.

The timer inaccuracy sounds like a software issue.  Off the top of my head, I think the internal RC oscillators should be something like +/- 2% accurate.

In fact Caps help a lot but not with the spikes >10V!, and yes I am using those wall adapters.

About the timer, I think in this case it should be calibrated:
http://forums.adafruit.com/viewtopic.php?t=5078

(I needed to calibrated another tiny in the past for this accelerometer datacollector but because the serial port was throwing garbage, not due long time precision, in oscilator serial data was a bit weird, then it worked nicely... but because that time I used just a 3V cell I didn't needed caps)


I used a slighly better wall adapter (like 2.5 usd) not one of the best, but at least it seems best  built and voltage + regulator is very clean now! I hope this closes the case for the towel's timer :P
My website: http://ried.cl
 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #56 on: July 07, 2012, 08:28:39 pm »
The red line (New PSU+ Caps) still does some nasty things at the beginning and end (12 volts spike?), so if your uC is happy with the 4V from the LM317 i'd leave it in.
Keyboard error: Press F1 to continue.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #57 on: July 07, 2012, 08:37:49 pm »
Yep, you're right about the +/- 10%.  I never realized it was that bad.

Atmel has an app note (AVR053) on using their various programmers/debuggers to do the calibration.  I've never tried it.  It seems like I've used avrdude to do it before, though.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #58 on: July 07, 2012, 09:13:16 pm »
Yes, with 4V works wonderfully! no more resets or hangs

About the auto calibration, I have a cheap ($4 usd) programmer from ebay, avrasp one, it is a pain to calibrate but it is once in each chip life :) so, not so bad. You need to set a serial terminal with the tiny
My website: http://ried.cl
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf