Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 78381|回復: 78
打印 上一主題 下一主題

arduino 的學習之路..( Part. 1~11 )

[複製鏈接]
跳轉到指定樓層
1#
發表於 2009-1-27 01:49:31 | 顯示全部樓層 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最後由 tommylin 於 2012-9-10 14:57 編輯

arduino Mini + USB Mini
LCD 2x16
光敏電阻

尚未遮蔽 光敏電阻 的數值顯示在 LCD

遮蔽 光敏電阻的時候, LCD 數據降低.

2#
 樓主| 發表於 2010-2-17 02:06:20 | 顯示全部樓層
先針對 Part. 1 做一些完整的補充..

[size=100%]~~ 安裝教學 Step By Step ~~

[size=85%]1. 先安裝電腦 Driver:
[size=85%]Download USBtoTTL Driver "CDM20600.exe",
[size=85%]下載連結 http://www.ftdichip.com/Drivers/VCP.htm
[size=85%]2. 將USB線插入電腦:
[size=85%]這時候打開工作管理員,
[size=85%]確認 COM PORT 的使用和驅動無誤( 使用的 PORT依情況會不同 ),
[size=85%]3. 執行 Arduino 0018:
[size=85%]設定 Tools->Board ( Arduino Mini )
設定 Tools->Serial Port ( COM 3)

[size=85%]4. 光感測器接線:
[size=85%]黑 - GND
[size=85%]紅 - 5V
[size=85%]白 - 信號 ( 接到 Arduino 的 digital pin 0 )
5. Parallax LCD接線:
GND
5V
RX  (接到 Arduino 的 TX)





[size=85%]~~ 使用材料 ~~
[size=85%]1. Arduino Mini.
[size=85%]2. USBtoTTL Board.
[size=85%]3. USB Cable.
[size=85%]4. 光感測器
[size=85%]5. LED
[size=85%]6. 麵包板.
[size=85%]7. Parallax LCD

























// 程式碼 //
int ledPin = 13; // LED connected to digital pin 13
int PhotoSPin = 0; // Photosensitive connected to digital pin 0
int Analog_val =0; // Photosensitive Val

void setup()
{
    Serial.begin ( 9600 );

    pinMode(ledPin, OUTPUT); // LED
    pinMode(PhotoSPin, INPUT); // Photosensitive

}

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(Analog_val); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(Analog_val); // wait for a second

Analog_val = analogRead(PhotoSPin); //讀取 光敏電阻 的數值

clearLCD (); // 清除 LCD 畫面

//===== 輸出數值到 LCD =====
Serial.print ( "Val= ");
Serial.print ( Analog_val, DEC );
Serial.print ( "\n");
}

// ===== clear the LCD =====
void clearLCD()
{
    Serial.print(12, BYTE);
}
3#
 樓主| 發表於 2010-2-17 02:09:10 | 顯示全部樓層

arduino Mini 的學習之路..( Part -2 )

本帖最後由 tommylin 於 2010-2-18 20:10 編輯

