官术网_书友最值得收藏!

The blinking program

To write our first C++ program, we are going to use Geany Programmer's Editor. To open Geany, click on the Raspberry icon, go to Programming, and then select Geany Programmer's Editor:

After opening Geany, you will see an unsaved file called Untitled. The first thing that we need to do is save the file. Click on File | Save as and give this file the name Blink.cpp.

Inside this file, write the following code to make the LED blink. You can download the Blink.cpp program from the Chapter02 folder of GitHub repository:

#include <iostream>
#include <wiringPi.h>

int main(void)
{
wiringPiSetup();
pinMode(15,OUTPUT);

for(;;)
{
digitalWrite(15,HIGH);
delay(1000);
digitalWrite(15,LOW);
delay(1000);
}
return 0;
}

If you have done Arduino programming before, you are likely to have understood around 90% of this code. This is because wiringPi allows us to write C++ programs in Arduino format: 

  1. In the preceding code, we first import the iostream and the wiringPi library. 
  2. Next, we have the main function, called int main. Since this function does not have any arguments, we write a void statement inside the round brackets.
  3. After this, the wiringPisetup() function initializes wiringPi. It assumes that this program will use the wiringPi numbering scheme. 
  4. Next, with the pinMode(15, OUTPUT) command, we are setting the wiringPi pin number 15 as the OUTPUT pin. This is the pin we have connected to the positive pin of the LED. 
  5. After that, we have an infinite for loop. The code written inside it will run infinitely, unless we stop it manually from the coding editor. 
  6. With the digitalWrite(15,HIGH) command, we write a HIGH signal on the LED, which means the LED will turn on. Instead of HIGH, we could also put the number 1.
  7. After this, with the delay(1000) command, we ensure that the LED is only on for one second.
  1. Next, with the digitalWrite(15,LOW) command, we write a LOW signal on the LED. This means that the LED will turn off for one second.
  2. Since this code is inside a for loop, the LED will keep turning on and off until we instruct it otherwise. 
主站蜘蛛池模板: 鲁甸县| 泗水县| 高唐县| 堆龙德庆县| 阿勒泰市| 荃湾区| 乳源| 崇文区| 石泉县| 鸡东县| 汤阴县| 陇南市| 云阳县| 沅江市| 板桥市| 和林格尔县| 于都县| 夹江县| 翁牛特旗| 桐梓县| 葫芦岛市| 孟州市| 循化| 武穴市| 望城县| 宜都市| 镇安县| 长海县| 南开区| 白银市| 沙坪坝区| 涡阳县| 高邑县| 乐昌市| 洪湖市| 南江县| 农安县| 临泽县| 太谷县| 伊宁市| 霍林郭勒市|