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

Setting Graph minimum to something other than 0
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Setting Graph minimum to something other than 0

by Teedee1 on Fri Feb 14, 2020 8:11 am

Hello All,

I'm looking at running a graph function for temperature on makecode that will display on the circuitplayground express. I want the graph to be blank below 20 degrees then for the lights to only graph between 20 and 30C. I can set the max at 30 but as im using temperature as the variable i can't seem to set it to anything other than 0.

Im looking to graph from 20 to 30 so there's more movement in the lights when the thermometer is interacted with. As it stands at the minute the first 2/3rds of the Neopixels are already illuminated at 20C so theres only a small amount of movement up to 30C

Anyone got any ideas?

Cheers!

Teedee1
 
Posts: 8
Joined: Thu Jan 30, 2020 5:45 am

Re: Setting Graph minimum to something other than 0

by V2man on Fri Feb 14, 2020 1:09 pm

Just that I think you found a bug with the graph function. In the board simulator it is not working as expected. I did not try on a real CPX board.

CPX-Temperature_20t30C_usingGraph.PNG
CPX-Temperature_20t30C_usingGraph.PNG (46.03 KiB) Viewed 171 times
Attachments
circuitplayground-Temperature_20t30C.uf2
(281 KiB) Downloaded 2 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Sat Feb 15, 2020 2:44 am

This does not use graph! I came up with this. I know no fancy colors!
CPX-Temperature_20t30C.PNG
CPX-Temperature_20t30C.PNG (86.19 KiB) Viewed 156 times
Attachments
CPX-Temperature_20t30C.uf2
(276.5 KiB) Downloaded 4 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Sat Feb 15, 2020 11:18 am

Here's a version I like with color.
CPX-Temperature_20t30Cx.PNG
CPX-Temperature_20t30Cx.PNG (99.52 KiB) Viewed 148 times
Attachments
CPX-Temperature_20t30Cx.uf2
(281 KiB) Downloaded 2 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Sun Feb 16, 2020 9:28 pm

@ Teedee1 This meets your requirements except for using graph block.

CPX-Temperature.JPG
CPX-Temperature.JPG (86.51 KiB) Viewed 130 times
Attachments
CPX_Temperature.uf2
(280 KiB) Downloaded 3 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by Teedee1 on Mon Feb 17, 2020 4:30 am

Thank you so much for the reply, this is great!

I've made an amendment to the code to add a 16 pixel strip instead of using the lights on the board.

For the hue section in the main body of the code I assumed i needed to changed the second value down to 10 from 16 to account for the increased number of pixels.

I've not got the opportunity to run the code until thursday but does this appear correct to you?

Thank you again for the help so far!
Attachments
circuitplayground-screenshot.png
circuitplayground-screenshot.png (392.68 KiB) Viewed 127 times

Teedee1
 
Posts: 8
Joined: Thu Jan 30, 2020 5:45 am

Re: Setting Graph minimum to something other than 0

by V2man on Mon Feb 17, 2020 11:27 pm

This is my first time using an external strip on Makecode.
Some minor comments:
I am not sure [set brightness] controls the external strip so use [set strip brightness].
I found out if you define [set strip brightness] before [set strip to create] you will get this error:
Program Error:sim error: Failed cast on null
So you must place the [set strip brightness] after [set strip to create]!
You might want to increase the value in the pause statement to 1000 ms or 1 sec or much higher. '
I had it at 100 ms so I could see the simulation results react quickly to my changing the simulation temperature in Makecode.

Hue has a range from 0 to 255 with 0 being the color red. Most people think of cold/blue and hot/red.
My starting point of 160 was pretty arbitrary. The key point is the calculation (160 - (10 x index)) must not be less than zero. The maximum index value is 16 - 1 = 15 That yields a value of 10. I wanted to save the red hue = 0 for the condition T > Tmax. If you want red to be the very last value you could substitute 150 for 160. Play with it.

Here's what I came up with for an external strip:

CPX-Temperature-xstrip.JPG
CPX-Temperature-xstrip.JPG (75.37 KiB) Viewed 114 times
Attachments
CPX_Temperature_xstrip.uf2
(280 KiB) Downloaded 2 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Tue Feb 18, 2020 12:16 am

I verified that [set brightness] will not control the brightness of the external strip. You have to use [set strip brightness].

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Tue Feb 18, 2020 2:22 am

The neopixels on a CPX board form a circle. Usually one associates an increase in value with a clockwise direction or for a linear display to the right. With the code as written an increase in temperature will display in the counter clockwise direction on a CPX board. The external circular strip I used NeoPixel Ring - 12 x 5050 RGB LED https://www.adafruit.com/product/1643 shows an increase in temperature in a clockwise direction. With a linear display you can just physically rotate the strip 180 degrees to get the desired direction.

You can compare the readings you are getting against those of a thermometer and factor in a small correction factor into the definition of T, temperature variable.

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by Teedee1 on Tue Feb 18, 2020 3:41 am

