Problem picking lowest ADC reading with Strain Gauge

Andrei IRL

Senior Member
Hello everyone.

I am building a sensor using strain gauge.

The sensor is built and amplifier made.

I am using READADC10 to convert my voltage reading into a variable, nothing new there.

I am not building a weighing scales but only a device that can monitor sensor reading and trigger a relay if the preset value has been reached.

Here is my issue, i have a routine at the beginning of the program that will be ran for 15 seconds.
This routine is taking ADC readings and i need it to same the smallest reading it gets within that 15 second window.
Then this value will be stored into EEPROM.

As i need the sensor to read in both directions, my at-rest ADC reading is somewhere in the middle of the ADC10 reading range, but can be between 400 and 600.

So my question is, how can i run a routine that will monitor ADC reading for 15 seconds and saves the smallest reading when value of ZERO will already be assigned to my variable by default?

Below is the example program i am trying to use.

Code:
do while time < 15
	time = time +1
	toggle greenled
	readadc10 4,w3					'w3-b6/b7
 		if w3<w4 then
   			let w4 = w3
 		end if
	pause 100
	loop
 

AllyCat

Senior Member
Hi,

If you know the ZERO value, then calculate the positive and negative absolute values, e.g w5 = w3 - zero : w6 = zero - w3.

Then you can calculate the overall minimum value (w4) with w4 = w4 MAX w5 MAX w6 (which I think was explained by hippy recently). It doesn't matter about "negative" values being created because they "wrap around" to become a very large number (which will be ignored by the minimum calculation).

For example, try something like w0 = 8 MAX 6 MAX 65530 in the simulator (w0 becomes 6). Or it can be done with IF .... THENs if you prefer.

Cheers, Alan.
 

BESQUEUT

Senior Member
Smallest :
Code:
symbol GreenLed=C.1
w5=12345


do
  	w4=60000
  	for b1=1 to 15
		toggle greenled
		readadc10 4,w3
		random w5		' for simulate only...
		w3=w5/66		' for simulate only...
		sertxd(" " ,#w3)	' for simulate only...
      	        w4=w3 max w4
		pause 1000
  	next b1

	toggle greenled
	Sertxd (13,10,"w4=" ,#w4,13,10)
	pause 10000
loop
Closest to ReadADC10=512 :
Code:
#simspeed 20
symbol GreenLed=C.1
w5=12345


do
  	w4=60000
  	for b1=1 to 15
		toggle greenled
		readadc10 4,w3
		random w5		' for simulate only...
		w3=w5/66		' for simulate only...
		sertxd(" " ,#w3)	' for simulate only...
		
		w3=512-w3
		if w3>30000 then
			w3=-w3
		endif
		sertxd ( " Delta=",#w3,13,10)
      	        w4=w3 max w4
		pause 1000
  	next b1

	toggle greenled
	Sertxd (13,10,"w4=" ,#w4,13,10)
	pause 10000
loop
 
Last edited:

hippy

Technical Support
Staff member
Preload the min and max readings. Set time to zero at the start and don't increment time within the loop ...

Code:
maxAdc = 0
minAdc = $FFFF
time = 0
Do While time < 15
  ReadAdc10 4, adc
  maxAdc = maxAdc Min adc
  minAdc = minAdc Max adc
  Pause 100
Loop
 

erco

Senior Member
Jeepers, it's so obvious when you put it like that, hippy. Thanks for that elegantly simple code snippet with DO WHILE TIME<N. I'll use that tonight in my current project which (related to another thread here) does an auto-off after a specified time with no user input. LIKE.
 

Andrei IRL

Senior Member
Thanks very much to everyone for taking the time to help me.

There is a wealth of knowledge in your answers.

I dont understand all of it but i'll run the examples ye have given to see whats what.

Thanks again.

Andrei.
 

AllyCat

Senior Member
Hi,

trigger a relay if the preset value has been reached.

i need the sensor to read in both directions, my at-rest ADC reading is somewhere in the middle

saves the smallest reading when value of ZERO will already be assigned
We have all given rather different answers because (IMHO) parts of your description can be interpreted in different ways. You say that the sensor reads in "both directions" , but a "maximum" (reached) in one direction is the same as a "minimum" (smallest) in the other direction.

I must admit that I slightly misread your description in that I assumed you have a variable named "zero", not just that it is a value assigned to a variable which will be used for another purpose.

Cheers, Alan.
 

Andrei IRL

Senior Member
Hi,



We have all given rather different answers because (IMHO) parts of your description can be interpreted in different ways. You say that the sensor reads in "both directions" , but a "maximum" (reached) in one direction is the same as a "minimum" (smallest) in the other direction.

I must admit that I slightly misread your description in that I assumed you have a variable named "zero", not just that it is a value assigned to a variable which will be used for another purpose.

Cheers, Alan.
Sorry, i was doing my best trying to describe the what it happening.

This code will be part of the larger code and will only be used as a "set-up" routine.

After that i will be running calibration routine.

Also, only one direction will ever be used at any one time, so i will have two set-up routines, depending on the direction that sensor is required to work in.

But the example codes in this post give me great ideas on how to modify my own code.

So thanks very much.
 

hippy

Technical Support
Staff member
Thanks for that elegantly simple code snippet with DO WHILE TIME<N.
15 seconds is 15000 ms, so, with "PAUSE 100" in the loop, 150 loops of 100 ms is 15 seconds, and no need to use 'time' at all ...

Code:
maxAdc = 0
minAdc = $FFFF
For counter = 1 To 150
  ReadAdc10 4, adc
  maxAdc = maxAdc Min adc
  minAdc = minAdc Max adc
  Pause 100
Next
 

Andrei IRL

Senior Member
15 seconds is 15000 ms, so, with "PAUSE 100" in the loop, 150 loops of 100 ms is 15 seconds, and no need to use 'time' at all ...

Code:
maxAdc = 0
minAdc = $FFFF
For counter = 1 To 150
  ReadAdc10 4, adc
  maxAdc = maxAdc Min adc
  minAdc = minAdc Max adc
  Pause 100
Next
Even better,

Thanks very much again hippy.
 

BESQUEUT

Senior Member
Ha ha. Sorry you are right.
Thank you
No problem.
I often add code to test program by simulation before posting, so it may seems more complex...
You did not specify how many samplings you want during 15s :
1) 15 samples ==> code #3
2) 150 samples ==> code #9
3) As many as possible ==> code #4 but without the PAUSE command.

IMHO, 3) is more suitable. But code #4 needs a TIME compatible Picaxe.

Looking at #1, it seems that you are not aware that TIME is a special variable : it autoincrement each second... (so no need for time=time+1...)
 
Last edited:

Andrei IRL

Senior Member
No problem.
I often add code to test program by simulation before posting, so it may seems more complex...
You did not specify how many samplings you want during 15s :
1) 15 samples ==> code #3
2) 150 samples ==> code #9
3) As many as possible ==> code #4 but without the PAUSE command.

IMHO, 3) is more suitable. But code #4 needs a TIME compatible Picaxe.

Looking at #1, it seems that you are not aware that TIME is a special variable : it autoincrement each second... (so no need for time=time+1...)
Thank you very much.

Its great to be learning and understanding new things within PICAXE Programming.

I love it.
 
Top