Arduino Tutorials – [1] Hello World

Getting prepared for these things:

  • Arduino
  • USB Cable
  • PC
  • Internet

 

First things first, let’s install Arduino IDE so that we can upload the code to the Arduino board.

  1. Download the software from http://arduino.cc/download or http://download.mby.pw/software/arduino.exe
  2. Install it. The drivers are included. Please choose “Install” when the driver installing box appears.  AT1-Screenshot-1
  3. Well done~

 

Secondly, let’s upload a test code.

  1. Connect your Arduino to your PC with an USB Cable.
  2. Launch Arduino IDE, then select Board Model (Arduino UNO) and Port in “Tools” (depending on your PC settings)AT1-Screenshot-2
  3. Click “File” -> “Example” -> “01. Basics” -> “Blink”, then click the second button on the top bar “Upload”.AT1-Screenshot-3
  4. Wait a moment until the code is uploaded.
  5. If all things don’t get wrong, you can see a light on the board blinking.
  6. Congratulations~ You succeed.

 

At last, you should understand the code.

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
点亮一秒一个LED灯,然后熄灭一秒,循环。
This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
// 在大多数Arduino板上,引脚13连接着一个板载的LED灯。
// 然后定义一个变量叫做 led,并赋值为13。
int led = 13;

// the setup routine runs once when you press reset:
// 一个被命名为setup的子程序在你按下RESET按钮(或刚上电)后只运行花括号里的代码一次。它通常用于初始化的工作。

void setup() {
// initialize the digital pin as an output.
// 一个被命名为setup的子程序在你按下RESET按钮(或刚上电)后只运行花括号里的代码一次。它通常用于初始化的工作。

//初始化LED(13)引脚为输出引脚。然后该引脚可用于输出高电平(一般为5V)或低电平(0V)
//数字引脚可以被设定为输入(将在下节讲)或输出模式。

//设置引脚模式函数用法:

//pinMode(pin, OUTPUT 或 INPUT);
//	     |               |
//要设置引脚模式的引脚   引脚模式(输出或输入)
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
// 又一个被命名为loop的子程序在执行完setup后一遍又一遍地执行花括号里的代码。
void loop() {
digitalWrite(led, HIGH);
// turn the LED on (HIGH is the voltage level)
// 设置led(13)引脚输出高电平。点亮LED。

// 设置引脚输出函数用法:
//digitalWrite(pin, HIGH 或 LOW);
//              |        |
// 要设置输出的引脚  输出电平(高电平或低电平)

delay(1000);
// wait for a second
// 延时1000毫秒(1秒)。

// 延时函数用法:
//delay(time);
//       |
//要延时的时间(毫秒)

digitalWrite(led, LOW);
// turn the LED off by making the voltage LOW
// 设置led(13)引脚输出低电平。熄灭LED。

delay(1000);
// wait for a second
// 延时1000毫秒(1秒)。
}

So fun. Try to change the time to delay. Then upload the code. What can you see?

The Plane Project [1] – 使用串口位运算传输飞行数据 / 控制请求

怎么传输飞行数据和控制请求是一个值得探究的问题。一开始我想用JSON,用Arduino的aJSON库和C#的写出了一些乱七八糟的代码,最后发现数据量、延时直接上天【500ms】,程序还经常跑飞,再加上无线模块的串口速率【19200】就更慢了ww,于是脑洞大开想到了位运算处理,将延时降到了【300ms】。

这是我最初使用JSON的Arduino核心代码:

void process_json() {
aJsonObject *temp_ex;

temp = aJson.getObjectItem(control_request, "Flaps");
if (temp) {
temp_ex = aJson.getObjectItem(temp, "l");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
flaps.l = temp_ex -> valueint;

temp_ex = aJson.getObjectItem(temp, "r");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
flaps.r = temp_ex -> valueint;
}

temp = aJson.getObjectItem(control_request, "Slaps");
if (temp) {
temp_ex = aJson.getObjectItem(temp, "l");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
slaps.l = temp_ex -> valueint;

temp_ex = aJson.getObjectItem(temp, "r");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
slaps.r = temp_ex -> valueint;
}

temp = aJson.getObjectItem(control_request, "Thro");
if (temp)
if (temp -> type == aJson_Int) throttle = temp -> valueint;

temp = aJson.getObjectItem(control_request, "SBak");
if (temp)
if (temp -> type == aJson_Int)
speed_brakes = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Rud");
if (temp)
if (temp -> type == aJson_Int)
rudder = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Ele");
if (temp)
if (temp -> type == aJson_Int)
elevator = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Trim");
if (temp)
if (temp -> type == aJson_Int)
trim = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Lig");
if (temp) {
temp_ex = aJson.getObjectItem(temp, "l1");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
lights[1] = temp_ex -> valuebool;

temp_ex = aJson.getObjectItem(temp, "l2");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
lights[2] = temp_ex -> valuebool;

temp_ex = aJson.getObjectItem(temp, "l3");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
lights[3] = temp_ex -> valuebool;
}
}

void create_json() {

aJson.deleteItem(flight_data);
flight_data = aJson.createObject();

aJson.addNumberToObject(flight_data, "MSpd", motor_speed);
aJson.addNumberToObject(flight_data, "ASpd", air_speed);
aJson.addNumberToObject(flight_data, "GSpd", ground_speed);
aJson.addNumberToObject(flight_data, "Alt", altitude);
aJson.addNumberToObject(flight_data, "Hea", heading);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "Lat", loc.lon);
aJson.addNumberToObject(temp, "Lou", loc.lat);
aJson.addItemToObject(flight_data, "Loc", temp);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "x", acc.x);
aJson.addNumberToObject(temp, "y", acc.y);
aJson.addNumberToObject(temp, "z", acc.z);
aJson.addItemToObject(flight_data, "Acc", temp);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "x", gyro.x);
aJson.addNumberToObject(temp, "y", gyro.y);
aJson.addNumberToObject(temp, "z", gyro.z);
aJson.addItemToObject(flight_data, "Gyro", temp);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "x", angle.x);
aJson.addNumberToObject(temp, "y", angle.y);
aJson.addNumberToObject(temp, "z", angle.z);
aJson.addItemToObject(flight_data, "Angle", temp);

