- Hands-On Robotics Programming with C++
- Dinesh Tavasalkar
- 370字
- 2021-06-24 15:30:31
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:
- In the preceding code, we first import the iostream and the wiringPi library.
- 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.
- After this, the wiringPisetup() function initializes wiringPi. It assumes that this program will use the wiringPi numbering scheme.
- 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.
- 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.
- 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.
- After this, with the delay(1000) command, we ensure that the LED is only on for one second.
- 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.
- Since this code is inside a for loop, the LED will keep turning on and off until we instruct it otherwise.
推薦閱讀
- Python程序設計教程(第2版)
- Oracle Database In-Memory(架構與實踐)
- Python自動化運維快速入門
- Learning AWS Lumberyard Game Development
- 深度強化學習算法與實踐:基于PyTorch的實現
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- PLC編程與調試技術(松下系列)
- Building Serverless Applications with Python
- Python深度學習原理、算法與案例
- 圖數據庫實戰
- Java EE企業級應用開發教程(Spring+Spring MVC+MyBatis)
- C語言程序設計實訓教程與水平考試指導
- Python網絡爬蟲技術與應用
- Python機器學習與量化投資
- 程序員的成長課