🎨
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
  • Tips
  • Code
  • main.swift
  • SevenSegment.swift
  • Video
  • See Also
  • References

Was this helpful?

  1. Built-In Examples
  2. MakerKit

Mission 06 Seven Segment Display

What you need

Circuit

Tips

Code

main.swift

/*
  Mission6 7-Segment Display

  You should see a number "6" on the 7-segment display.

  The circuit:
  - Connect the 7-segment display into the Arduino Sheild.
  - Note: 7-segment display consists of 7 LEDs, called segments, arranged in the shape of an “8”. 
    Most 7-segment displays actually have 8 segments, with a dot on the right side of the digit 
    that serves as a decimal point.

  created 2019
  by Orange J

  Try to show characters like “H” “E” “L” “L” “O” on it. 
  This example code is in the public domain.
  Visit madmachine.io for more info.
*/

import SwiftIO

let number = 6
let sevenSeg = SevenSegment()

while true {
    sevenSeg.print(number)

}

SevenSegment.swift

/*
  Show number 0-9 on a Common Anode 7-segment LED display.
    A
   ---
F |   | B
  | G |
   ---
E |   | C
  |   |
   ---
    D
  This example code is in the public domain.
 */

import SwiftIO

class SevenSegment {
    // Initialize the seven digital pins which are connected to the segment pins.
    static let a = DigitalOut(Id.D8)
    static let b = DigitalOut(Id.D7)
    static let c = DigitalOut(Id.D6)
    static let d = DigitalOut(Id.D5)
    static let e = DigitalOut(Id.D4)
    static let f = DigitalOut(Id.D2)
    static let g = DigitalOut(Id.D3)

    let leds = [a, b, c, d, e, f, g]
    // Use a binary data to store the status of each segment for the number from 0 to zero.
    let ledState: [UInt8] = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F]

    public func print(_ number: Int) {
        let num = number % 10
        let value = ledState[num] 

        // Get the value of each bit to determine whether the relevant segment is on or off.
        for i in 0..<7{
            let state = (value >> i) & 0x01
            if state == 0 {
                leds[i].write(true)
            } else {
                leds[i].write(false)
            }   
        }

    }
}

Video

See Also

References

Last revision 2020/09/10 by Johnson

PreviousMission 05 BuzzerNextMission 07 DC Motor

Last updated 4 years ago

Was this helpful?