Things used in this project

Hardware components:
Pi 3 02
Raspberry Pi Raspberry Pi 3 Model B
×1
09590 01
LED (generic)
×1
12002 04
Breadboard (generic)
×1
08377 02 l
Resistor 330 ohm
×1
11026 02
Jumper wires (generic)
×1
Software apps and online services:
Raspberry Pi Raspbian Jessie
Swift-Lite

Schematics

GPIO Pin Layout
GPIO pin layout diagram for the Raspberry Pi 40 pin models
6sqiftkxhzptfignplsc

Code

piCodeLEDTimer.swiftSwift
Swift 3.0 project file for Flash LED tutorial
// Created with PiCode for Swift 3.0
// A Swift-Lite project file
// type:project
// name:piCodeLEDTimer
// include:piCodeGPIO.swift

import Foundation

// auto detect board type (detects all boards with 40 GPIO pins)
let gpios = autoDetectBoardType()

// for older boards with 26 GPIO pins please set board type manually
//let gpios = PiCodeGPIO.RPIRev1
//let gpios = PiCodeGPIO.RPIRev2

// setup the GPIO pins
var gp = gpios[.P17]!   // Pin to conect the LED to
gp.direction = .OUT     // Pin connection direction - OUT - when turned on the pin will output 5v
gp.value = 0            // Set intial value 0 = off 1 = on


var intervalTimer: Timer!   // this timer controls the time intervals the LED turns on. As this is a repeating timer it needs to be added to a runloop.
var flashTimer: Timer!      // this timer controls the length of time the LED remains on.

var flashNumber = 5 // the number of flashes

var flashIntervalTime: Double = 2.0 // the time between the start of each flash

var flashLengthTime: Double = 0.75 // the length of time of each flash 

var runLoopTime: Double = 11 // the lenghth of time the runloop will run - set this to be longer than the time for all the flashes to complete
                             // the runloop can be set to continous if the is action you need.


func runIntervalTimer() {
    intervalTimer = Timer.scheduledTimer(withTimeInterval: flashIntervalTime, repeats: true, block: {(_) in flashOnLED()})
}

func runflashTimer() {
    flashTimer = Timer.scheduledTimer(withTimeInterval: flashLengthTime, repeats: false, block: {(_) in flashOffLED()})
}

func flashOnLED() {
    print("flash on")
    gp.value = 1
    runflashTimer()
}

func flashOffLED() {
    gp.value = 0
    print("flash off")
    flashNumber -= 1
    if flashNumber == 0 {
        intervalTimer.invalidate()
        print("finished")
    }
}

runIntervalTimer()

let flashLEDLoop = RunLoop.current
flashLEDLoop.add(intervalTimer, forMode: .defaultRunLoopMode)

flashLEDLoop.run(until: Date(timeIntervalSinceNow: runLoopTime))

Credits

Oeq87mf14w7ffi52nxro
FutureJones

Development Engineer at SwiftPi - Bringing Swift to the Raspberry Pi. Designer and creator of Swift-Lite.

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Setting Up Your Pi Cap on the Raspberry Pi 1, 2 or 3
Easy
  • 69
  • 2

Protip

Follow this tutorial to set up your Pi Cap with a Raspberry Pi 1 A+/B+, Raspberry Pi 2 or Raspberry Pi 3.

Setting Up Your Pi Cap on the Raspberry Pi Zero
Easy
  • 76
  • 2

Protip

Follow this tutorial to set up your Pi Cap with a Raspberry Pi Zero.

Secure Simple Remote Access for Camera Viewing
Easy
  • 2,001
  • 8

Full instructions

Turn your RPi into a full featured camera with remote access, end2end encryption, and easy user enrollment.

PiCorder
Easy
  • 3,626
  • 14

Full instructions

A simple Pi-based camcorder using the very nice HyperPixel touchscreen from Pimoroni.

RPi Serial Console Automation using Arduino
Easy
  • 234
  • 3

Full instructions

A follow-up article to "One Button Restart of a Stalled Raspberry Pi": 3 digital pins provide 8 separate functions that can be called.

Run uTorrent on Raspberry Pi
Easy
  • 468
  • 2

Protip

Step-by-step instruction of running uTorrent on Raspberry Pi 2 using ExaGear Desktop.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login