Skip to content

Instantly share code, notes, and snippets.

@attilavago
Last active January 2, 2017 22:08
Show Gist options
  • Save attilavago/ae382297a6507b4f807b3054c2e68264 to your computer and use it in GitHub Desktop.
Save attilavago/ae382297a6507b4f807b3054c2e68264 to your computer and use it in GitHub Desktop.
This tests the ambient light sensor in light intensity percentage
/// EV3 Ambient Light Sensor Sample
/// Demonstrates how to poll EV3's light sensor for ambient light information.
import RobotKit
// Initializes a new EV3Robot instance through which all device communication happens.
let robot = try EV3Robot()
// Sets up a light sensor at input port 1.
guard let lightSensor = robot.lightSensor(atPort: .one) else {
fatalError("No light sensor at the given port!")
}
// Continues until the program is stopped.
while true {
// Reads the ambient light value as a percent.
if let percent = lightSensor.light(forMode: .ambient) {
// Draws everything in one pass for efficiency.
robot.window.batchDisplayUpdates {
robot.window.clear()
robot.window.drawText("Ambient", fontSize: .medium, at: Point(x: 30, y: 20), color: .black)
robot.window.drawText("light", fontSize: .medium, at: Point(x: 40, y: 40), color: .black)
robot.window.drawText("intensity:", fontSize: .medium, at: Point(x: 10, y: 60), color: .black)
robot.window.drawText(String(percent) + "%", fontSize: .medium, at: Point(x: 80, y: 90), color: .black)
}
}
// Pauses for half a second before going again.
wait(milliseconds: 500)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment