Skip to content

Instantly share code, notes, and snippets.

@denistsoi
Last active June 22, 2025 08:32
Show Gist options
  • Save denistsoi/860c09029e3505c2a3fb878f705832ea to your computer and use it in GitHub Desktop.
Save denistsoi/860c09029e3505c2a3fb878f705832ea to your computer and use it in GitHub Desktop.
Makecode remote/controller
radio.onReceivedNumber(function (num) {
if (role === Role.Motor) {
motor(num)
}
console.log(num)
})
radio.on()
radio.setGroup(1)
basic.showIcon(IconNames.Yes)
const ROLL_BOUNDARY = 40
const PITCH_BOUNDARY = 25
enum Role {
Controller,
Motor,
}
let role: Role
basic.forever(() => {
let pitch = input.rotation(Rotation.Pitch)
let roll = input.rotation(Rotation.Roll)
if (role === Role.Controller) {
controller(roll, pitch)
}
console.logValue("Roll", roll)
console.logValue("Pitch", pitch)
pause(10)
})
input.onButtonPressed(Button.A, () => {
role = Role.Controller
basic.showIcon(IconNames.Duck)
})
input.onButtonPressed(Button.B, () => {
role = Role.Motor
basic.showIcon(IconNames.Rollerskate)
})
function controller(roll: number, pitch: number) {
if (roll < -ROLL_BOUNDARY) {
basic.showArrow(ArrowNames.West)
radio.sendNumber(1)
} else if (roll > ROLL_BOUNDARY) {
basic.showArrow(ArrowNames.East)
radio.sendNumber(2)
} else if (pitch < -PITCH_BOUNDARY) {
basic.showArrow(ArrowNames.North)
radio.sendNumber(3)
} else if (pitch > PITCH_BOUNDARY) {
basic.showArrow(ArrowNames.South)
radio.sendNumber(4)
} else {
basic.showIcon(IconNames.Square)
radio.sendNumber(0)
}
}
function motor(num: number) {
if (num === 1) {
basic.showArrow(ArrowNames.West)
servos.P0.run(-60)
servos.P2.run(-60)
} else if (num === 2) {
basic.showArrow(ArrowNames.East)
servos.P0.run(60)
servos.P2.run(60)
} else if (num === 3) {
basic.showArrow(ArrowNames.North)
servos.P0.run(-60)
servos.P2.run(60)
} else if (num === 4) {
basic.showArrow(ArrowNames.South)
servos.P0.run(60)
servos.P2.run(-60)
} else {
basic.showIcon(IconNames.Square)
servos.P0.stop()
servos.P2.stop()
}
}
// on start
radio.on()
radio.setGroup(1)
basic.showIcon(IconNames.Yes)
const BOUNDARY = 40;
// on forever
basic.forever(() => {
let pitch = input.rotation(Rotation.Pitch)
let roll = input.rotation(Rotation.Roll)
console.logValue('Pitch', pitch)
console.logValue('Roll', roll)
controller(roll, pitch)
pause(50);
})
function controller(roll: number, pitch: number) {
if (roll < -BOUNDARY) {
basic.showArrow(ArrowNames.West)
radio.sendNumber(1)
} else if (roll > BOUNDARY) {
basic.showArrow(ArrowNames.East)
radio.sendNumber(2)
} else if (pitch < -BOUNDARY) {
basic.showArrow(ArrowNames.North)
radio.sendNumber(3)
} else if (pitch > BOUNDARY) {
basic.showArrow(ArrowNames.South)
radio.sendNumber(4)
} else {
basic.showIcon(IconNames.Square)
radio.sendNumber(0)
}
}
// on start
radio.on()
radio.setGroup(1)
basic.showIcon(IconNames.Yes)
const ROLL_BOUNDARY = 40
const PITCH_BOUNDARY = 25
// on forever
basic.forever(() => {
let pitch = input.rotation(Rotation.Pitch)
let roll = input.rotation(Rotation.Roll)
console.logValue('Pitch', pitch)
console.logValue('Roll', roll)
remote(roll, pitch)
pause(100)
})
function controller(roll: number, pitch: number) {
if (roll < -ROLL_BOUNDARY) {
basic.showArrow(ArrowNames.West)
radio.sendNumber(1)
} else if (roll > ROLL_BOUNDARY) {
basic.showArrow(ArrowNames.East)
radio.sendNumber(2)
} else if (pitch < -PITCH_BOUNDARY) {
basic.showArrow(ArrowNames.North)
radio.sendNumber(3)
} else if (pitch > PITCH_BOUNDARY) {
basic.showArrow(ArrowNames.South)
radio.sendNumber(4)
} else {
basic.showIcon(IconNames.Square)
radio.sendNumber(0)
}
}
function remote(roll: number, pitch: number) {
if (roll < -ROLL_BOUNDARY) {
basic.showArrow(ArrowNames.West)
servos.P0.run(-60)
servos.P2.run(-60)
} else if (roll > ROLL_BOUNDARY) {
basic.showArrow(ArrowNames.East)
servos.P0.run(60)
servos.P2.run(60)
} else if (pitch < -PITCH_BOUNDARY) {
basic.showArrow(ArrowNames.North)
servos.P0.run(-60)
servos.P2.run(60)
} else if (pitch > PITCH_BOUNDARY) {
basic.showArrow(ArrowNames.South)
servos.P0.run(60)
servos.P2.run(-60)
} else {
basic.showIcon(IconNames.Square)
servos.P0.stop()
servos.P2.stop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment