首先先介紹一下Ardunio以下是她的官方網站。
http://www.arduino.cc/
簡單來講,如果有電子電機背景的人,在做出一實際有用之電路時,需要經過許多步驟,頗
為麻煩。
但是如果只對其電路其""效果""以及""功能""感興趣的人呢,Ardunio是個非常適合的選擇,其程
式碼簡單易懂,也不必耗費心神在複雜的電路之上,只需專注於其效果以及應用,在國外其
實已經非常風行,而亞洲方面也有許多好玩的東西,只不過像編譯器的開發目前在亞洲並沒
有看到太大的進展。
如果對Ardunio有興趣的人,可以到以下網誌查查其基礎,以及簡單的應用。
cooperma
裡面有許多基本功能的介紹,簡單易懂,我也時常在此查資料。
http://coopermaa2nd.blogspot.tw/GROBO
裡面有些應用,由於我寫程式的實力很弱,如果是資工背景的朋友可以到這個網誌查查其應用
http://gcyrobot.blogspot.tw/
先給大家看一下假的完成品。
功能如下 偵測到頭部動作,送出一個持續且微小的訊號(約2.2V),使繼電器產生開關,做到
控制的功能。
使用材料:
Ardunio*1
MPU 6050*1
電線數條
12V鹵素燈頭燈
繼電器
Ardunio與MPU 6050接角
Ardunio MPU6050
A4 SDA
MPU 6050*1
電線數條
12V鹵素燈頭燈
繼電器
Ardunio與MPU 6050接角
Ardunio MPU6050
A4 SDA
A5 SCL
3v VCC
GND GND
接好之後要安裝相關library,給編譯器來使用。
(之後會補下載連結)
裝好之後當然就是先開始寫程式啦~
接好之後要安裝相關library,給編譯器來使用。
(之後會補下載連結)
裝好之後當然就是先開始寫程式啦~
// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2011-10-07 - initial release
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"
// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"
// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define LED_PIN 13
bool blinkState = false;
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(38400);
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// configure Arduino LED for
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);
// display tab-separated accel/gyro x/y/z values
Serial.print("a/g:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
digitalWrite(LED_PIN,high);
}
以上是他給的範例,可以讀出RAWvalue,由於我們的功能是要偵測到動作就送出一個訊號
因此我們可以直接在loop函式內直接加上這樣功能的程式碼即可。
以上是他給的範例,可以讀出RAWvalue,由於我們的功能是要偵測到動作就送出一個訊號
因此我們可以直接在loop函式內直接加上這樣功能的程式碼即可。
/
#include <EEPROM.h>
void loop()
{
int address = 5;
int value;
value = EEPROM.read(address);
if(ax<100)
{
EEPROM.write(address, 0);
}
if((ax>100)
{
EEPROM.write(address, 1);
}
if( value < 2 && value > 0)
{
digitalWrite(8,HIGH);
}
if( value < 1 && value > -1 )
{
digitalWrite(LED_PIN,LOW);
}
Serial.println(value);
}
如果直接用
if(gx <100)
{
delay(1000);
}
是可以讓他按一秒,但是delay函式會影響到全部的功能故要較少使用。
影片
沒有留言:
張貼留言