看了網頁上的一些前輩教學,( http://accrochages.drone.ws/en/node/90 )
接續我的 Arduino Part -1 程式 ,
加入應用 Processing 實現了圖形化的數據顯示,
和學習 Serial Port 如何和 PC <-> Arduino 通訊,
一方面也學習使用 Java 底層的應用工具程式 Arduino / Processing
相關網站 www.Arduino.cc / www.processing.org
因為開始複雜化了.. 2 x 16 LCD 不敷使用了.
上面的曲線就是光敏電阻傳回來的數值.

實驗使用環境:
SONY CPU i5 (2.13GHz),  RAM 3GB,
Windows 7 (64Bit)
Arduino 0018
Processing 1.0.9

接下來 Project 越來越精彩了... 敬請期待~

http://tommylin99.blogspot.com )
4#
 樓主| 發表於 2010-2-17 10:10:33 | 顯示全部樓層
本帖最後由 tommylin 於 2010-2-18 20:08 編輯

課目: 伺服馬達的控制  ( PWM )

我這個實驗使用的是 Hitec HSR-8498HB 伺服馬達.
目前使用驅動模式選擇第一種已經成功.
這個馬達總共有3種模式,
1. Standard Pulse Mode (Pulse width 550 to 2450 Microseconds)
角度計算式 Servo Angle (degrees) = (Pulse Width (‧S) – 1500) / 10

2. Extended Pulse Mode. (Pulse width 50 to 200 Microseconds) (未完成)
3. Serial Mode. (Pulse width 416 Microseconds) (未完成)

範例程式:
#include


Servo myservo; // create servo object to control a servo

void setup()
{
myservo.attach(9); //連接 Servo 信號到 Digital Pin 9

myservo.writeMicroseconds( 1024 ); //Pulse width 550 to 2450 Microseconds = Standard Pulse Mode
}

void loop()
{}
5#
 樓主| 發表於 2010-2-17 13:46:01 | 顯示全部樓層
本帖最後由 tommylin 於 2010-2-17 13:55 編輯


成功設定 Servo 轉動角度,
讀取 servo 角度數值.

範例程式:
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
void setup()
{
  Serial.begin( 19200 );
  myservo.attach(9);  //連接 Servo 信號到 Digital Pin 9
}
int angleVal=550;
void loop()
{
  myservo.writeMicroseconds( angleVal ); //Pulse width 550 to 2450 Microseconds = Standard Pulse Mode
  if ( angleVal + 100 <= 2450 )
   {
     angleVal += 100;
     delay ( 1000 );
   }
  else angleVal = 550;
  
  delay ( 300 );
  
  int readdata=0;
  readdata = myservo.read ();
  Serial.println ( readdata );
}
6#
 樓主| 發表於 2010-2-17 13:54:13 | 顯示全部樓層
呵呵
這是很好的教學哩, 繼續加油喔
不過為何你的arduino推的動馬達呢?
還是這顆馬達的電流需求不高!?
...
mzw2008 發表於 2010-2-17 12:21


Hi 鯨魚大大..
我直接用 Arduino 的 5V 接出來,
電表量馬達電壓大約 4.8 左右,
不知道電流多少 XD
只有接一顆應該OK吧~
7#
 樓主| 發表於 2010-2-17 15:00:04 | 顯示全部樓層
本帖最後由 tommylin 於 2010-2-17 15:01 編輯

更正~~~
剛剛確認下列的代碼是無效的... XD
myservo.read ();

等我研究清楚再來報告... Sorry ..
如果有哪位大大知道怎麼讀取 servo 數值的話,
請不吝賜教..謝謝嚕~
8#
 樓主| 發表於 2010-2-17 21:40:14 | 顯示全部樓層
感謝鯨魚大大的解說..
和 Datasheet 上面說的..
我有看沒有懂 = =
我目前功力不夠.. 所以先跳過了..
先進行下一個課目練習嚕~
9#
 樓主| 發表於 2010-2-18 12:26:42 | 顯示全部樓層
本帖最後由 tommylin 於 2010-3-10 16:29 編輯

網站好像不提供圖片上傳了..
先把圖上傳到別的網站..然後做連結... 測試一下...

10#
 樓主| 發表於 2010-3-10 16:30:21 | 顯示全部樓層
換了一張... 這張的背肌... 漂亮吧~
11#
 樓主| 發表於 2010-7-11 11:19:02 | 顯示全部樓層
本帖最後由 tommylin 於 2010-7-11 11:36 編輯

Part 4
控制 servo 轉動的速度,
而且要轉動的 Smooth
影片如下:

原始碼如下:
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
void setup()
{
  Serial.begin(9600);
  myservo.attach(9);
}
int pulse =2450;
int dir=1;
int offset=1;
void loop()
{
  myservo.writeMicroseconds( pulse );
  if ( pulse + offset >= 2450 ) dir = -1;
  else if ( pulse - offset <= 600 ) dir = 1;
  
  pulse += ( offset * dir);

  Serial.println ( pulse, DEC );
}
12#
 樓主| 發表於 2010-7-11 15:43:59 | 顯示全部樓層
本帖最後由 tommylin 於 2010-7-11 15:46 編輯

換轉動上臂的 servo , 最慢速..
13#
 樓主| 發表於 2010-7-11 15:47:05 | 顯示全部樓層
本帖最後由 tommylin 於 2010-7-11 16:23 編輯

換轉動上臂的 servo , 中速..
14#
 樓主| 發表於 2010-8-22 14:50:39 | 顯示全部樓層
本帖最後由 tommylin 於 2010-8-27 11:48 編輯

arduino Mini 的學習之路..( Part -5 )
曠課了好久 @@ 把買了幾個月的零件翻出來試試,

課目: RGB Led 的控制.
電路圖:


這個實作..關鍵在電路板上印 "-" 的地方要接 DC + .. 不要問我為什麼(我也不知道)
程式中, pin 的 Value 255 是關閉 LED, 明暗的數值是 0~254 (靠 Gooooogle 老師教的 )

影片:

原始程式碼:
int R_ledPin = 9;
int G_ledPin = 10;
int B_ledPin = 11;
int ledPin = 13;
// val 255 = off
int R_val=254;
int G_val=254;
int B_val=254;

void setup()
{
    pinMode(ledPin, OUTPUT);
    pinMode( R_ledPin, OUTPUT);
    pinMode( G_ledPin, OUTPUT);
    pinMode( B_ledPin, OUTPUT);
    Serial.begin ( 9600 );

    analogWrite ( R_ledPin, R_val );
    analogWrite ( G_ledPin, G_val );
    analogWrite ( B_ledPin, B_val );
    delay ( 1000 );
}
int dir=0;
int color = 0;
int offset = 5;

void loop()
{
    digitalWrite(ledPin, HIGH);
    switch ( color )
   {
      case 0: analogWrite ( R_ledPin, R_val ); break;
      case 1: analogWrite ( G_ledPin, R_val ); break;
      case 2: analogWrite ( B_ledPin, R_val ); break;
   }
   if ( dir == 0 )
  {
      if ( R_val - offset > 0 )
     {
       R_val -= offset;
     }
     else
    {
      R_val = 0;
      dir = 1;
    }
  }
  if ( dir == 1 )
{
   if ( R_val + offset < 255 )
   {
     R_val += offset;
   }
   else
  {
    R_val = 254;
    dir = 0;
    if ( color + 1 < 3 ) color ++;
    else color = 0;
   }
  }
  clearLCD (); // 清除 LCD 畫面
  // 輸出數值到 LCD
  Serial.print ( "R= ");
  Serial.print ( R_val, DEC );
  Serial.print ( "\n");
}

// ===== clear the LCD =====
void clearLCD()
{
  Serial.print(12, BYTE);
}
15#
 樓主| 發表於 2011-2-21 00:21:59 | 顯示全部樓層
Part 6:
這個版好冷@@ 我來貼一些...
最近連續做了2個和馬達有關的練習..
提供各位參考嚕..

Stepper motor 步進馬達的控制:

步進馬達的控制:
把老舊的 彩色印表機拆了,
發現裡面有一顆步進馬達,
使用 Arduino 接上馬達的4條線,
我使用了 pin 9~12,
輕易控制馬達前進後退和速度..


Code:
#include

const int stepsPerRevolution = 100;

Stepper myStepper(stepsPerRevolution, 9,10,11,12);

void setup() {
myStepper.setSpeed(100);
Serial.begin(9600);
}

void loop() {
Serial.println("clockwise");
myStepper.step(stepsPerRevolution * 10);
delay(500);

Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution * 10 );
delay(500);
}



影片:


16#
 樓主| 發表於 2011-2-21 00:27:22 | 顯示全部樓層
本帖最後由 tommylin 於 2011-2-21 00:28 編輯

Part 7:
無刷馬達的控制..
在這個學習中使用了搖控直升機的 無刷馬達和電子變速器(ESC)
使用材料:
Align ESC BL15X
Align BL250 Brushless motor

動作說明:
提升馬達速度, 降低馬達速度, 暫停..

Code:
#include

Servo myservo;

void setSpeed(int speed)
{
int val = map(speed, 0, 100, 0, 180);
myservo.write(val);
}
void setup()
{
Serial.begin(115200);
myservo.attach(9);
}
void loop()
{
Serial.println("System Ready !!! Hit the 1 ");
do{ if ( Serial.read() == '1' ) break; } while(1);


int speed;

Serial.println("Throttle up");
for(speed = 37; speed <= 90; speed += 1) { setSpeed(speed); Serial.println(speed); delay(100); } setSpeed(30); delay(1000); Serial.println("Throttle down"); for(speed = 90; speed > 37; speed -= 1) {
setSpeed(speed);
Serial.println(speed);
delay(100);
}
Serial.println("waiting for 5 sec...");
setSpeed(30);
delay(5000);
}

//=============================
另外也可以用程式..取代使用遙控器油門設定電變(ESC)的方式
例如電變的 緩啟動, 煞車模式, 截止電壓...等..
依照不同廠商生產不同的電變而有所差異..