Thanks you again V2man. I dont think i'd have been able to finish this project without your help!

Teedee1
 
Posts: 8
Joined: Thu Jan 30, 2020 5:45 am

Re: Setting Graph minimum to something other than 0

by V2man on Tue Feb 18, 2020 12:11 pm

@ Teedee1 Your welcome! : )

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Tue Feb 18, 2020 11:15 pm

In the previous version the neopixels light up in a counter clockwise direction as the temperature increases on a CPX board.
Here's a version displaying the neopixels lighting up in a clockwise direction as the temperature increases on a CPX board.

CPX-Temperature-CW.JPG
CPX-Temperature-CW.JPG (92.14 KiB) Viewed 89 times
Attachments
CPX-Temperature-CW.uf2
(280.5 KiB) Downloaded 3 times

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Wed Feb 19, 2020 2:15 am

The CPX-Temperature-CW.uf2 in the last post won't load into Makecode. Its original name in Makecode was CPX_Temperature_cw.uf2
To fix it I copied CPLAYBOOT/CURRENT.UF2 to my PC. Then I opened a new project in Makecode and dragged CURRENT.UF2 into it. It came in fine but when I try and change the name in Makecode and save it to my PC. I cannot import it or drag new file into Makecode.
I was trying to follow Makecodes convention of using dashes in a name but I usually define a name with underscores.

Can I use the CURRENT.UF2 from CPLAYBOOT to recreate my design?
Is there a problem in Makecode between recognizing dashes and unscores?
Can I rename Makecode uf2 files outside of Makecode?
It seems like it is retaining the name originally assigned in Makecode.

V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Wed Feb 19, 2020 11:44 am

To recover I dragged CPLAYBOOT/CURRENT.UF2 and dragged it into Makecode.
Then I switched from Blocks to Java script.
Copied all the text in Java to a CPX-Temperature-CW.txt file.
Then I closed the project and deleted it in Makecode.
Then I opened a new Makecode Project and switched to Java script.
I then copied the text from Java script to a CPX-Temperature-CW.txt file.
Gave the project a new name and saved the project to CPX-Temp-CW.uf2 on my PC. Instead of the default circuitplayground-CPX-Temp-CW.uf2.
Then I the closed the project and deleted it.
Finally I dragged CPX-Temp-CW.uf2 to Makecode. The project opened without error.
I then copied CPX-Temp-CW.uf2 to CPLAYBOOT drive.
All is back to normal.
I think from now on I am going to save the Makecode java script in a txt file to use as a backup.
It is a more efficient way of storing files: 1KB txt file versus 280 KB uf2. Smaller file size.
One downside is you have to keep re-arranging the blocks to make it more presentable when you return to Block editor.

Code: Select all | TOGGLE FULL SIZE
let np = 0
let n = 0
let Tc = 0
let T = 0
light.setBrightness(20)
let num_np = 10
let Tmin = 20
let Tmax = 30
/**
 * CPX-Temperature-CW Example of using neopixels to measure temperature rise in clockwise direction on CPX board.
 */
forever(function () {
    T = input.temperature(TemperatureUnit.Celsius)
    Tc = Math.constrain(T, Tmin, Tmax)
    n = Math.map(Tc, Tmin, Tmax, 0, num_np - 1)
    if (T < Tmin) {
        light.setAll(0x000000)
    } else {
        for (let index = 0; index <= num_np - 1; index++) {
            np = num_np - 1 - index
            if (index <= n) {
                light.setPixelColor(np, light.hsv(18 * np, 255, 255))
            } else {
                light.setPixelColor(np, 0x000000)
            }
        }
    }
    pause(1000)
})


V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: Setting Graph minimum to something other than 0

by V2man on Fri Feb 21, 2020 10:31 pm

I submitted the following issue at https://github.com/microsoft/pxt-maker/issues.
Plot of temperature using onboard neopixels does not work on CPB board
and this code:

Code: Select all | TOGGLE FULL SIZE
/**
 * CP-Temperature-CW
 *
 * Example of using neopixels to measure temperature rise in
 *
 * clockwise direction on CPX/CPB board.
 */
let np = 0
let n = 0
let Tc = 0
let T = 0
light.onboardStrip().setBrightness(20)
let Tmin = 60
let Tmax = 80
forever(function () {
    T = input.temperature(TemperatureUnit.Fahrenheit)
    Tc = Math.constrain(T, Tmin, Tmax)
    n = Math.map(Tc, Tmin, Tmax, 0, light.onboardStrip().length() - 1)
    if (T < Tmin) {
        light.onboardStrip().setAll(0x000000)
    } else {
        for (let index = 0; index <= light.onboardStrip().length() - 1; index++) {
            np = light.onboardStrip().length() - 1 - index
            if (index <= n) {
                light.onboardStrip().setPixelColor(np, light.hsv(18 * np, 255, 255))
            } else {
                light.onboardStrip().setPixelColor(np, 0x000000)
            }
        }
    }
    pause(1000)
})


V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Please be positive and constructive with your questions and comments.