🎨
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
  • Video
  • See Also
  • References

Was this helpful?

  1. Built-In Examples
  2. MakerKit

Mission 02 RGB LED

What you need

Circuit

Tips

Code

/*
  Mission2 RGB LED Control

  The Red, Green and Blue LED blink one by one. The delay between each flash is 1s.

  The circuit:
  - Connect the anode (long leg) of each LED to digital pins D16, D17 and D18 (via a 330 ohm resistor).
  - Connect the cathodes (short leg) to the SwiftIO’s ground.

  created 2019
  by Orange J

  Add more LEDs to your circuit. Don’t forget the current-limiting resistors. 
  You will need to declare the new pins in your code and set them all to OUTPUT. 
  Try to simulate traffic light logic. This example code is in the public domain. 
  Visit madmachine.io for more info.
*/

import SwiftIO


// Initialize three LEDs.
let red = DigitalOut(Id.D16)
let green = DigitalOut(Id.D17)
let blue = DigitalOut(Id.D18)

while true {
    // Turn on red LED for 1 second, then off.
    red.write(true)
    sleep(ms: 1000)
    red.write(false)

    // Turn on green LED for 1 second, then off.
    green.write(true)
    sleep(ms: 1000)
    green.write(false)

    // Turn on blue LED for 1 second, then off.
    blue.write(true)
    sleep(ms: 1000)
    blue.write(false)
}

Video

See Also

References

Last revision 2020/09/10 by Johnson

PreviousMission 01 BlinkNextMission 03 Push Button

Last updated 4 years ago

Was this helpful?