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

Test Your Internet Speed Using a Raspberry Pi + Ubidots
Easy
  • 218
  • 4

Protip

Use your Raspberry Pi to measure any internet connection speed with Ubidots.

Chicken Coop Livestream
Easy
  • 2,161
  • 7

Full instructions

We have two chickens, and their coop is located at the back of the garden. I decided to build a Pi-based streaming device to check on them.

Raspberry Pi Shutdown / Restart Button
Easy
  • 5,087
  • 20

Full instructions

Building an installation project? Shut down or reboot your project safely, without a keyboard or SSH!

3d printed case for a RaspPi 3, an Arduino and a 5" display
Easy
  • 275
  • 4

Full instructions

A simple and compact frame to mount a 5" monitor, a Raspberry Pi, an Arduino, and a battery, which is extensible for other sensors.

DIY Raspberry Pi Indoor Outdoor Webcam
Easy
  • 1,346
  • 6

Full instructions

Ever want to monitor your home but don't want to buy a $100 1080p webcam? Well I did, and you can too!

DYI hydroponics system powered by UniPi
Easy
  • 1,339
  • 10

Full instructions

DYI cloud connected hydroponics system powered by UniPi and Cloud4RPi

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog