Raspberry Pi: WiringPi, LED and LDR

If you want use the GPIO pins of Raspberry Pi, it is very important that you use wiringPi. You just need to compile wiringPi. This is very easy. You edit the CMakeLists.txt. You just place -lwiringPi at line 9 and line 11.

Then in the main.cpp, you define your pins and just add de library’s. I made a LED go on/off, blink and fade. I also used the LDR. This is my code: main.cpp (2.8 KB)

int main(void)
{ 
	// initialize thinger.io device
    thinger_device thing(USER_ID, DEVICE_ID, DEVICE_CREDENTIAL);
	
	wiringPiSetup();
	
	pinMode(LED_PIN, OUTPUT);
	pinMode(LDR_PIN, INPUT);
	pinMode(LEDPWM_PIN, PWM_OUTPUT);
	
	thing["LED"] << [](pson & in){
        if(in.is_empty()){
			digitalWrite(LED_PIN, in ? HIGH : LOW);
        }
		else{
        	if(in){
				digitalWrite (LED_PIN, HIGH);
        	}else{
        		digitalWrite (LED_PIN, LOW);
        	}
        }
	};
	thing["BLINKINGLED"] << [](pson& in){
		float amountOfBlinks;
		amountOfBlinks = (long)in["Amount of Blinks"];
		
		for (int a = 0; a < amountOfBlinks; a++)
		{
			digitalWrite (LED_PIN, HIGH) ;	// On
			delay (500) ;
			digitalWrite (LED_PIN, LOW) ;
			delay (500) ;
		}
	};
	thing["FADINGLED"] << [](pson & in){
		float amountOfFades;
		amountOfFades = (long)in["Amount of Fades"];
		
		pwmWrite(LEDPWM_PIN, 0);
		for (int i = 0 ; i < amountOfFades; i++)
		{
			for (int j = 0 ; j < 1024 ; j+=25)
			{
				pwmWrite (LEDPWM_PIN, j) ;
				delay (20) ;
			}
			for (int j = 1023 ; j >= 0 ; j-=25)
			{
				pwmWrite (LEDPWM_PIN, j) ;
				delay (20) ;
			}
		}
		pwmWrite(LEDPWM_PIN, 0);
	};
	thing["LDR"] >> [](pson & out){
        if (digitalRead(LDR_PIN) == HIGH){
			out = "Light";
		} 
		else{
			out = "Dark";
		}	
    };

	thing.start();
	return 0;
};

On YouTube you can find a preview this is the link: https://www.youtube.com/watch?v=h-LYV2mZCRg

4 Likes