I am working on a school project requiring the use of the circuit playground express. I am trying to connect a bunch of external photocells to the cpx so that it will change the color of its lights everytime any individual photocell detects darkness. I was trying to use an analog multiplexer for this, but I can't get it to work. I've attatched the code... am using adafruit's makecode environment.
let dark = false
let value = 0
let numChannels = 1
let darkThreshold = 500
let muxPin = pins.A1.analogRead()
let S0 = pins.A2.analogRead()
let S1 = pins.A3.analogRead()
let S2 = pins.A4.analogRead()
let S3 = pins.A5.analogRead()
function setChannel (ch: number) {
if (ch % 2 == 1) {
pins.A2.digitalWrite(true)
} else {
pins.A2.digitalWrite(false)
}
if (ch / 2 % 2 == 1) {
pins.A3.digitalWrite(true)
} else {
pins.A3.digitalWrite(false)
}
if (ch / 4 % 2 == 1) {
pins.A4.digitalWrite(true)
} else {
pins.A4.digitalWrite(false)
}
if (ch / 8 % 2 == 1) {
pins.A5.digitalWrite(true)
} else {
pins.A5.digitalWrite(false)
}
}
forever(function () {
for (let index = 0; index <= 16; index++) {
let channel = 0
index += 1
setChannel(channel)
value = pins.A1.analogRead()
if (value < 200) {
dark = true
}
console.log(value)
pause(1000)
if (dark) {
light.setAll(0xffff00)
} else {
light.setAll(0xff0000)
}
}
})
My wiring is similar to this, but with the circuit playground:
I have sig plugged into pin A1 and S0-S3 plugged into A0, A2, A3 and A4. My photocell wiring looks like this:
So far I have only tried to connect 1 photocell to it and then will add more from there. It is currently always reading dark.

