Chapter 9 Potentiometer & RGBLED
118
Code
C Code 9.1.1 Colorful Softlight
First observe the project result, and then analyze the code.
1.
Use cd command to enter 09.1.1_ColorfulSoftlight directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/09.1.1_ColorfulSoftlight
2.
Use following command to compile "ColorfulSoftlight.c" and generate executable file "ColorfulSoftlight".
gcc ColorfulSoftlight.c -o ColorfulSoftlight -lwiringPi -lpthread
3.
Then run the generated file "ColorfulSoftlight".
sudo ./ColorfulSoftlight
After the program is executed, rotate one of potentiometers, then the color of RGBLED will change
consequently. And the terminal window will print out the ADC value of each potentiometer.
The following is the program code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>
#define address 0x48
//pcf8591 default address
#define pinbase 64
//any number above 64
#define A0 p 0
#define A1 p 1
#define A2 p 2
#define A3 p 3
#define ledRedPin 3
//define 3 pins of RGBLED
#define ledGreenPin 2
#define ledBluePin 0
int
main
(
void
){
int
val_Red
,
val_Green
,
val_Blue
;
if
(
wiringPiSetup
()
==
-
1
){
//when initialize wiring failed, print message to screen
printf
(
"setup wiringPi failed !"
);
return
1
;
}
softPwmCreate
(
ledRedPin
,
0
,
100
);
//create 3 PWM output pins for RGBLED
softPwmCreate
(
ledGreenPin
,
0
,
100
);
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...