3 * Use the PiFace board to demonstrate an H bridge
4 * circuit via the 2 relays.
5 * Then add on an external transsitor to help with PWM.
7 * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
8 ***********************************************************************
9 * This file is part of wiringPi:
10 * https://projects.drogon.net/raspberry-pi/wiringpi/
12 * wiringPi is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * wiringPi is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
24 ***********************************************************************
35 int outputs [2] = { 0,0 } ;
37 #define PIFACE_BASE 200
38 #define PWM_OUT_PIN 204
42 void scanButton (int button)
44 if (digitalRead (PIFACE_BASE + button) == LOW)
46 outputs [button] ^= 1 ;
47 digitalWrite (PIFACE_BASE + button, outputs [button]) ;
48 printf ("Button %d pushed - output now: %s\n",
49 button, (outputs [button] == 0) ? "Off" : "On") ;
52 while (digitalRead (PIFACE_BASE + button) == LOW)
62 printf ("Raspberry Pi PiFace - Motor control\n") ;
63 printf ("==================================\n") ;
66 "This program is designed to be used with a motor connected to the relays\n"
67 "in an H-Bridge type configuration with optional speeed control via PWM.\n"
69 "Use the leftmost buttons to turn each relay on and off, and the rigthmost\n"
70 "buttons to increase ot decrease the PWM output on the control pin (pin\n"
74 piFaceSetup (PIFACE_BASE) ;
75 softPwmCreate (PWM_OUT_PIN, 100, 100) ;
77 // Enable internal pull-ups & start with all off
79 for (pin = 0 ; pin < 8 ; ++pin)
81 pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ;
82 digitalWrite (PIFACE_BASE + pin, 0) ;
87 for (button = 0 ; button < 2 ; ++button)
90 if (digitalRead (PWM_UP) == LOW)
96 softPwmWrite (PWM_OUT_PIN, pwmValue) ;
97 printf ("PWM -> %3d\n", pwmValue) ;
99 while (digitalRead (PWM_UP) == LOW)
103 if (digitalRead (PWM_DOWN) == LOW)
109 softPwmWrite (PWM_OUT_PIN, pwmValue) ;
110 printf ("PWM -> %3d\n", pwmValue) ;
112 while (digitalRead (PWM_DOWN) == LOW)