本文章为入门级APM编程控制教程,使用arduino编程,编写最简单的流水灯实验。
编写环境arduino的安装以及APM飞控源码等内容就不在做介绍了,直接说如何进行APM的小灯控制。
打开后可以看到两个函数,分别是void setup()和void loop()其中setup()仅执行一次,loop()为永久循环,因此我们在setup中添加初始化引脚的按钮。
在APM飞控中,有三个小灯,分别为PA3、PA4、PA5,对应的arduino引脚为5、2、3,分别为蓝,黄和红三个小灯(可自行查看原理图)因此在setup()中初始化该三个引脚为输出,代码如下:(以下代码写到setup()函数中)
pinMode( 5, OUTPUT );
pinMode( 2, OUTPUT );
pinMode( 3, OUTPUT );
接下来就是让这三个等循环点亮,很简单,就是延时拉低某个引脚就可以了:(以下代码写到loop()中)
digitalWrite( 5, LOW);
digitalWrite( 2, HIGH );
digitalWrite( 3, HIGH );
delay(500);
digitalWrite( 5, HIGH );
digitalWrite( 2, LOW);
digitalWrite( 3, HIGH );
delay(500);
digitalWrite( 5, HIGH );
digitalWrite( 2, HIGH );
digitalWrite( 3, LOW);
delay(500);
连接USB线和APM飞控,在arduino上选择相应的串口,点击上传就可以将程序上传到APM上了,完成后就可以看到三个小灯在轮流闪烁,这样APM做出来的流水灯就完成了。
附上自己写的使用APM的电调信号输出口输出的LED流水灯程序。
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the Uno and
Leonardo, it is attached to digital pin 13. If you're unsure what
pin the on-board LED is connected to on your Arduino model, check
the documenta
tion
This example code is in the public domain.
modified 8 May 2014
by Scott Fitzgerald
*/
unsigned char pinMap[11] = {5,2,3,6,7,8,11,12,25,26,27};
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
unsigned char i;
//
for( i = 0; i < 11; i++ )
pinMode( pinMap
, OUTPUT );
}
// the loop function runs over and over again forever
void loop() {
unsigned char i,j;
for( i = 0; i < 8; i++ )
{
for( j = 0; j < 8; j++ )
digitalWrite( pinMap[j], HIGH );
digitalWrite( pinMap[7-i], LOW );
if( i == 0 )
{
digitalWrite( pinMap[8], HIGH );
digitalWrite( pinMap[9], HIGH );
digitalWrite( pinMap[10], HIGH );
}
if( i == 3 )
{
digitalWrite( pinMap[8], LOW );
digitalWrite( pinMap[9], LOW );
digitalWrite( pinMap[10], LOW );
}
delay(125);
}
for( i = 0; i < 8; i++ )
{
for( j = 0; j < 8; j++ )
digitalWrite( pinMap[j], HIGH );
digitalWrite( pinMap, LOW );
if( i == 0 )
{
digitalWrite( pinMap[8], HIGH );
digitalWrite( pinMap[9], HIGH );
digitalWrite( pinMap[10], HIGH );
}
if( i == 3 )
{
digitalWrite( pinMap[8], LOW );
digitalWrite( pinMap[9], LOW );
digitalWrite( pinMap[10], LOW );
}
delay(125);
}
}
原作者:杜杜dalek