Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 22:23 27 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Getting the best out of Pico ADC

     Page 16 of 17    
Author Message
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2136
Posted: 01:27pm 11 Sep 2023
Copy link to clipboard 
Print this post

> dim samples(3)
> for n=0 to 3:samples(n)=n+5:next
> Min = Math(MIN samples(), c%)
> ? Min, c%
5       0
> Max = Math(Max samples(), c%)
> ? Max, c%
8       3
>

" If the integer variable is specified then it will be updated
with the index of the maximum value in the array." Applies to  Math(MAX

The point is the index is the position in the array, not the value, that is the 'Min =' part.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 02:25pm 11 Sep 2023
Copy link to clipboard 
Print this post

Thank you for fast reply Phil sir. It was the element in the samples!() number in c% I wanted not the value.
So if I want the element with the lowest value it's
Min = Math(MIN samples(), c%)
where Min is the element number.
but I can't dim it. Is MIN a keyword?

 
NPHighview

Senior Member

Joined: 02/09/2020
Location: United States
Posts: 200
Posted: 02:32pm 11 Sep 2023
Copy link to clipboard 
Print this post

Stan -
This syntax is fairly unique to MMBasic, but described on p. 136 of the PicoMite user manual, https://geoffg.net/Downloads/picomite/PicoMite_User_Manual.pdf
- Steve
Live in the Future. It's Just Starting Now!
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 02:40pm 11 Sep 2023
Copy link to clipboard 
Print this post

This works but print 0's

   adc start samples!() 'get new samples
low% = Math(MIN samples!(),c%)
print low%'c%

I think c% should be c! if it's to hold samples!()
No, it has to be c%.
This is so confusing!!. I'll post humour.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 04:08pm 11 Sep 2023
Copy link to clipboard 
Print this post

  NPHighview said  Stan -
This syntax is fairly unique to MMBasic, but described on p. 136 of the PicoMite user manual, https://geoffg.net/Downloads/picomite/PicoMite_User_Manual.pdf
- Steve

Er.. not really mate. page 136. I knew the math stuff wasn't for noobs but some of it was easy to use.. It's me not the manual. Dunno why min is highlighted in mmedit as a keyword??.
cheers, stan.
ps what's the difference between
do:If samples!(c%) < 0.2 then if samples!(c%+1) > 0.1 then exit do
and
do:If samples!(c%+1) > 0.1 then if samples!(c%) < 0.2 then exit do
except the priority.. and it's better than and:)
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6100
Posted: 09:21pm 11 Sep 2023
Copy link to clipboard 
Print this post

  stanleyella said   Dunno why min is highlighted in mmedit as a keyword??.
cheers, stan.