Code:
// 程式設定ESC 狀態
void AdjustESC ()
{
//plug ESC Battery first, when ESC start music ready, enter "1" in serial window
Serial.println("Enter Setup Mode");
do{ if ( Serial.read() == '1' ) break;
setSpeed(90);
} while(1);
delay ( 2000 );
setSpeed(30);

// waiting for ESC edit mode music and into brake edit mode,
// then enter "1" in serial window
Serial.println("Break Mode - soft Brake");
do{ if ( Serial.read() == '1' ) break; } while(1);
setSpeed(50);


Serial.println("Electronic Timing -Mid timing");
do{ if ( Serial.read() == '1' ) break; } while(1);
setSpeed(50);


Serial.println("Battery Protection - High cut off voltage protection");
do{ if ( Serial.read() == '1' ) break; } while(1);
setSpeed(30);


Serial.println("Aircraft Mode - normal airplane");
do{ if ( Serial.read() == '1' ) break; } while(1);
setSpeed(30);


Serial.println("Throttle response speed - Quick speed");
do{ if ( Serial.read() == '1' ) break; } while(1);
setSpeed(90);


Serial.println("BEC output voltage - 5.5V");
do{ if ( Serial.read() == '1' ) break; } while(1);
setSpeed(50);

}

影片:
17#
 樓主| 發表於 2011-2-21 10:52:17 | 顯示全部樓層
由電腦連接Arduino控制DC馬達(可正反轉)最簡單的方式為何?
vegewell 發表於 2011-2-21 04:46



   Vegewell 大大.. 您的問題很棒,也是我下一個想做的課目,
目前找到的資料都是針對 "碳刷馬達", "步進馬達"的方案,
我會繼續尋找 "無刷馬達"的正反轉方案...

如果有人已經玩過了, 也懇請幫忙解惑吧~
18#
 樓主| 發表於 2011-2-21 15:49:54 | 顯示全部樓層
本帖最後由 tommylin 於 2011-2-21 15:52 編輯
回復  vegewell


    聽說:
[目前無刷馬達主要的應用大多在遙控航空模型上當做動力來源,但在益智娛樂 ...
vegewell 發表於 2011-2-21 15:14


原因很單純的..
碳刷是直接接觸, 會磨耗...
19#
 樓主| 發表於 2011-2-21 22:20:33 | 顯示全部樓層
Part - 8:

讀取 Futaba 2.4G 接收機油門信號..配件:
Futaba FF9 2.4G 發射器
Futaba R617FS 2.4G 接收器

Arduino Pin 9 連接 接收器 pin 3 信號
11.1V 鋰電 和 Arduino 共地 (GND)

執行結果:顯示數值
油門最低點 110
油門最高點 191











Code:

unsigned long TransmitterThrottle=0;

void setup()
{
  pinMode (9, INPUT);

  Serial.begin(57600);
  Serial.println("System Ready~");
}

void CheckTransmitter()
{
  TransmitterThrottle = (pulseIn (9, HIGH, 200000))/10;
}

void loop()
{
CheckTransmitter();
Serial.print ("pulse: ");
Serial.println (TransmitterThrottle);

delay (200);
}
20#
 樓主| 發表於 2011-2-22 23:40:06 | 顯示全部樓層
回復 30# vegewell

看起來這個有 遊星減速齒了,而且好像是傳說中可以調進角的馬達?
有這顆馬達的相關規格嗎?
多少齒比? 多少 Kv ? ... 多少錢 $_$
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-5-4 02:34 , Processed in 0.220103 second(s), 11 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表