aJson.addNumberToObject(flight_data, "BtVolt", battery_voltage);

}

 

 

The Plane Project [Index]

就是因为这个坑!=_=

image

 

于是就开始填233

目前使用C#作为地面端语言,使用Saitak的F.L.Y. 5控制无人机,Arduino和Raspberry Pi作为机上主要控制系统,搭载各种作死的传感器,如空速计,倾角传感器,地磁传感器等,和各种舵机,使用串口通信模块和DTU进行地面与空中的通信。如果可能,我将会加入自动驾驶仪功能。

别忘了,以后还有更新哟~

mWeather

Summary:

科学课上学气温和气压,于是我开始脑洞大开,想造一个简单的气象监视系统,可以通过Web Browser查看气温、气压、湿度、空气质量变化曲线和当前值。使用了我的Raspberry Pi、一对Arduino Uno、一对无线模块APC220、温湿度传感器AM2303、气压传感器BMP180、尘埃传感器GP2Y1050AU0F、网络扩展板Ethernet Shield和一堆乱七八糟的外围电路。

Features:

  • 可以查看气温、大气压、湿度、空气质量变化曲线和当前值。
  • 仪表板页面采用响应式布局。
  • 支持导出数据表。
  • 使用Bootstrap。

Screenshot:

Demo:

// 已暂停工作 http://mbysensor.wicp.net:2333

Known Issues:

在不久的将来,随着数据量的逐日增大(24*60*4=5760条/天)和树莓派比较渣的性能,数据查询速度会越来越慢,平均每天减慢0.2s左右,然后就没有然后了 🙁