MATH(MIN... is a function that returns the minimum from an array,
MIN( is a function that returns the minimum of two numbers/variables)

MMEdit highlights MIN because it is a keyword.
It also happens to be a parameter (for MATH)

Jim
VK7JH
MMedit   MMBasic Help
 
NPHighview

Senior Member

Joined: 02/09/2020
Location: United States
Posts: 200
Posted: 09:30pm 11 Sep 2023
Copy link to clipboard 
Print this post

Stan - the code you quoted,
do:If samples!(c%) < 0.2 then if samples!(c%+1) > 0.1 then exit do

and
do:If samples!(c%+1) > 0.1 then if samples!(c%) < 0.2 then exit do

is in two different SELECT CASE clauses. One is for rising-edge trigger and the other is for falling-edge trigger.

In both cases, I've modified the code to work around the mean value rather than values close to 0 VDC as there's no guarantee that the input will ever drop to zero. I use the math function to scan through the acquired voltage levels and look for such transitions based on which trigger type is selected.

My most recent "TwoTrace.bas" code is more complex (sorry!) in that the trigger can now be in either channel 1 (GP26 / Pin 31) or channel 2 (GP27, Pin 21). The CASE clauses select which buffer will be used to calculate the mean value, and which buffer will be scanned to try to find the appropriate transition (below-to-above the mean value or above-to-below the mean value).
Live in the Future. It's Just Starting Now!
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 01:37pm 12 Sep 2023
Copy link to clipboard 
Print this post

  TassyJim said  
  stanleyella said   Dunno why min is highlighted in mmedit as a keyword??.
cheers, stan.

MATH(MIN... is a function that returns the minimum from an array,
MIN( is a function that returns the minimum of two numbers/variables)

MMEdit highlights MIN because it is a keyword.
It also happens to be a parameter (for MATH)

Jim

Thanks Jim, it's page 133
MAX( arg1 [, arg2 [, …]] )
or
MIN( arg1 [, arg2 [, …]] )
Returns the maximum or minimum number in the argument list.
Note that the comparison is a floating point comparison (integer arguments
are converted to floats) and a float is returned.

adc start samples!() 'get new samples
min=Math (MIN samples!())
print min
end

I get
>
RUN
[19] min=Math(MIN samples!())
Error : MIN is not declared
>
I cut and pasted but in mmedit min is MIN, it makes it a blue keyword.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 02:22pm 12 Sep 2023
Copy link to clipboard 
Print this post

Hi NPH. I thought a "scope" would be interesting but only for the pico board where signals would be 0V.. maybe with noise. I will try my simple scope with the sound filter I use with vga mmbasic to see the tone/sine with carrier, hopefully not. It should be 0 to 3.3V... yes? stan
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 02:30pm 12 Sep 2023
Copy link to clipboard 
Print this post

This is how I've done AC inputs on a PCB that I haven't posted on here yet.

oscope.pdf
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 02:54pm 12 Sep 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  This is how I've done AC inputs on a PCB that I haven't posted on here yet.

oscope.pdf

Good idea, connecting to 5V logic and pico gone. so easy to go over 3.3V.
how do auto range multi meters work? select vac and stick 240v mains across probes no prob. rhetorical.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 03:26pm 12 Sep 2023
Copy link to clipboard 
Print this post

I can't work out how to make/use math min a function. sorry I'm thick.
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4246
Posted: 05:27pm 12 Sep 2023
Copy link to clipboard 
Print this post

Assume you have an array() woth values


A=MATH(MIN array(),B)

A=lowest value in the array
B=the place in the array where the lowest valu is
PicomiteVGA PETSCII ROBOTS
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 06:09pm 12 Sep 2023
Copy link to clipboard 
Print this post

Thanks Volhuout.
 do
   adc start samples!() 'get new samples
indx%=Math (MIN samples!(),low%)
print indx% , low%
loop
end

gives

not the lowest element.
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4246
Posted: 06:28pm 12 Sep 2023
Copy link to clipboard 
Print this post

Oh yes, the lowest value is 0, found at location 147 in the array.
Your index is the low value, ypur low value is the index.
And you have used an integer. So when the array value is 0.4, a 0 is printed
PicomiteVGA PETSCII ROBOTS
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 09:12pm 12 Sep 2023
Copy link to clipboard 
Print this post


do
adc start samples!() 'get new samples
low!=Math (MIN samples!(),indx%)
print indx% , low!
pause 500
loop
end

gives following so indx% is the index pointer to lowest value in each samples!()
At last and not useful after all.

307      0.00967032967
28     0.01047619048
590      0.0008058608059
636      0.0008058608059
38     0.01047619048
5    0.01047619048
218      0.0008058608059
193      0.0008058608059
37     0.001611721612
18     0.01047619048
34     0.001611721612
182      0.0008058608059
455      0.0008058608059
18     0.01047619048
344      0.00967032967
97     0.0008058608059
14     0.01047619048
222      0.0008058608059
168      0.00967032967
465      0.00967032967
88     0.00967032967
39     0.001611721612
28     0.006446886447
2    0.01047619048
27     0.001611721612
11     0.006446886447
5    0.006446886447
43     0.01047619048
45     0.006446886447
18     0.01047619048
27     0.006446886447
21     0.006446886447
2    0.01047619048
48     0.0008058608059
13     0.01047619048
436      0.0008058608059
28     0.0008058608059
20     0.001611721612
445      0.0008058608059
6    0.01047619048
3    0.01047619048
585      0.00967032967
30     0.001611721612
14     0.006446886447
553      0.00967032967
8    0.0008058608059
3    0.01047619048
34     0.006446886447
279      0.0008058608059
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2136
Posted: 09:52pm 12 Sep 2023
Copy link to clipboard 
Print this post

  Quote  At last and not useful after all.

??? The Math Function requires you to have the variable Low! but you can just ignore it and use index% as your trigger point for the display.
Memory Copy can then be used to copy from that array Hres. values starting at index% to another array which you then display.

Edit
Perhaps something like this may work when added to your program.
'setup
HRes = MM.HRes
Dim float samples!(HRes * 2)
Dim float display(HRes)
disp.addr = PEEK(VARADDR display())
samp.addr = PEEK(VARADDR samples!())

'main
 low! = Math(MIN samples!(),indx%)
 Memory copy FLOAT samp.addr + indx% * 8, disp.addr, HRes

Then send display() to the screen.

Edited 2023-09-13 08:35 by phil99
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 07:58pm 13 Sep 2023
Copy link to clipboard 
Print this post

I wire the sound filter to the adc input and left 1000Hz right 2000Hz
tone looks like... and it's within 0 to 3.3V



left and right look like


vero fan moi

Edited 2023-09-14 06:01 by stanleyella
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 08:20pm 13 Sep 2023
Copy link to clipboard 
Print this post

  phil99 said  
  Quote  At last and not useful after all.

??? The Math Function requires you to have the variable Low! but you can just ignore it and use index% as your trigger point for the display.
Memory Copy can then be used to copy from that array Hres. values starting at index% to another array which you then display.

Edit
Perhaps something like this may work when added to your program.
'setup
HRes = MM.HRes
Dim float samples!(HRes * 2)
Dim float display(HRes)
disp.addr = PEEK(VARADDR display())
samp.addr = PEEK(VARADDR samples!())

'main
 low! = Math(MIN samples!(),indx%)
 Memory copy FLOAT samp.addr + indx% * 8, disp.addr, HRes

Then send display() to the screen.

Nice one Phil but I'm not knowing your code yet. Another prob is the lowest val is element 600+ in a 640() array.
cheers, stan
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2136
Posted: 11:10pm 13 Sep 2023
Copy link to clipboard 
Print this post

  Quote  Another prob is the lowest val is element 600+ in a 640() array.
Yes, that will always be an issue if you want to use the highest or lowest values as they only occur only once each in the whole array (the individual highs and lows will rarely ever be exactly the same as each other). And those points could be anywhere. If the array is much bigger (HRes * 5 or more) it will happen less often.

You need to stop the search HRes samples before the end which MATH(MIN can't do.

Searching through the array to find a point somewhere between min. and max. has the added complication that you must also determine if the samples are increasing or decreasing as they pass through that point. That is how the other scope programs do it, though using different methods.
disp.addr = PEEK(VARADDR display()) 'find the memory address of the start of the output array.
samp.addr = PEEK(VARADDR samples!()) 'find the memory address of the start of the input array.

Memory copy FLOAT samp.addr + indx% * 8, disp.addr, HRes 'Copy HRes samples from the input array's memory address, starting at the indx%*8 byte memory position (each sample is 8 bytes) to the output array's address
This shifts the data you want to display to align with the start of the output array.

Edit.
  I said  You need to stop the search HRes samples before the end which MATH(MIN can't do.
To get around that first copy samples!() minus the last HRes samples to a another array that is HRes shorter than samples!().
Then find  indx% in that array. This ensures there will always be enough samples left for the display.
' setup
Dim Integer HRes = MM.HRes, buff = MM.HRes * 3
Dim Float samples!(buff) 'input buffer using OPTION BASE 0
Dim Float samples2(buff)  'output buffer
Dim Float samples3(buff-HRes)  'temp buffer for trigger indx% search
samp1.addr = Peek(VARADDR samples!()) 'find the memory address of the start of the input array.
samp2.addr = Peek(VARADDR samples2())
samp3.addr = Peek(VARADDR samples3())

' main
Memory copy FLOAT samp1.addr, samp3.addr, buff-HRes 'copy to shorter temp array
low! = Math(Max samples3(),indx%) ' find the index of the minimum value
Memory copy FLOAT samp1.addr + indx% * 8, samp2.addr, buff-HRes 'copy to the output array, starting at indx%
Tested OK on my program but you may need to alter variable names to suit your program.
Edited 2023-09-14 13:33 by phil99
 
     Page 16 of 17    
Print this page
© JAQ Software 2024