Blink
Last updated
Was this helpful?
Last updated
Was this helpful?
Let's first come to an easy beginner project - blink the onboard LED. This example shows the simplest thing you can do with just a SwiftIO board to see physical output: it blinks the on-board REG LED.
• SwiftIO board
For this project, you only need the SwiftIO board.
There is a built-in RGB LED on the board. You can control it using the methods in DigitalOut
class.
Note: the onboard LED will be turned on when you apply a low voltage.
Just plug the board to your computer through a USB cable after you finished code.
Digital signal has only two state, its value is either 1 or 0. For SwiftIO board, 1 represents 3.3V and 0 represents 0V. Thus, we can control the output voltage to turn on or off the LED.
import SwiftIO
refers to this named library. In this tutorial, all case programs must first reference this library so that we can use everything in it, like classes and functions.
let
is a keyword for Swift language to declare constants. We will often use it to assign names to each port for easy reference in the future.
Setting the while
loop to true
means that the loop check will always be true, unless the hardware is powered off or restarted, the loop will not stop after entering. The loop body is the part of the loop code, enclosed by a pair of braces {}
and needs to be indented for reading.
The .write()
method belongs to the DigitalOut
object and its incoming values ​​are true
and false
, which represent output high level (3.3V) and low level (0V or GND) respectively. And for these three LED, we need false
to turn on them.
The sleep(ms:)
function is a built-in function, which means the delay time, calculated in milliseconds. The external name of the parameter ms
must be added to pass in the parameter.
It's time for the code. Let's see how it works. You can find the example code at the bottom left corner of IDE: > GettingStarted > Blink.
Id
is an enumeration. All types of enumerated Id can be viewed . Its members include all IO ports. You may be confused why RED, GREEN, and BLUE are not marked on pinMap. Because the SwiftIO board is equipped with built-in RGB three-color LEDs by default and connected in series with resistors, the corresponding pins of the LEDs connected to the three ports are shown in the circuit diagram.
- Enumerations of all pins on the board.
- suspend the processor’s work in a given time period (in millisecond).
- DigitalOut class is used to set a High or Low voltage output to a digital output pin