====== Softlight ====== **//The Circuit://** {{:hpc0:softlight.jpg?nolink|}} **//The Code://** 1 /********************************************************************** 2 * Filename : Softlight.cpp 3 * Description : Use potentiometer to control LED 4 * Author : www.freenove.com 5 * modification: 2020/03/07 6 **********************************************************************/ 7 #include 8 #include 9 #include 10 #include 11 12 #define ledPin 0 13 14 ADCDevice *adc; // Define an ADC Device class object 15 16 int main(void){ 17 adc = new ADCDevice(); //Create a link to the Device 18 int adcValue; //Stores the a hardware value 19 float voltage; //Stores the voltage 20 printf("Program is starting ... \n"); 21 22 //are we using the pcf8591 23 if(adc->detectI2C(0x48)){ 24 delete adc; // Free previously pointed memory 25 adc = new PCF8591(); // If we are using it, create it 26 } 27 //are we using the ads7830 28 else if(adc->detectI2C(0x4b)){ 29 delete adc; // Free previously pointed memory 30 adc = new ADS7830(); // If we are using it, create it. 31 } 32 //if we have nothing 33 else{ 34 printf("No correct I2C address found, \n" 35 "Please use command 'i2cdetect -y 1' to check the I2C address! \n" 36 "Program Exit. \n"); 37 return -1; 38 } 39 wiringPiSetup(); //setup the board 40 softPwmCreate(ledPin,0,100); 41 while(1){ 42 adcValue = adc->analogRead(0); // read analog value for voltage 43 softPwmWrite(ledPin,adcValue*100/255); // Mapping to PWM duty cycle 44 voltage = (float)adcValue / 255.0 * 3.3; // Calculate voltage 45 printf("ADC value : %d ,\tVoltage : %.2fV\n",adcValue,voltage); 46 delay(30); 47 } 48 return 0; 49 } This project requires a few things to be done before the program can be ran. First I had to have the i2c interface enabled on the PI, then I had install the i2c tools, and finally I had to build the ADCDevice library. After doing all of this the program will run. Otherwise the circuit requires a potentiometer, the ADC module (I used the PCF8591) two 10k ohm resistors, one 220 ohm resistor, and an LED. When ran the LED will start at no brightness and grow brighter as I adjust the potentiometer. I also added more comments to the code to to describe the conditionals used, I aligned/edited to comments to make them read better, and I moved the declaration of variables to outside the while loop.