MBot
public class MBot: MakeblockRobot
The Class used to control a Makeblock mBot
-
which LED Light to set when sending setRGBLED commands
See moreDeclaration
Swift
public enum RGBLEDPosition: UInt8
-
an enum of available ports of mBot controller board
See moreDeclaration
Swift
public enum MBotPorts: UInt8
-
an enum of music note pitch -> frequencies
See moreDeclaration
Swift
public enum MusicNotePitch: Int
-
an enum of music note duration -> milliseconds
See moreDeclaration
Swift
public enum MusicNoteDuration: Int
-
an enum of line-follower sensor status.
See moreDeclaration
Swift
public enum LineFollowerSensorStatus: Float
-
Create a mBot instance.
Declaration
Swift
public override init(connection conn: Connection)
Parameters
conn
a connection to send/receive messages from
Return Value
MBot instance
-
Set the speed of both motors of the mBot
Declaration
Swift
public func setMotors(leftMotor: Int, rightMotor: Int)
Parameters
leftMotor
speed of the left motor, -255~255
rightMotor
speed of the right motor, -255~255
-
Set the speed of a single motor of a mBot
Declaration
Swift
public func setMotor(port: MBotPorts, speed: Int)
Parameters
port
which port the motor is connect to. .M1 or .M2
speed
the speed of the motor -255~255
-
Tell the mBot to move forward
Declaration
Swift
public func moveForward(speed: Int)
Parameters
speed
the speed of moving. -255~255
-
Tell the mBot to move backward
Declaration
Swift
public func moveBackward(speed: Int)
Parameters
speed
the speed of moving. -255~255
-
Tell the mBot to turn left
Declaration
Swift
public func turnLeft(speed: Int)
Parameters
speed
the speed of moving. -255~255
-
Tell the mBot to turn right
Declaration
Swift
public func turnRight(speed: Int)
Parameters
speed
the speed of moving. -255~255
-
Tell the mBot to stop moving
Declaration
Swift
public func stopMoving()
-
Set the color of on-board LEDs of the mBot
Declaration
Swift
public func setRGBLED(position: RGBLEDPosition, red: Int, green: Int, blue: Int)
Parameters
position
which LED. Can be .left, .right or .all
red
red value (0~255)
green
green value (0~255)
blue
blue value (0~255)
-
Use the buzzer to play a musical note
Declaration
Swift
public func setBuzzer(pitch: MusicNotePitch, duration: MusicNoteDuration)
Parameters
pitch
pitch value eg. .C4 .E4
duration
duration. Could be .full, .half, .quarter, .eighth, or .sixteenth
-
Read the value of the ultrasoic sensor. and Call the callback when there’s value returning usage:
mbot.getUltrasonicSensorValue() { value in print("ultrasonic sensor says \(value)") }
Declaration
Swift
public func getUltrasonicSensorValue(port: MBotPorts = .Port3, callback: ((Float) -> Void))
Parameters
port
which port the sensor is connected to. By default .Port3
callback
a block of code executed after we have a value. Receive a Float as the argument.
-
Read the value of the lightness sensor. and Call the callback when there’s value returning usage:
mbot.getLightnessSensorValue() { value in print("lightness sensor says \(value)") }
Declaration
Swift
public func getLightnessSensorValue(port: MBotPorts = .LightnessSensor, callback: ((Float) -> Void))
Parameters
port
which port the sensor is connected to. By default .LightnessSensor (on board sensor)
callback
a block of code executed after we have a value. Receive a Float as the argument.
-
Read the value of the line-follower sensor. and Call the callback when there’s value returning usage:
mbot.getLinefollowerSensorValue() { value in if(value == .LeftBlackRightBlack) { // do things when the line-follower is left-black-right-black } }
Declaration
Swift
public func getLinefollowerSensorValue(port: MBotPorts = .Port2, callback:((LineFollowerSensorStatus) -> Void))
Parameters
port
which port the sensor is connected to. By default .Port2 (on board sensor)
callback
a block of code executed after we have a value. Receive a LineFollowerSensorStatus as the argument.