Toms Online Notebook Sharing my stuff.

Raspberry Pi2 Pi4J Java - RGB LED Example

by tgutwin


Posted on Tuesday Jan 26, 2016 at 08:41PM in Technology


Pi4J IconI
have started to get some useful sample/examples completed on my
Raspberry Pi2. Here is the Schematic and Java code to control an RGB LED.


I also have examples for:



  • controlling multiple GPIO pins with external transistor driven LEDs and Java Pi4J code that times their cycling.

  • Pulse Width Modulation (PWM) control of GPIO pins and an RGP LED.


 These will be discussed in other posts.


RGB LED


This example controls an RGB LED with 3 GPIO pins and simply turns them On or Off.  I based this on the RGBLed.java code I saw at https://github.com/OlivierLD/raspberry-pi4j-samples.


It has a commandline interface that lets you control the three pins
individually by typing 'R' , 'G', or 'B' to allow you to have any
combination of the three colours.


Schematic:



Java Code:












RGBLed.java




1 /*
2 *$Rev:$:Revisionoflastcommit
3 *$Author:$:Authoroflastcommit
4 *$Date:$:Dateoflastcommit
5 *$URL:$
6 *
7 *Written by Tom Gutwin
8 *Copyright (C) 2015 Tom B. Gutwin,North Vancouver BC Canada
9 *
10 *This program is free software; you can redistribute it and / or modify
11 *it under the terms of the GNU General Public License as published by
12 *the Free Software Foundation; either version 3 of the License, or
13 *(at your option) any later version.
14 *
15 *This program is distributed in the hope that it will be useful,
16 *but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *MERCHANT ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 *GNU General Public License for more details.
19 *
20 *You should have received a copy of the GNU General Public License
21 *along with this program; If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 package ca.bc.webarts.raspberry;
25
26 import com.pi4j.io.gpio.GpioController;
27 import com.pi4j.io.gpio.GpioFactory;
28 import com.pi4j.io.gpio.GpioPinDigitalOutput;
29 import com.pi4j.io.gpio.PinState;
30 import com.pi4j.io.gpio.RaspiPin;
31
32 import java.io.BufferedReader;
33 import java.io.InputStreamReader;
34
35 /**Asimpleexamplecalsstocontrol3GPIOpinsthatareconnectedtoaRGBLED.**/
36 public class RGBLed
37 {
38 private static final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
39
40 public static String userInput(String prompt)
41 {
42 String retString = "";
43 System.err.print(prompt);
44 try
45 {
46 retString = stdin.readLine();
47 }
48 catch(Exception e)
49 {
50 System.out.println(e);
51 String s;
52 try
53 {
54 s = userInput("<Oooch/>");
55 }
56 catch(Exception exception)
57 {
58 exception.printStackTrace();
59 }
60 }
61 return retString;
62 }
63
64 public static void main(String[] args)
65 throws InterruptedException
66 {
67 System.out.println("GPIOControl-pin22,23&24...started.");
68
69 //creategpiocontroller
70 final GpioController gpio = GpioFactory.getInstance();
71
72 final GpioPinDigitalOutput redPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_23, "red", PinState.LOW);
73 final GpioPinDigitalOutput greenPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_24, "green", PinState.LOW);
74 final GpioPinDigitalOutput bluePin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_25, "blue", PinState.LOW);
75
76 /*
77 *yellow=R+G
78 *cyan=G+B
79 *magenta=R+B
80 *white=R+G+B
81 */
82
83 boolean go = true;
84 while (go)
85 {
86 String s = userInput("R,G,B,orQUIT>");
87 if ("R".equals(s.toUpperCase()))
88 redPin.toggle();
89 else if ("G".equals(s.toUpperCase()))
90 greenPin.toggle();
91 else if ("B".equals(s.toUpperCase()))
92 bluePin.toggle();
93 else if ("QUIT".equals(s.toUpperCase()) || "Q".equals(s.toUpperCase()))
94 go = false;
95 else
96 System.out.println("Unknowncommand[" + s + "]");
97 }
98 //Switchthemoff
99 redPin.low();
100 greenPin.low();
101 bluePin.low();
102
103 redPin.toggle(); //R
104 sleep(500);
105 redPin.toggle(); //G
106 greenPin.toggle();
107 sleep(500);
108 greenPin.toggle();//B
109 bluePin.toggle();
110 sleep(500);
111 bluePin.toggle();
112
113 redPin.toggle();//RG=Y
114 greenPin.toggle();
115 sleep(500);
116 redPin.toggle();//GB
117 //greenPin.toggle();
118 bluePin.toggle();
119 sleep(500);
120 redPin.toggle();//RGB
121 sleep(500);
122 greenPin.toggle();//RB
123 sleep(500);
124 bluePin.toggle();
125 redPin.toggle();
126 sleep(500);
127 redPin.high();
128 greenPin.high();
129 bluePin.high();
130 sleep(500);
131 redPin.low();
132 greenPin.low();
133 bluePin.low();
134 sleep(500);
135 redPin.high();
136 greenPin.high();
137 bluePin.high();
138 sleep(500);
139
140
141 //Switchthemoff
142 redPin.low();
143 greenPin.low();
144 bluePin.low();
145 //stopallGPIOactivity/threadsbyshuttingdowntheGPIOcontroller
146 //(thismethodwillforcefullyshutdownallGPIOmonitoringthreadsandscheduledtasks)
147 gpio.shutdown();
148 }
149
150
151 /**
152 *AmethodtosimplyabstracttheTry/Catchrequiredtoputthecurrent
153 *threadtosleepforthespecifiedtimeinms.
154 *
155 *@paramwaitTimethesleeptimeinmilliseconds(ms).
156 *@returnbooleanvaluespecifyingifthesleepcompleted(true)orwasinterupted(false).
157 */
158 public static boolean sleep(long waitTime)
159 {
160 boolean retVal = true;
161 if (waitTime<0)
162 retVal=false;
163 else
164 {
165 /*
166 *BLOCKforthespec'dtime
167 */
168 try
169 {
170 Thread.sleep(waitTime);
171 }
172 catch (InterruptedException iex)
173 {
174 retVal = false;
175 }
176 }
177 return retVal;
178 }
179
180 }
181



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed