Menú
input.onButtonPressed(Button.A, function () {
TPBot.setWheels(50, 50)
basic.pause(3000)
TPBot.stopCar()
})basic.forever(function () {
let lightLevel = input.lightLevel()
if (lightLevel < 30) {
// In darker environments, move forward slowly
TPBot.setWheels(30, 30)
TPBot.headlightColor(0xffffff) // Turn on headlights
} else if (lightLevel < 70) {
// In medium brightness environments, move forward at medium speed
TPBot.setWheels(60, 60)
TPBot.headlightColor(0x000000) // Turn off headlights
} else {
// In brighter environments, move forward quickly
TPBot.setWheels(90, 90)
TPBot.headlightColor(0x000000) // Turn off headlights
}
})basic.forever(function () {
if (TPBot.trackLine(TPBot.TrackingState.L_R_Line)) {
// Both left and right sensors detect black line
TPBot.setWheels(40, 40)
} else if (TPBot.trackLine(TPBot.TrackingState.L_Line_R_Line)) {
// Left sensor detects black line, right sensor doesn't
TPBot.setWheels(0, 60)
} else if (TPBot.trackLine(TPBot.TrackingState.L_UnLine_R_Line)) {
// Right sensor detects black line, left sensor doesn't
TPBot.setWheels(60, 0)
} else {
// Neither sensor detects black line
TPBot.setWheels(40, 40)
}
})let lastState = TPBot.TrackingState.L_R_unline
let correctionCount = 0
basic.forever(function () {
// Determine current state by checking each possible tracking state
let currentState = TPBot.TrackingState.L_R_unline
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
currentState = TPBot.TrackingState.L_R_line
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
currentState = TPBot.TrackingState.L_line_R_unline
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
currentState = TPBot.TrackingState.L_unline_R_line
}
// Increase correction count if state hasn't changed
if (currentState == lastState) {
correctionCount++
} else {
correctionCount = 0
lastState = currentState
}
// Adjust driving strategy based on current state and duration
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
TPBot.setWheels(40, 40)
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
if (correctionCount > 10) {
// If left turn state continues for more than 10 cycles,
// increase right turn correction
TPBot.setWheels(-20, 80)
} else {
TPBot.setWheels(0, 60)
}
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
if (correctionCount > 10) {
// If right turn state continues for more than 10 cycles,
// increase left turn correction
TPBot.setWheels(80, -20)
} else {
TPBot.setWheels(60, 0)
}
} else {
// Lost the line, search based on last known state
if (lastState == TPBot.TrackingState.L_line_R_unline) {
// Last was left turn state, try right turn search
TPBot.setWheels(60, -20)
} else {
// Last was right turn state or initial state, try left turn search
TPBot.setWheels(-20, 60)
}
}
basic.pause(50)
})let distance = 0
basic.forever(function () {
distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
if (distance < 15 && distance > 0) {
// Obstacle ahead, stop and turn
TPBot.stopCar()
TPBot.setWheels(-40, -40)
basic.pause(500)
// Randomly choose to turn left or right
if (Math.randomBoolean()) {
TPBot.setWheels(-40, 40)
basic.pause(500)
} else {
TPBot.setWheels(40, -40)
basic.pause(500)
}
} else {
// No obstacle ahead, drive normally
TPBot.setWheels(50, 50)
}
basic.pause(100)
})let distance = 0
let lastDistance = 0
let distanceChange = 0
basic.forever(function () {
distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
// Calculate distance change
distanceChange = Math.abs(distance - lastDistance)
lastDistance = distance
// Display different icons based on distance
if (distance < 10) {
basic.showIcon(IconNames.Skull)
TPBot.headlightColor(0xff0000) // Red warning
} else if (distance < 30) {
basic.showIcon(IconNames.Sad)
TPBot.headlightColor(0xffff00) // Yellow warning
} else if (distance < 50) {
basic.showIcon(IconNames.Happy)
TPBot.headlightColor(0x00ff00) // Green normal
} else {
// For longer distances, display numerical distance
if (distance > 0) {
basic.showNumber(distance)
} else {
basic.showString("-")
}
TPBot.headlightColor(0x000000) // Turn off headlights
}
// If distance changes suddenly a lot, it might be a moving obstacle
if (distanceChange > 10) {
TPBot.setWheels(20, 20) // Slow down
} else {
TPBot.setWheels(50, 50) // Normal speed
}
basic.pause(200)
})let lightLevel = 0
let lastLightLevel = 0
let lightChange = 0
basic.forever(function () {
lightLevel = input.lightLevel()
// Calculate light change
lightChange = Math.abs(lightLevel - lastLightLevel)
lastLightLevel = lightLevel
// Adjust headlight brightness based on light intensity
if (lightLevel < 10) {
// Very dark, maximum brightness
TPBot.headlightColor(0xffffff)
} else if (lightLevel < 30) {
// Dark, medium brightness
TPBot.headlightColor(0x808080)
} else if (lightLevel < 60) {
// Slightly dark, low brightness
TPBot.headlightColor(0x404040)
} else {
// Bright, turn off headlights
TPBot.headlightColor(0x000000)
}
// If light changes suddenly a lot, it might be entering a tunnel or strong light exposure
if (lightChange > 20) {
// Flash headlights to alert
for (let i = 0; i < 3; i++) {
TPBot.headlightColor(0x0000ff)
basic.pause(100)
TPBot.headlightColor(0x000000)
basic.pause(100)
}
}
// Display approximate value of current light intensity (0-9)
let displayLevel = Math.floor(lightLevel / 10)
if (displayLevel > 9) displayLevel = 9
basic.showNumber(displayLevel)
basic.pause(500)
})let targetDistance = 20 // Target following distance (centimeters)
let distance = 0
let followState = false // Following state flag
// When button A is pressed, start following mode
input.onButtonPressed(Button.A, function () {
followState = true
basic.showIcon(IconNames.Yes)
TPBot.headlightColor(0x00ff00) // Green indicates following mode
})
// When button B is pressed, stop following mode
input.onButtonPressed(Button.B, function () {
followState = false
basic.showIcon(IconNames.No)
TPBot.headlightColor(0x000000) // Turn off headlights
TPBot.stopCar() // Stop the car
})
basic.forever(function () {
if (followState) {
distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
// Only follow if within effective distance
if (distance > 0) {
let distanceDiff = distance - targetDistance
// Adjust speed based on distance difference
if (Math.abs(distanceDiff) < 3) {
// Distance appropriate, stop
TPBot.stopCar()
} else if (distanceDiff > 0) {
// Object too far, move forward to follow
// Greater distance difference, faster speed
let speed = Math.min(50, Math.abs(distanceDiff) * 3)
TPBot.setWheels(speed, speed)
} else {
// Object too close, move backward to maintain distance
// Greater distance difference, faster backward speed
let speed = Math.min(50, Math.abs(distanceDiff) * 3)
TPBot.setWheels(-speed, -speed)
}
// Display distance on LED matrix
basic.showNumber(distance)
} else {
// No object detected, stop
TPBot.stopCar()
basic.showString("-")
}
}
basic.pause(100)
})
let straightLineCount = 0 // Straight line count
let curveCount = 0 // Curve count
let lastState = TPBot.TrackingState.L_R_unline
let speedBoost = false // Speed boost flag
// When button A is pressed, start line tracking
input.onButtonPressed(Button.A, function () {
straightLineCount = 0
curveCount = 0
lastState = TPBot.TrackingState.L_R_unline
speedBoost = false
basic.showIcon(IconNames.Rabbit)
})
// When button B is pressed, switch speed mode
input.onButtonPressed(Button.B, function () {
speedBoost = !speedBoost
if (speedBoost) {
basic.showIcon(IconNames.Triangle) // Use Triangle instead of Rocket
TPBot.headlightColor(0xff0000) // Red indicates high-speed mode
} else {
basic.showIcon(IconNames.Triangle) // Use Triangle instead of Rabbit
TPBot.headlightColor(0x00ff00) // Green indicates standard mode
}
})
basic.forever(function () {
// Determine current state by checking each possible tracking state
let currentState = TPBot.TrackingState.L_R_unline
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
currentState = TPBot.TrackingState.L_R_line
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
currentState = TPBot.TrackingState.L_line_R_unline
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
currentState = TPBot.TrackingState.L_unline_R_line
}
let baseSpeed = speedBoost ? 80 : 50
// Detect if on straight line
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
straightLineCount++
curveCount = 0
// If driving on straight line for a long time, can accelerate appropriately
let straightSpeed = baseSpeed
if (straightLineCount > 20) {
straightSpeed = Math.min(baseSpeed + 20, 100)
}
TPBot.setWheels(straightSpeed, straightSpeed)
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
// Turn left
curveCount++
straightLineCount = 0
// Adjust speed based on curve degree
let turnSpeed = baseSpeed
if (curveCount > 5) {
// Continuous left turns, possibly sharp curve, slow down
turnSpeed = Math.max(baseSpeed - 20, 30)
}
TPBot.setWheels(turnSpeed * 0.3, turnSpeed)
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
// Turn right
curveCount++
straightLineCount = 0
// Adjust speed based on curve degree
let turnSpeed = baseSpeed
if (curveCount > 5) {
// Continuous right turns, possibly sharp curve, slow down
turnSpeed = Math.max(baseSpeed - 20, 30)
}
TPBot.setWheels(turnSpeed, turnSpeed * 0.3)
} else {
// Lost the line, need to search
if (lastState == TPBot.TrackingState.L_line_R_unline) {
// Last was left turn state, try right turn search
TPBot.setWheels(baseSpeed, -baseSpeed * 0.5)
} else {
// Last was right turn state or initial state, try left turn search
TPBot.setWheels(-baseSpeed * 0.5, baseSpeed)
}
// Flash headlights to alert
TPBot.headlightColor(0xff0000)
basic.pause(50)
TPBot.headlightColor(0x000000)
}
lastState = currentState
basic.pause(20) // Increase response frequency
})4. Issue alerts when abnormalities are detected
let patrolMode = false
let lastLightLevel = input.lightLevel()
let obstacleCount = 0
let patrolTime = 0
// When buttons A+B are pressed, start patrol mode
input.onButtonPressed(Button.AB, function () {
patrolMode = !patrolMode
if (patrolMode) {
patrolTime = 0
obstacleCount = 0
lastLightLevel = input.lightLevel()
basic.showIcon(IconNames.Happy) // Use Happy instead of Shield
TPBot.headlightColor(0x0000ff) // Blue indicates patrol mode
} else {
basic.showIcon(IconNames.Asleep) // Use Asleep instead of Sleepy
TPBot.headlightColor(0x000000)
TPBot.stopCar()
}
})
function alert异常() {
// Abnormality alert
for (let i = 0; i < 5; i++) {
TPBot.headlightColor(0xff0000)
basic.showIcon(IconNames.Skull)
basic.pause(200)
TPBot.headlightColor(0x000000)
basic.showIcon(IconNames.Happy)
basic.pause(200)
}
}
basic.forever(function () {
if (patrolMode) {
patrolTime++
// Environmental light change detection
let currentLightLevel = input.lightLevel()
if (Math.abs(currentLightLevel - lastLightLevel) > 30) {
// Large light change, possibly a window being opened
alert异常()
lastLightLevel = currentLightLevel
}
// Obstacle detection and detour
let distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
if (distance > 0 && distance < 20) {
// Obstacle detected
obstacleCount++
if (obstacleCount > 10) {
// Obstacle detected continuously, possibly a fixed obstacle, need to detour
TPBot.stopCar()
TPBot.setWheels(-40, -40)
basic.pause(500)
// Randomly choose to turn left or right
if (Math.randomBoolean()) {
TPBot.setWheels(-40, 40)
basic.pause(800)
} else {
TPBot.setWheels(40, -40)
basic.pause(800)
}
obstacleCount = 0
} else {
// Temporarily stop, possibly a moving obstacle
TPBot.stopCar()
}
} else {
obstacleCount = 0
// Normal patrol
if (patrolTime % 100 == 0) {
// Every once in a while, randomly adjust direction to increase patrol coverage
if (Math.randomBoolean()) {
TPBot.setWheels(-30, 30)
basic.pause(300)
} else {
TPBot.setWheels(30, -30)
basic.pause(300)
}
} else {
// Normal forward patrol
TPBot.setWheels(40, 40)
}
}
// Display patrol status
basic.showIcon(IconNames.Happy) // Use Happy instead of Shield
}
basic.pause(100)
})// Define state enumeration
enum CarState {
Forward,
Backward,
Left,
Right,
Stop,
Avoiding,
Tracking
}
let currentState = CarState.Stop
let stateTimer = 0
let obstacleDistance = 0
let trackingState = TPBot.TrackingState.L_R_unline
// State processing function
function processState() {
stateTimer++
switch (currentState) {
case CarState.Forward:
TPBot.setWheels(50, 50)
basic.showIcon(IconNames.Triangle)
// Check if state transition is needed
obstacleDistance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
if (obstacleDistance > 0 && obstacleDistance < 15) {
currentState = CarState.Avoiding
stateTimer = 0
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline) || TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
currentState = CarState.Tracking
stateTimer = 0
} else if (stateTimer > 100) {
// Forward time too long, random turn
if (Math.randomBoolean()) {
currentState = CarState.Left
} else {
currentState = CarState.Right
}
stateTimer = 0
}
break
case CarState.Backward:
TPBot.setWheels(-40, -40)
basic.showIcon(IconNames.Square)
// Switch to stop state after backing up for a while
if (stateTimer > 30) {
currentState = CarState.Stop
stateTimer = 0
}
break
case CarState.Left:
TPBot.setWheels(-30, 30)
basic.showIcon(IconNames.Diamond)
// Switch to forward state after turning left for a while
if (stateTimer > 20) {
currentState = CarState.Forward
stateTimer = 0
}
break
case CarState.Right:
TPBot.setWheels(30, -30)
basic.showIcon(IconNames.SmallDiamond)
// Switch to forward state after turning right for a while
if (stateTimer > 20) {
currentState = CarState.Forward
stateTimer = 0
}
break
case CarState.Stop:
TPBot.stopCar()
basic.showIcon(IconNames.Square)
// Switch to forward state after stopping for a while
if (stateTimer > 50) {
currentState = CarState.Forward
stateTimer = 0
}
break
case CarState.Avoiding:
TPBot.stopCar()
basic.showIcon(IconNames.Skull)
TPBot.headlightColor(0xff0000)
if (stateTimer == 1) {
// First entering obstacle avoidance state, back up
TPBot.setWheels(-50, -50)
basic.pause(500)
} else if (stateTimer > 10 && stateTimer < 30) {
// After backing up, randomly choose to turn left or right
if (Math.randomBoolean()) {
TPBot.setWheels(-40, 40) // Left turn
} else {
TPBot.setWheels(40, -40) // Right turn
}
} else if (stateTimer > 30) {
// After turning, return to forward state
currentState = CarState.Forward
stateTimer = 0
TPBot.headlightColor(0x000000)
}
break
case CarState.Tracking:
// Determine tracking state by checking each possible state
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
trackingState = TPBot.TrackingState.L_R_line
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
trackingState = TPBot.TrackingState.L_line_R_unline
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
trackingState = TPBot.TrackingState.L_unline_R_line
} else {
trackingState = TPBot.TrackingState.L_R_unline
}
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
TPBot.setWheels(40, 40)
basic.showIcon(IconNames.Diamond)
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
TPBot.setWheels(0, 60)
basic.showIcon(IconNames.Diamond)
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
TPBot.setWheels(60, 0)
basic.showIcon(IconNames.SmallDiamond)
} else {
// Lost the line, return to forward state
currentState = CarState.Forward
stateTimer = 0
}
// Check for obstacles
obstacleDistance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
if (obstacleDistance > 0 && obstacleDistance < 15) {
currentState = CarState.Avoiding
stateTimer = 0
}
break
}
}
// Button control for state switching
input.onButtonPressed(Button.A, function () {
currentState = CarState.Forward
stateTimer = 0
})
input.onButtonPressed(Button.B, function () {
currentState = CarState.Stop
stateTimer = 0
})
// Main loop
basic.forever(function () {
processState()
basic.pause(50)
})// PID parameters
let kp = 0.5 // Proportional coefficient
let ki = 0.1 // Integral coefficient
let kd = 0.3 // Derivative coefficient
// Control variables
let error = 0 // Current error
let lastError = 0 // Previous error
let integral = 0 // Error integral
let derivative = 0 // Error derivative
let pidOutput = 0 // PID output
let baseSpeed = 50 // Base speed
// Calculate error
function calculateError() {
// Use line tracking sensor state to calculate error here
// -100 indicates completely left, 0 indicates middle, 100 indicates completely right
if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
return -50
} else if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
return 0
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
return 50
} else {
// Lost the line, predict based on last error
if (lastError > 0) {
return 100
} else if (lastError < 0) {
return -100
} else {
return 0
}
}
}
// Calculate PID output
function calculatePID() {
error = calculateError()
// Calculate integral term
integral = integral + error
// Limit integral range to prevent integral saturation
if (integral > 1000) integral = 1000
if (integral < -1000) integral = -1000
// Calculate derivative term
derivative = error - lastError
// Calculate PID output
pidOutput = kp * error + ki * integral + kd * derivative
// Save current error as previous error
lastError = error
return pidOutput
}
// Use PID to control line tracking
function pidTracking() {
let output = calculatePID()
// Calculate left and right wheel speeds
let leftSpeed = baseSpeed - output
let rightSpeed = baseSpeed + output
// Limit speed range
if (leftSpeed > 100) leftSpeed = 100
if (leftSpeed < -100) leftSpeed = -100
if (rightSpeed > 100) rightSpeed = 100
if (rightSpeed < -100) rightSpeed = -100
// Set wheel speeds
TPBot.setWheels(leftSpeed, rightSpeed)
// Display PID output (approximate value)
let displayValue = Math.floor(pidOutput / 10)
if (displayValue > 9) displayValue = 9
if (displayValue < -9) displayValue = -9
if (displayValue != 0) {
basic.showNumber(displayValue)
} else {
basic.showString("-" + Math.abs(displayValue))
}
}
// Button control
input.onButtonPressed(Button.A, function () {
// Start line tracking
baseSpeed = 50
integral = 0
lastError = 0
basic.showIcon(IconNames.Rabbit)
})
input.onButtonPressed(Button.B, function () {
// Stop line tracking
TPBot.stopCar()
basic.showIcon(IconNames.Square)
})
// Main loop
basic.forever(function () {
pidTracking()
basic.pause(20)
})
// Define sensor data structure
let sensorData = {
distance: 0, // Ultrasonic distance
lightLevel: 0, // Ambient light intensity
trackingState: TPBot.TrackingState.L_R_unline, // Line tracking state
lastUpdated: 0 // Last update time
}
// Define behavior decision enumeration
enum BehaviorDecision {
Forward,
Backward,
Left,
Right,
Stop,
FastForward,
SlowForward
}
// Update sensor data
function updateSensorData() {
sensorData.distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
sensorData.lightLevel = input.lightLevel()
// Use appropriate method to check tracking state
// Track line directly using trackLine method
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
sensorData.trackingState = TPBot.TrackingState.L_R_line
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
sensorData.trackingState = TPBot.TrackingState.L_unline_R_line
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
sensorData.trackingState = TPBot.TrackingState.L_line_R_unline
} else {
sensorData.trackingState = TPBot.TrackingState.L_R_unline
}
sensorData.lastUpdated = input.runningTime()
}
// Make decisions based on fused data
function makeDecision(): BehaviorDecision {
// Priority 1: Obstacle detection
if (sensorData.distance > 0 && sensorData.distance < 15) {
return BehaviorDecision.Stop
}
// Priority 2: Line tracking state
if (TPBot.trackLine(sensorData.trackingState)) {
// On the line tracking path
if (sensorData.trackingState == TPBot.TrackingState.L_R_line) {
// Straight driving, can accelerate appropriately
return BehaviorDecision.FastForward
} else {
// Curve driving, slow down
return BehaviorDecision.SlowForward
}
}
// Priority 3: Ambient light intensity
if (sensorData.lightLevel < 20) {
// Dark environment, slow driving
return BehaviorDecision.SlowForward
} else if (sensorData.lightLevel > 80) {
// Bright environment, can drive quickly
return BehaviorDecision.FastForward
}
// Default behavior
return BehaviorDecision.Forward
}
// Execute decision
function executeDecision(decision: BehaviorDecision) {
switch (decision) {
case BehaviorDecision.Forward:
TPBot.setWheels(50, 50)
basic.showIcon(IconNames.Triangle)
break
case BehaviorDecision.Backward:
TPBot.setWheels(-40, -40)
basic.showIcon(IconNames.Square)
break
case BehaviorDecision.Left:
TPBot.setWheels(-30, 30)
basic.showIcon(IconNames.Diamond)
break
case BehaviorDecision.Right:
TPBot.setWheels(30, -30)
basic.showIcon(IconNames.SmallDiamond)
break
case BehaviorDecision.Stop:
TPBot.stopCar()
basic.showIcon(IconNames.Square)
break
case BehaviorDecision.FastForward:
TPBot.setWheels(80, 80)
basic.showIcon(IconNames.Triangle)
break
case BehaviorDecision.SlowForward:
TPBot.setWheels(30, 30)
basic.showIcon(IconNames.Square) // Use Square instead of Snail
break
}
// Adjust headlights based on ambient light intensity
if (sensorData.lightLevel < 30) {
TPBot.headlightColor(0xffffff)
} else {
TPBot.headlightColor(0x000000)
}
}
// Main loop
basic.forever(function () {
updateSensorData()
let decision = makeDecision()
executeDecision(decision)
basic.pause(50)
})
// Button A: Forward
input.onButtonPressed(Button.A, function () {
TPBot.setWheels(50, 50) // Forward
basic.showIcon(IconNames.Triangle)
})
// Button B: Backward
input.onButtonPressed(Button.B, function () {
TPBot.setWheels(-50, -50) // Backward
basic.showIcon(IconNames.Square)
})
// Press both A and B buttons simultaneously: Stop
input.onButtonPressed(Button.AB, function () {
TPBot.stopCar() // Stop
basic.showIcon(IconNames.Square)
})// Regularly display sensor data
basic.forever(function () {
// Display ultrasonic distance
let distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
if (distance > 0) {
basic.showString("D:")
basic.showNumber(distance)
} else {
basic.showString("No")
}
basic.pause(1000)
// Display line tracking state
if (TPBot.trackLine(TPBot.TrackingState.L_R_line)) {
basic.showString("LINE")
} else if (TPBot.trackLine(TPBot.TrackingState.L_line_R_unline)) {
basic.showString("LEFT")
} else if (TPBot.trackLine(TPBot.TrackingState.L_unline_R_line)) {
basic.showString("RIGHT")
} else {
basic.showString("NONE")
}
basic.pause(1000)
})// Light show and action combination demonstration
input.onButtonPressed(Button.A, function () {
// Move forward and flash lights
TPBot.setWheels(30, 30)
for (let i = 0; i < 5; i++) {
TPBot.headlightColor(0xff0000) // Red
basic.pause(200)
TPBot.headlightColor(0x00ff00) // Green
basic.pause(200)
}
TPBot.stopCar()
TPBot.headlightColor(0x000000)
})
// Swinging light effect
input.onButtonPressed(Button.B, function () {
// Rotate in place and change light colors
TPBot.setWheels(-20, 20) // Turn left in place
let colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff]
for (let i = 0; i < colors.length; i++) {
TPBot.headlightColor(colors[i])
basic.pause(500)
}
TPBot.stopCar()
TPBot.headlightColor(0x000000)
})// Adjust behavior based on ambient Light distance
let lightThreshold = 50 // Light threshold
let distanceThreshold = 20 // Distance threshold
basic.forever(function () {
let lightLevel = input.lightLevel()
let distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
// Adjust headlights and speed based on light intensity
if (lightLevel < lightThreshold) {
// Dark light, turn on lights and drive slowly
TPBot.headlightColor(0xffffff)
if (distance > 0 && distance < distanceThreshold) {
// Dark and obstacle present, stop and warn
TPBot.stopCar()
basic.showIcon(IconNames.Skull)
// Flash warning lights
for (let i = 0; i < 3; i++) {
TPBot.headlightColor(0xff0000)
basic.pause(200)
TPBot.headlightColor(0x000000)
basic.pause(200)
}
} else {
// Dark but no obstacle, drive forward slowly
TPBot.setWheels(30, 30)
}
} else {
// Bright light, turn off lights and drive normally
TPBot.headlightColor(0x000000)
if (distance > 0 && distance < distanceThreshold) {
// Bright but obstacle present, turn to avoid
TPBot.stopCar()
// Randomly choose turning direction
if (Math.randomBoolean()) {
TPBot.setWheels(-30, 30) // Left turn
} else {
TPBot.setWheels(30, -30) // Right turn
}
basic.pause(500)
} else {
// Bright and no obstacle, drive forward normally
TPBot.setWheels(50, 50)
}
}
basic.pause(100)
})
// Traffic Light Game: Control the car according to light colors
let gameState = 0 // 0: Red light, 1: Green light, 2: Yellow light
let score = 0
// Initialization
basic.showString("GO!")
TPBot.stopCar()
// Button A: Press A to score when green light is on, lose points when red light is on
input.onButtonPressed(Button.A, function () {
if (gameState == 1) { // Green light
score++
basic.showString("+")
TPBot.headlightColor(0x00ff00)
} else if (gameState == 0) { // Red light
score--
basic.showString("-")
TPBot.headlightColor(0xff0000)
}
basic.pause(300)
showScore()
})
// Display score
function showScore() {
basic.clearScreen()
basic.showString("S:")
basic.showNumber(score)
}
// Game main loop
basic.forever(function () {
// Randomly set light color state
gameState = Math.randomRange(0, 2)
if (gameState == 0) { // Red light
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
TPBot.headlightColor(0xff0000)
} else if (gameState == 1) { // Green light
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
TPBot.headlightColor(0x00ff00)
} else { // Yellow light
basic.showLeds(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #
`)
TPBot.headlightColor(0xffff00)
}
// Random duration
basic.pause(Math.randomRange(500, 2000))
})
//Simple obstacle avoidance patrol mode
let patrolMode = false
// Button A: Turn patrol mode on/off
input.onButtonPressed(Button.A, function () {
patrolMode = !patrolMode
if (patrolMode) {
basic.showString("ON")
TPBot.headlightColor(0x0000ff)
} else {
basic.showString("OFF")
TPBot.stopCar()
TPBot.headlightColor(0x000000)
}
})
// Patrol main loop
basic.forever(function () {
if (patrolMode) {
let distance = TPBot.sonarReturn(TPBot.SonarUnit.Centimeters)
if (distance > 0 && distance < 20) {
// Obstacle detected, stop and turn
TPBot.stopCar()
TPBot.headlightColor(0xff0000)
// Back up a short distance
TPBot.setWheels(-30, -30)
basic.pause(500)
// Randomly choose to turn left or right
if (Math.randomBoolean()) {
TPBot.setWheels(-40, 40) // Left turn
} else {
TPBot.setWheels(40, -40) // Right turn
}
basic.pause(800)
TPBot.headlightColor(0x0000ff)
} else {
// No obstacle, drive normally
TPBot.setWheels(40, 40)
}
}
basic.pause(100)
})
3. Modify speed parameters and judgment thresholds in the code