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:
#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 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 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);