====== Nightlamp ====== **//The Circuit://** {{:hpc0:nightlamp.jpg?nolink|}} **//The Code://** 1 /********************************************************************** 2 * Filename : Nightlamp.cpp 3 * Description : Photoresistor control LED 4 * Author : www.freenove.com 5 * modification: 2020/03/09 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(); //Tells us what hardware we are using 18 int value; //Used for calculating voltage 19 float voltage; //Stores the voltage 20 printf("Program is starting ... \n"); 21 //check to see what ADC device we have 22 if(adc->detectI2C(0x48)){ // is it the pcf8591. 23 delete adc; // Make certain adc is empty 24 adc = new PCF8591(); // store what device we have 25 } 26 else if(adc->detectI2C(0x4b)){ // is it the ads7830 27 delete adc; // Make certain adc is empty 28 adc = new ADS7830(); // store what device we have. 29 } 30 else{ 31 printf("No correct I2C address found, \n" 32 "Please use command 'i2cdetect -y 1' to check the I2C address! \n" 33 "Program Exit. \n"); 34 return -1; 35 } 36 wiringPiSetup(); //Setup the board 37 softPwmCreate(ledPin,0,100); 38 while(1){ 39 value = adc->analogRead(0); //read analog value of first pin 40 softPwmWrite(ledPin,value*100/255); 41 voltage = (float)value / 255.0 * 3.3; // calculate voltage 42 printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage); 43 delay(100); 44 } 45 return 0; 46 } This project is very similar to the [[hpc0:softlight|Softlight]] project with the only key changes being that we swapped out the potentiometer with a photo-resistor. This difference allows the LED to receive a different amount of voltage depending on how much light the photo-resistor receives. The more light the LED gets the dimmer it gets; the less light the LED gets the brighter it gets. As for the code I aligned all the comments, changed up what the comments said in regards to new hardware, added a comment about the conditional, and made it so that the variables were declared outside of the loop.