Output Demonstration 2
Introduction
This program uses the circuit for an LED or a DC motor described previously. It shows how you can use two timers to create an on-off sequence. You could use the technique to output Morse code to an LED. See Jack Fearn's program MorseCode for background information and sound output.
The code of the output unit and form follows a screenshot of the program in action.

GPIO Output Demonstration 2 in Action
Code of u_output_demo2.pas
unit u_output_demo2; {Demo application for GPIO on Raspberry Pi based on http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi, which was inspired by the Python input/output demo application by Gareth Halfacree written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5} //Build with Ctrl+F9 then run with SuperUser permissions e.g. sudo ./gpio_input {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, Unix; type TfrmOutputDemo2 = class(TForm) btnStart: TButton; LogMemo: TMemo; On_timer: TTimer; Off_timer: TTimer; procedure btnStartClick(Sender: TObject); procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); procedure FormCreate(Sender: TObject); procedure On_timerTimer(Sender: TObject); procedure Off_timerTimer(Sender: TObject); end; const NUM_OF_CYCLES = 5; ON_DURATION = 1000; OFF_DURATION = 2000; var frmOutputDemo2: TfrmOutputDemo2; gReturnCode: longint; //stores the result of the IO operation Count : integer; implementation {$R *.lfm} procedure TfrmOutputDemo2.FormCreate(Sender: TObject); begin Count := 0; On_timer.Interval:= ON_DURATION; Off_timer.Interval:= OFF_DURATION; //Prepare GPIO17 (P1-11) for access. gReturnCode := fpsystem('echo "17" > /sys/class/gpio/export'); if gReturnCode = 0 then LogMemo.Lines.Add('GPIO17 prepared for access') else LogMemo.Lines.Add('Error preparing GPIO17 for access'); LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1; //Set GPIO17 as output gReturnCode := fpsystem('echo "out" > /sys/class/gpio/gpio17/direction'); if gReturnCode = 0 then LogMemo.Lines.Add('GPIO17 set as output') else LogMemo.Lines.Add('Error setting GPIO17 as output'); LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1; end; procedure TfrmOutputDemo2.On_timerTimer(Sender: TObject); begin inc(Count); gReturnCode := fpsystem('echo "0" > /sys/class/gpio/gpio17/value'); if gReturnCode = 0 then begin On_timer.Enabled := False; LogMemo.Lines.Add('Off'); if Count < NUM_OF_CYCLES then Off_timer.Enabled := True else //Reset for next button press begin Count := 0; btnStart.Enabled := True; end; end else LogMemo.Lines.Add('Error switching off'); LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1; end; procedure TfrmOutputDemo2.Off_timerTimer(Sender: TObject); begin gReturnCode := fpsystem('echo "1" > /sys/class/gpio/gpio17/value'); if gReturnCode = 0 then begin On_timer.Enabled := True; LogMemo.Lines.Add('On'); Off_timer.Enabled := False; end else LogMemo.Lines.Add('Error switching on'); LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1; end; procedure TfrmOutputDemo2.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin fpsystem('echo "17" > /sys/class/gpio/unexport'); end; procedure TfrmOutputDemo2.btnStartClick(Sender: TObject); begin gReturnCode := fpsystem('echo "1" > /sys/class/gpio/gpio17/value'); if gReturnCode = 0 then begin On_timer.Enabled := True; LogMemo.Lines.Add('On'); btnStart.Enabled := False; end else LogMemo.Lines.Add('Error switching on LED'); LogMemo.SelStart := Length(LogMemo.Lines.Text) - 1; end; end.
Code of u_output_demo2.lfm
object frmOutputDemo2: TfrmOutputDemo2 Left = 671 Height = 148 Top = 422 Width = 314 Caption = 'GPIO Output Demo 2' ClientHeight = 148 ClientWidth = 314 OnClose = FormClose OnCreate = FormCreate LCLVersion = '0.9.30.4' object LogMemo: TMemo Left = 8 Height = 136 Top = 0 Width = 230 Lines.Strings = ( 'LogMemo' ) TabOrder = 0 end object btnStart: TButton Left = 256 Height = 25 Top = 8 Width = 40 Caption = 'Start' Color = clBtnFace OnClick = btnStartClick TabOrder = 1 end object On_timer: TTimer Enabled = False OnTimer = On_timerTimer left = 264 top = 40 end object Off_timer: TTimer Enabled = False Interval = 2000 OnTimer = Off_timerTimer left = 264 top = 88 end end