Arrays of pins

I want to make a moisture sensor that will check up to eight pots. So I want to read, into an array, the input of each of the 8 A0..A7 pins. I don't see how I can get an array of pin numbers.

Consider the following pseudocode (i.e., I'm playing fast and loose wtih syntax)

Code: Select all | TOGGLE FULL SIZE
`start:   set pins to [A0, A1, ..., A7]   set dry_value to [1500, 1500, 2000, 1500, 1500, 1200, 2000, 1800]   set current_value to [0,0,0,0,0,0,0,0]   set blink_start to millis()   set blink_time to 200   set LED[pins.size() + 1] to BLUEforever do:   for(i in range 0..pins.size())      set current_value[i] to read(pins[i])      sleep(2 minutes)forever do:   if(millis() - blink_start > blink_time)         blink = !blink         blink_start = millis()   for(i in range 0..pins.size())      if(current_value[i] > dry_value[i])            if(blink)               set LED i to RED            else                set LED i to BLACK      else          set LED i to GREEN`

This will not be battery powered so the green lights and the blue light (meaning power-on) have no effect on the lifetime of the wallwart.

Any mechanism that lets me do the equivalent of pins[i] and lets me read the pins A0..A7 sequentially will work. I just don't want to repeat the same logic eight times. There also appears to be no way to pass a pin number to a function, or I could have eight threads each calling checkPin(i).

flounder

Posts: 465
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA

Re: Arrays of pins

Something like that should work fine, but you need to settle on an actual language.
In C++/Arduino, pins are identified by small integers, so it would be like:
Code: Select all | TOGGLE FULL SIZE
`     byte pins = {A0, A1, ..., A7}'       :     curvals[i] = digitalRead(pins[i]);`

While in CircuitPython (which I haven't used), it looks like pins are higher-level objects, so it'd probably look something like:
Code: Select all | TOGGLE FULL SIZE
`    pins = [AnalogIn(board.A1), ... AnalogIn(board.A7)]       :    curvals[i] = pins[i].value`

(except that using an index to step through a python list-of-things is not very python-like.)

westfw

Posts: 1781
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Arrays of pins

In C++, it is trivial. I do this sort of thing without even thinking hard about it, I just type it. But I'm trying to use MakeCode. A0..A7 are not recognized values I can put in an array.

The choice of MakeCode is dictated by external constraints which I cannot change.

flounder

Posts: 465
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA

Re: Arrays of pins

Sorry. I haven't got a clue.
Can you use the "JavaScript" variation of MakeCode, or are you restricted to the graphical Blocks?

westfw

Posts: 1781
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Arrays of pins

The recipient of this is someone who would work well with MakeCode, but may be challenged by JS, Python, and definitely by C++.

I learned that the pin numbers A0..A7 map to integers, and that A0 is not available. OK, we'll settle for seven flowerpots.

So I learn A0=3, A1=10, A2=11, A3=12, A4=48, A5=47, A6=8, A7=7.

But MakeCode protects me from doing an AnalogRead on a pin other than A1..A7, so I can't use pin numbers as equivalents.

Key here is that in the interests of "protecting" me from mistakes, it has made it impossible to do something that should be simple, which is to sequentially read seven analog pins. I can't create an array of pins, I can't use an integer pin number for analog input.

flounder

Posts: 465
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA

Re: Arrays of pins

I'm looking into this. Which board are you using, a CPX in makecode.adafruit.com or something else on maker.makecode.com?

johnpark

Posts: 881
Joined: Wed Mar 25, 2009 2:15 pm

Re: Arrays of pins

flounder

Posts: 465
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA

Re: Arrays of pins

I don't know of a way to do that in blocks either. You could make a feature request on the Microsoft MakeCode github. https://github.com/microsoft/pxt-adafruit

johnpark

Posts: 881
Joined: Wed Mar 25, 2009 2:15 pm

Re: Arrays of pins

Thanks. I have done so.

flounder

Posts: 465
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA