🎨
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
  • Kits that meet the experimental conditions:
  • Circuit
  • Code
  • Instruction
  • What is a timer?
  • What is an interrupt?
  • What happens in this example?
  • See Also
  • References

Was this helpful?

  1. Built-In Examples
  2. SimpleIO

BlinkTimer

PreviousBlinkAnalogInNextBrightnessAnalogIn

Last updated 4 years ago

Was this helpful?

This example shows how to use the timer and interrupt mechanism on the SwiftIO Board to make the on-board Red LED blinking at a constant frequency.

What you need

  • SwiftIO board

Kits that meet the experimental conditions:

Circuit

Only the SwiftIO Board itself is required here. No extra shenanigans.

Code

/// Change the LED state every second by setting the interrupt.

/// Import the library to enable the relevant classes and functions.
import SwiftIO

/// Initialize the red onboard LED and a timer to set interrupt.
let red = DigitalOut(Id.GREEN)
let timer = Timer()

/// Raise the interrupt to turn on or off the LED every second.
timer.setInterrupt(ms: 1000) {
    red.toggle()
}

while true {

}

Instruction

What is a timer?

Timer is precisely a part of the hardware on the SwiftIO board. It works just like an alarm clock, and it can be programmed by manipulation of some registers. You can set some parameters for the timer, which makes it trigger every so often.

What is an interrupt?

An interrupt ensures that the processor responds quickly to some important events. When a interrupt signal is detected, the processor will stop its current job and perform some other codes, so that the board can react to the external signals quickly and accordingly.

What happens in this example?

In this case, the timer is set to be the internal interrupt source, which is done by using .setInterrupt(ms: ), a method in the Timer() class. The parameter indicates that the timer triggers an interrupt every 1000 ms, or, every 1 s.

The .toggle() (as the name implies) method of DigitalOut class means that the output level of the specific pin is inverted. In this case, the red LED light will be switch on or off when the interruption happens.

You can also use the sleep(ms: ) function to achieve the same effect. But this function will make the processor unresponsive during this period of time, making it not do anything. Therefore, the advantage of using interrupt is that the processor can still do other things between two toggles.

See Also

References

- The Timer class is used to set the occasion to raise the interrupt.

- To alternate between two voltage level.

Maker Kit for SwiftIO
Timer()
toggle()
wiki: Timer
wiki: Interrupt
wiki: toggle