🎨
MadMachine-Examples
  • Introduction
  • About our project
  • Getting Started
  • Tutorial
    • What is SwiftIO
    • How to use IDE
    • Libraries
    • MM SDK
  • Built-In Examples
    • GettingStarted
      • Blink
      • ReadDigitalInput
      • ReadAnalogInput
      • PWMBrightnessControl
    • SimpleIO
      • ButtoncontrolLED
      • BlinkAnalogIn
      • BlinkTimer
      • BrightnessAnalogIn
      • Debounce
      • LEDsBrightnessControl
      • PWMSoundOutput
      • PWMMelody
    • AdvancedIO
      • ILI9341
      • LCD1602
      • MidiPlayer
      • PCA9685
      • SHT3x
      • SSD1315
    • Challenge
      • LCDFamily
      • RobotArm
      • Tetris
    • MakerKit
      • Mission 01 Blink
      • Mission 02 RGB LED
      • Mission 03 Push Button
      • Mission 04 Potentiometer RGB
      • Mission 05 Buzzer
      • Mission 06 Seven Segment Display
      • Mission 07 DC Motor
      • Mission 08 Servo Motor
      • Mission 09 LCD
      • Mission 10 Humiture Sensor
  • Library Reference
  • FAQ
Powered by GitBook
On this page
  • What you need
  • Circuit
  • Schematic
  • Code
  • Instruction
  • What is digital signal?
  • About code
  • See Also
  • Reference

Was this helpful?

  1. Built-In Examples
  2. GettingStarted

Blink

PreviousGettingStartedNextReadDigitalInput

Last updated 4 years ago

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.

What you need

• SwiftIO board

Circuit

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.

Schematic

Code

// Import the library to enable everything in it, like relevant classes and methods. 
// This is first step for your coding process.
import SwiftIO

// Declare a constant. You may choose any descriptive name you like. 
// Initialize the onboard green LED. 
// The Id of onboard LED should be capitalized.
let green = DigitalOut(Id.GREEN) 

// In the dead loop, the code will run over and over again.
while true {
    // Output 3.3V to turn off the green LED.
    green.write(true)
    // Pause for a second. Or, you won't notice LED state change. 
    // During this period, the board will do nothing but just wait.
    sleep(ms: 1000)

    // Output 0V to turn on the green LED.
    green.write(false)
    sleep(ms: 1000)
}

Instruction

What is digital signal?

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.

About code

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.

See Also

Reference

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

here
Id
sleep(ms:)
DigitalOut.write()
wiki: Digital signal