Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Arrays of pins
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arrays of pins

by flounder on Tue Sep 15, 2020 3:50 pm

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 BLUE

forever 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: 406
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA

Re: Arrays of pins

by westfw on Tue Sep 15, 2020 9:46 pm

set current_value[i] to read(pins[i])

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: 1715
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Arrays of pins

by flounder on Tue Sep 15, 2020 9:56 pm

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: 406
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA

Re: Arrays of pins

by westfw on Tue Sep 15, 2020 11:10 pm

Ah. I hadn't noticed the "group" your question was in...
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: 1715
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Arrays of pins

by flounder on Wed Sep 16, 2020 12:34 am

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: 406
Joined: Wed Sep 18, 2013 9:10 pm
Location: Pittsburgh PA

Re: Arrays of pins

by johnpark on Wed Sep 16, 2020 1:16 pm

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: 766
Joined: Wed Mar 25, 2009 2:15 pm

Re: Arrays of pins

by flounder on Thu Sep 17, 2020 12:11 pm

CPX and makecode.adafruit.com

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

Re: Arrays of pins

by johnpark on Mon Sep 21, 2020 3:36 pm

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: 766
Joined: Wed Mar 25, 2009 2:15 pm

Please be positive and constructive with your questions and comments.