LEDsBrightnessControl
Last updated
Was this helpful?
Last updated
Was this helpful?
This example demonstrates the usage of analog output (Pulse Width Modulation, PWM) to fade three LEDs. PWM is a technique to obtain an analog-like behavior from a digital output by switching it off and on very fast, and with different ratio between on and off time.
SwiftIO board
Jumper wires
3x color LEDs and 3x 330 ohm resistors (or 3 color LED Modules)
SwiftIO shield (optional)
let leds = [red, green, blue]
Swift provides three primary collection types, known as arrays, arrays are ordered collections of values. You access and modify an array through its methods and properties, or by using subscript syntax, such as leds.red
, leds.green
, leds.blue
.
var value: Float = 0.0
explicitly declares that the type of value is a floating-point number type, not an integer type. Explicitly declaring the type is very important for scenarios where the type is easy to be confused. The subsequent use shows that the variable must be a floating-point real number.
You can iterate over the entire set of values in an array with the for-in loop: for led in leds
This is the reason why we want to create the array leds
. With the for-in loop syntax structure, the elements are It becomes very convenient and concise when traversing and iterating.
- An integer type can be initialized with a Double or Float value.
- You use the for-in loop to iterate over a sequence, such as items in an array, ranges of numbers, or characters in a string.