chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / examples / PiFace / motor.c
1 /*
2  * motor.c:
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.
6  *
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/
11  *
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.
16  *
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.
21  *
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  ***********************************************************************
25  */
26
27 #include <wiringPi.h>
28 #include <piFace.h>
29 #include <softPwm.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34
35 int outputs [2] = { 0,0 } ;
36
37 #define PIFACE_BASE     200
38 #define PWM_OUT_PIN     204
39 #define PWM_UP          202
40 #define PWM_DOWN        203
41
42 void scanButton (int button)
43 {
44   if (digitalRead (PIFACE_BASE + button) == LOW)
45   {
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") ;
50   }
51
52   while (digitalRead (PIFACE_BASE + button) == LOW)
53     delay (1) ;
54 }
55
56
57 int main (void)
58 {
59   int pin, button ;
60   int pwmValue = 0 ;
61
62   printf ("Raspberry Pi PiFace - Motor control\n") ;
63   printf ("==================================\n") ;
64   printf ("\n") ;
65   printf (
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"
68 "\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"
71 "4)\n\n") ;
72
73   wiringPiSetup () ;
74   piFaceSetup (PIFACE_BASE) ;
75   softPwmCreate (PWM_OUT_PIN, 100, 100) ;
76
77 // Enable internal pull-ups & start with all off
78
79   for (pin = 0 ; pin < 8 ; ++pin)
80   {
81     pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ;
82     digitalWrite    (PIFACE_BASE + pin, 0) ;
83   }
84
85   for (;;)
86   {
87     for (button = 0 ; button < 2 ; ++button)
88       scanButton (button) ;
89
90     if (digitalRead (PWM_UP) == LOW)
91     {
92       pwmValue += 10 ;
93       if (pwmValue > 100)
94         pwmValue = 100 ;
95
96       softPwmWrite (PWM_OUT_PIN, pwmValue) ;
97       printf ("PWM -> %3d\n", pwmValue) ;
98
99       while (digitalRead (PWM_UP) == LOW)
100         delay (5) ;
101     }
102
103     if (digitalRead (PWM_DOWN) == LOW)
104     {
105       pwmValue -= 10 ;
106       if (pwmValue < 0)
107         pwmValue = 0 ;
108
109       softPwmWrite (PWM_OUT_PIN, pwmValue) ;
110       printf ("PWM -> %3d\n", pwmValue) ;
111
112       while (digitalRead (PWM_DOWN) == LOW)
113         delay (5) ;
114     }
115
116     delay (5) ;
117   }
118
119   return 0 ;
120 }