2016年1月9日 星期六

透過COM port控制Arduino的辦法

這篇文章 大概就是我需要的解藥了
http://coopermaa2nd.blogspot.tw/2011/02/arduino-serial-led.html

內文提到使用switch (inByte)這指令來判斷按下的鍵盤按鈕為何?
這下對於特殊的ASCII就會有疑惑了,比如說上下左右呢?
果然網路上的資料真多,一查馬上有.

First Character is Escape (27 [0x1B]) followed by...
Key PressedASCII CharacterKey PressedASCII Character
F10x3BINS0x52
F20x3CDEL0x53
F30x3DHOME0x47
F40x3EEND0x4F
F50x3FPAGE UP0x49
F60x40PAGE DOWN0x51
F70x41UP Arrow0x48
F80x42DOWN Arrow0x50
F90x43LEFT Arrow0x4B
F100x44RIGHT Arrow0x4D
F110x57WINDOWS0x5B
F120x58Print Screen0x54

瞧這就是答案了.嘿嘿~距離計畫完成又更進一點了.

 switch(inByte)
        {
            case 0x49:
              Serial.print("[PgUp]");
              break;
            case 0x51:
              Serial.print("[PgDn]");
              break;    

搭配上回的步進馬達控制 組合起來就開心啦...
// defines pins numbers
const int XstepPin = 2;
const int XdirPin = 5;
const int YstepPin = 3;
const int YdirPin = 6;
// Makes pulses for making one full cycle rotation
const  int pulses = 2;

void setup() {
  Serial.begin(9600);
  // Sets the 4 pins as Outputs
  pinMode(XstepPin,OUTPUT);
  pinMode(XdirPin,OUTPUT);
  pinMode(YstepPin,OUTPUT);
  pinMode(YdirPin,OUTPUT);
}
void loop() {

  if (Serial.available() > 0) {
    int inByte = Serial.read();
    switch (inByte) {
    //UP Arrow
    case '0x48':
      digitalWrite(YdirPin,HIGH); // Enables the motor to move in a particular direction
      for(int y = 0; y < pulses; y++) {
        digitalWrite(YstepPin,HIGH);
        delayMicroseconds(50);
        }
        digitalWrite(YstepPin,LOW);
      break;
   
    //DOWN  Arrow
    case '0x50':
      digitalWrite(YdirPin,LOW); // Enables the motor to move in a particular direction
      for(int y = 0; y < pulses; y++) {
        digitalWrite(YstepPin,HIGH);
        delayMicroseconds(50);
        }
        digitalWrite(YstepPin,LOW);
      break;
     
    //LEFT Arrow
    case '0x4B':
      digitalWrite(XdirPin,LOW); // Enables the motor to move in a particular direction
      for(int x = 0; x < pulses; x++) {
      digitalWrite(XstepPin,HIGH);
      delayMicroseconds(50);        
      }  
      digitalWrite(XstepPin,LOW);
      break;

    //RIGHT Arrow
    case '0x4D':
      digitalWrite(XdirPin,HIGH); // Enables the motor to move in a particular direction
      for(int x = 0; x < pulses; x++) {
      digitalWrite(XstepPin,HIGH);
      delayMicroseconds(50);
      }  
      break;
      digitalWrite(XstepPin,LOW);
    }



PS.
文中其中提到沒有直接反應的RS232軟體.感謝Google
馬上找到免費的

AccessPort 1.36 RS232 串列埠存取工具

http://tedmyblog.blogspot.tw/2011/11/accessport-136-rs232.html

沒有留言:

張貼留言

小說創作. Part 4 故事的設定集

  1. 懷錶的時空穿越設定 基本設定 名稱 :時空懷錶(暫定,未正式命名) 外觀 :老舊的懷錶,外殼磨損嚴重,內側刻有「陽子 母ちゃんが守るよ」和「建桜」字樣,指針常停在1942年。 起源 :1992年由咲良在港口撿到,疑似二戰時期美軍士兵遺落,後由健一修復並發現其特殊能力。 ...