pxt icon indicating copy to clipboard operation
pxt copied to clipboard

Block to negate number

Open PSLLSP opened this issue 10 months ago • 0 comments
trafficstars

There is no block to negate number in Makecode. There is a block to negate logic value (not).

I miss a block to negate value of number. When number is constant, it is not problem, just negative number can be entered but when number is stored in variable, then entering negative value is "tricky". Block 0 - variable or -1 * variable can be used but this makes code just more complex. Could be a block -1 * number or negative number) added to Makecode? There is a block to have math operations on single parameter like round, floor, etc or square root, sin, etc, could be negate operation added to one of these blocks?

EXAMPLE

I want to control continuous servo with micro:bit. Servo can be controlled with value from -100 to 100, to define speed in percentage. I have custom function servo with one parameter, speed. It is speed in percentage and it is used to calculate duration of the control pulse. Note that when I want serve to run in opposite direction (with value -speed) I has to use 0 - speed block, not straight forward solution...

CODE

function servo (pspeed: number) {
    pins.servoSetPulse(AnalogPin.P0, 1500 + 5 * pspeed)
}
let speed = 100
basic.forever(function () {
    servo(speed)
    basic.pause(1000)
    servo(0 - speed)
    basic.pause(1000)
})

DEMO in Makecode

PSLLSP avatar Jan 01 '25 14:01 PSLLSP