顯示具有 ROSA (Arduino) 標籤的文章。 顯示所有文章
顯示具有 ROSA (Arduino) 標籤的文章。 顯示所有文章

2020年6月19日 星期五

ROSA 2020 系統開發 4 ─ 超音波距離偵測器

https://4rdp.blogspot.com/2020/06/rosa-2020-4.html

會開發這個程式是因為正好在重新設計 ROSA,文創造設計工作室的張宇文老師正在設計一個超音波感測讓垃圾桶蓋開閉的程式,因此就順手寫一個相同功能的程式。

主程式為 ROSA_Door.ino,Arduino pin 12, 11 分別連接超音波感測器 trig, echo,pin 9 接 servo ,並使用 USB 來幫忙除錯,所以 pin 1, 0 分別為 TX, RX,baud 115200 bps,程式碼如下:

// (C) 2019-2020, Bridan Wang, CC BY-NC-SA 3.0 TW
// This is a demo program applied Robot Operating System for Arduino (ROSA)
// http://4rdp.blogspot.tw/search/label/ROSA%20(Arduino)

//軟件許可協議
//
//研發養成所 Bridan Wang 提供此軟體供學校教育或個人單獨使用
//對外分享展示本軟體時,請說明來源來自研發養成所
//你可以架構在本軟體基礎上,設計新功能或修改
//本軟體屬於 Bridan 和或其它原始碼供應商,並受適用的法律版權保護
//此軟體按“原樣”提供,可能含有錯誤,不作任何明示,暗示或法律的保證
//本軟體僅限 Arduino 部分微控制器產品,適用於特定用途
// Bridan 在任何情況、環境以及特殊使用不負任何原因損害賠償責任
//
//這是 ROSA 韌體版本的一部分。
//
//建議使用 Arduino-1.8.8 以後版本編譯,因為有發現舊版本有錯誤情形

 
/***********************************************************
// System Condition DEFINE
************************************************************/
#define PRODUCT  "ROSA,Door "
#define VERSION  "v2020.5.12"

//#define TIME_2ms      // ROSA_TIME_2ms()
//#define TIME_10ms     // ROSA_TIME_10ms()
#define TIME_50ms       // ROSA_TIME_50ms()
//#define TIME_100ms    // ROSA_TIME_100ms()
//#define TIME_250ms    // ROSA_TIME_250ms()
#define TIME_500ms      // ROSA_TIME_500ms()
//#define TIME_1sec     // ROSA_TIME_1sec()

//#define TIME_SYSTEM   // ROSA_TIME_SETUP()

#define STRING_LENGTH 25
/*********************************************************** // Include ************************************************************/ #include ".\ROSA\ROSA_BASE.cpp" #include ".\ROSA\ROSA_LINK.cpp" #include ".\ROSA\ROSA_SENSOR.cpp" #include ".\ROSA\ROSA_SERVO.cpp" ROSA_LINK  link; ROSA_SONAR sonar; ROSA_SERVO servo; /*********************************************************** // Main Program ************************************************************/ void setup() {   link.USB_SETUP(115200);         // pin 1, 0  TX, RX USB, 115200 baud   sonar.SETUP(12, 11);            // pin trig, echo   servo.SETUP(9);                 // pin 9 } void loop()  {   ROSA_TIME_RUNNING();       // 系統時間處理 } /*********************************************************** // SYSTEM TIMER ************************************************************/ void ROSA_TIME_50ms() {   servo.PROCESS();           // 伺服馬達處理 } void ROSA_TIME_500ms() {   int cm = sonar.DETECT();   // 超音波偵測   link.PRINT(STRING("Distance : %d cm\n", cm)); // 印出以便偵錯      if (cm < 25) {             // 接近時開動     servo.setAngle = 180;     servo.hold = HOLDON;   } else if (cm > 100) {     // 遠離時關閉     servo.setAngle = 0;     servo.hold = 0;   } }

2020年6月11日 星期四

ROSA 2020 系統開發 3 ─ Arduino 的 String

https://4rdp.blogspot.com/2020/06/rosa-2020-3-arduino-string.html

最近開始動手重寫 ROSA (Robot Operating System for Arduino),原本想利用 Arduino 的字串函數來處理字串,發現它的函數庫尚未優化,會佔用很多記憶體,因此留文記錄問題,並提供 ROSA 的解決方案。

正式討論 ROSA 程式之前,先從資料結構說起,話說 String 是一串字元以零值結尾,可以表示成

String str1 = "123456789"; 它也可以是字元陣列,
char str2[10] = "123456789";   或
char str3[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 0};  或
char str4[10] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00};  或
char str5[10] = {49, 50, 51, 52, 53, 54, 55, 56, 57, 0}; 甚至可以是字元指標,
char* str6 = str2;   將 str6 指標指向 str2 字串的起頭。

了解上述概念後,我們寫幾個程式測試,你會更加清楚怎麼一回事。
void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  String str1 = "123456789";
  Serial.println(str1);
}

void loop() {
  // put your main code here, to run repeatedly:
}
這個程式編譯後,程式碼佔用 ROM 2642 bytes,RAM 使用 208 bytes

後面的程式僅更改 setup() 比較差異,
void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  char str2[10] = "123456789";     // ROM 1546 bytes, RAM 198 bytes
  Serial.println(str2);
}
當陣列改成 {'1', '2', '3', '4', '5', '6', '7', '8', '9', 0}、{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00}  或 {49, 50, 51, 52, 53, 54, 55, 56, 57, 0},都和 "123456789" 一樣。

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  char str2[10] = "123456789";     // ROM 1546 bytes, RAM 198 bytes
  char* str6 = str2;
  Serial.println(str6);
}
使用指標也相同於陣列。
=====================================================

再來測試字串與數值混雜顯示的情形,
void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  int a = 255;
  char str2[4] = "ABCD";     // ROM 1576 bytes, RAM 196 bytes
  Serial.print(str2);
  Serial.print(a);
  Serial.println("EF");
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  int a = 255;
  Serial.println(String("ABCD")+a+"EF");     // ROM 3132 bytes, RAM 206 bytes
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  int a = 255;
  char str2[10];          // ROM 3020 bytes, RAM 196 bytes
  sprintf(str2, "ABCD%dEF", a);
  Serial.println(str2);
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  int a = 255;
  Serial.println(STRING("ABCD%dEF", a));     // ROM 1728 bytes, RAM 206 bytes
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  int a = 255;
  Serial.print(STRING("ABCD%dEF\n", a));     // ROM 1710 bytes, RAM 204 bytes
}
這個 STRING() 副程式是自己設計的,雖然無法像第一個例子 ROM size 那麼小,但是可以像 sprintf() 進行格式設定。

2020年5月30日 星期六

ROSA 2020 系統開發 2 ─ 投幣計時器 (4rdp Timer)

https://4rdp.blogspot.com/2020/05/rosa-2020-2-4rdp-timer.html

每次創客活動擺攤,4rdp 益智拼圖攤位前總是一堆遊客玩拼圖,因此讓我興起十元投幣贈獎的念頭,設計一個投幣計時器,投入十塊錢開始計時,只要五分鐘內排好拼圖,現場獎品隨便挑,反正家中很多雜物藉機出脫,即使拼圖沒賣出一個,總是有人會殺時間嘗試拼圖能力吧!

去年因參加創客比賽,學會雷切木盒子設計,因此計時器從外觀設計到內部電路及程式設計,一手包辦,下次擺攤時,這個計時器就可以讓遊客們打賞使用。

電路的挑選就是 Arduino Uno 和 LCD KeyPad 模組,一個按鈕 (停止鍵) 和微動開關 (偵測投幣)。

這個 LCD KeyPad 模組,使用 Arduino pin 4 ~ 9,pin 10 為背光控制,A0 是按鍵讀取的腳位,利用類比方式處理,因此不會占用過多數位腳,不過模組的按鍵這裡沒有使用。 另外這個投幣計時器使用 pin 3 偵測計時啟動,pin A1 偵測計時停止,這兩隻腳都設定數位輸入,並且 PULLUP,所以未按鍵時會讀到 1。

程式當然是使用 ROSA 2020,它使用了ROSA_BASE.cpp 、ROSA_DISPLAY.cpp、ROSA_TIMER.cpp 三個函式庫,ROSA_DISPLAY 內含 LCD KeyPad 驅動程式,並引用外部的程式庫 LiquidCrystal.h (Arduino 內含的程式庫)、LCDKeypad.h,ROSA_TIMER 函式可以處理碼錶正數和倒數計時,ROSA_BASE 除了基本時間和 IO 腳控制外,還加入 STRING,可將整數依據格式轉換成字串,這個 STRING 讓我花了五天時間把它最佳化,下一篇文章再仔細介紹設計的關鍵在哪裡。

現在繼續看 ROSA_Coin.ino 程式碼,系統使用 2ms 和10ms,2ms 用於碼錶計時,也是 ROSA 系統最小時間單位,10ms 為按鍵讀取時間間隔,原本設定 50ms 即可,不需要這麼密集,但是怕有人反應計時器按鍵反應遲鈍誤差高達 50ms,所以選擇 10ms,另外 Stop 按鍵持續一秒鐘,會把時間歸零。










// (C) 2019-2020, Bridan Wang, CC BY-NC-SA 3.0 TW
// This is a demo program applied Robot Operating System for Arduino (ROSA)
// http://4rdp.blogspot.tw/search/label/ROSA%20(Arduino)

//軟件許可協議
//
//研發養成所 Bridan Wang 提供此軟體供學校教育或個人單獨使用
//對外分享展示本軟體時,請說明來源來自研發養成所
//你可以架構在本軟體基礎上,設計新功能或修改
//本軟體屬於 Bridan 和或其它原始碼供應商,並受適用的法律版權保護
//此軟體按“原樣”提供,可能含有錯誤,不作任何明示,暗示或法律的保證
//本軟體僅限 Arduino 部分微控制器產品,適用於特定用途
// Bridan 在任何情況、環境以及特殊使用不負任何原因損害賠償責任
//
//這是 ROSA 韌體版本的一部分。
//
//建議使用 Arduino-1.8.8 以後版本編譯,因為有發現舊版本有錯誤情形


/***********************************************************
// System Condition DEFINE
************************************************************/
#define PRODUCT  "ROSA- 4rdp Timer"
#define VERSION  "v2020.5.12"

#define TIME_2ms      // ROSA_TIME_2ms()
#define TIME_10ms     // ROSA_TIME_10ms()
//#define TIME_50ms     // ROSA_TIME_50ms()
//#define TIME_100ms    // ROSA_TIME_100ms()
//#define TIME_250ms    // ROSA_TIME_250ms()
//#define TIME_500ms    // ROSA_TIME_500ms()
//#define TIME_1sec     // ROSA_TIME_1sec()

//#define TIME_SYSTEM   // ROSA_TIME_SETUP()

#define PROJECT_ROOT C:\Users\bridwang\Desktop\Bridan\ROSA_2020
#define TO_STRING(s) #s
#define ABSOLUTE_PATH(root, relative_path) TO_STRING(root\relative_path)
#define RELATIVE_PATH(library) ABSOLUTE_PATH(PROJECT_ROOT, library)
/***********************************************************
// Include
************************************************************/
#include RELATIVE_PATH(ROSA\ROSA_BASE.cpp)
#include RELATIVE_PATH(ROSA\ROSA_DISPLAY.cpp)
#include RELATIVE_PATH(ROSA\ROSA_TIMER.cpp)

ROSA_PIN        key_start;
ROSA_PIN        key_stop;
ROSA_LCD_KEYPAD lcd;
ROSA_TIMER      timer;

/***********************************************************
// Main Program
************************************************************/
void setup() {
  timer.START(0, 0, 0, 0, 0);        // start, hr, min, sec, msec  
  key_start.SETUP(3, INPUT_PULLUP);  // pin 3
  key_stop.SETUP(A1, INPUT_PULLUP);  // pin A1
  lcd.SETUP();                       // pin 4 ~ 9, 10, A0
  DISPLAY_INITIAL();
}

void loop() 
{
  ROSA_TIME_RUNNING();     // 系統時間處理
  DISPLAY_UPDATE();
}

/***********************************************************
// DISPLAY
************************************************************/

void DISPLAY_INITIAL() {
  lcd.CLEAR();
  lcd.PRINT(PRODUCT);
  lcd.CURSOR(0,1);   // 換行
  lcd.PRINT("  00:00:00.000");
}

void DISPLAY_UPDATE() {
  lcd.CURSOR(2,1);   // 換行
  lcd.PRINT(STRING("%02d:", timer.hr));
  lcd.PRINT(STRING("%02d:", timer.min));
  lcd.PRINT(STRING("%02d.", timer.sec));
  lcd.PRINT(STRING("%03d", timer.msec));
}

/***********************************************************
// SYSTEM TIME
************************************************************/
void ROSA_TIME_2ms()
{
  timer.RUNUP();
}

void ROSA_TIME_10ms()
{
  if (key_stop.IN()==0) {
    if (key_stop.count == 100)        // 持續按 STOP 一秒
      timer.START(0, 0, 0, 0, 0);     // start, hr, min, sec, msec
    else {
      timer.start = 0;                // 按一下 STOP
      key_stop.count++;
    }
  } else {
    key_stop.count = 0;
    if (key_start.IN()==0)            // 投幣 START
      timer.START(1, 0, 0, 0, 0);     // start, hr, min, sec, msec
  }
}

2020年5月22日 星期五

ROSA 2020 系統開發 1 ─ LED control

https://4rdp.blogspot.com/2020/05/rosa-2020-1-led-control.html

今年因為 COVID-19 的關係,把一些活動行程打亂,最近有空再重整 ROSA (Robot Operation System for Arduino) 程式碼,程式庫改以 C++ 物件導向設計,因此 ROSA 2020 跟 ROSA 2019 還是有許多差異,不過基本概念仍然相通。以下程式使用 Arduino Uno 控制 LED 為例,將程式寫得更結構化,以此 ROSA 再出發。


ROSA 主程式目錄檔案

ROSA 函式庫檔案

主程式為 ROSA_LED.ino,下面藍色部分,它讓 pin 13 的 LED 亮 250 ms,滅 250 ms 循環閃爍,因為這程式不需要處理日期時間,所以取消綠色部分的程式,可讓程式更小。關於 ROSA 系統參數設定,基本上標示作品名稱及日期版本,和重要 #define,ROSA 設計成函式庫型式,就是讓一般使用者專注寫自己的程式碼,而專業開發者提供精簡好用的功能, 這些 #define 和 #include 是介於兩者之間的橋樑,讓那些函式庫程式碼引入,個人覺得這些放在主程式比較醒目,提醒使用者他的設定。

// (C) 2019-2020, Bridan Wang, CC BY-NC-SA 3.0 TW
// This is a demo program applied Robot Operation System for Arduino (ROSA)
// http://4rdp.blogspot.tw/search/label/ROSA%20(Arduino)

//軟件許可協議
//
//研發養成所 Bridan Wang 提供此軟體供學校教育或個人單獨使用
//對外分享展示本軟體時,請說明來源來自研發養成所
//你可以架構在本軟體基礎上,設計新功能或修改
//本軟體屬於 Bridan 和或其它原始碼供應商,並受適用的法律版權保護
//此軟體按“原樣”提供,可能含有錯誤,不作任何明示,暗示或法律的保證
//本軟體僅限 Arduino 部分微控制器產品,適用於特定用途
// Bridan 在任何情況、環境以及特殊使用不負任何原因損害賠償責任
//
//這是 ROSA 韌體版本的一部分。
//
//建議使用 Arduino-1.8.8 以後版本編譯,因為有發現舊版本有錯誤情形


/***********************************************************
// System Condition DEFINE
************************************************************/
#define PRODUCT  "ROSA,LED "
#define VERSION  "v2020.5.11"

//#define TIME_2ms      // ROSA_TIME_2ms()
//#define TIME_10ms     // ROSA_TIME_10ms()
//#define TIME_50ms     // ROSA_TIME_50ms()
//#define TIME_100ms    // ROSA_TIME_100ms()
#define TIME_250ms    // ROSA_TIME_250ms()
//#define TIME_500ms    // ROSA_TIME_500ms()
//#define TIME_1sec     // ROSA_TIME_1sec()

//#define TIME_SYSTEM   // ROSA_TIME_SETUP()

/***********************************************************
// Include
************************************************************/
#include ".\ROSA\ROSA_BASE.cpp"

ROSA_PIN     led;

/***********************************************************
// Main Program
************************************************************/
void setup() {
  //ROSA_TIME_SETUP(20, 5, 9, 0, 0, 0) ;  // YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
  led.SETUP(13, OUTPUT);                  // pin 13
}

void loop() 
{
  ROSA_TIME_RUNNING();     // 系統時間處理
}

/***********************************************************
// SYSTEM TIME
************************************************************/
void ROSA_TIME_250ms()
{
  led.OUT(CHANGE);     // 閃爍
}

所有 ROSA 程式必須包含 ROSA_BASE.cpp 函式庫,它是 ROSA 的基礎程式,先看一下程式碼,先說明跟時間相關部分,根據主程式 #define 設定,將需要的程式碼含入編譯。

2019年8月18日 星期日

呼吸燈設計

https://4rdp.blogspot.com/2019/08/blog-post.html

以 mBot 當載體,以 ROSA 為程式架構,設計呼吸燈

為 4rdp 拼圖夜燈設計燈光控制,這程式包含了亮度控制、呼吸燈、變色燈三種模式,現在簡易說明如下:

顏色控制 ─ 以 RGB 三色混光,每色數值 0 ~ 255
亮度控制 ─ lit_speed = 0 時,lit_auto 決定亮度,lit_auto = 0 燈滅, lit_auto = 255 燈最亮
呼吸燈 ─ lit_auto = 0 時,lit_speed 控制呼吸速度,1 最快,9 最慢,變化範圍 0.3 ~ 2.7 秒
變色燈 ─ lit_auto = 1~7 時,lit_speed 控制變色速度,1 最快,9 最慢,變化範圍 0.3 ~ 2.7 秒

程式碼如下:

2019年2月19日 星期二

ROSA 系統開發 65 ─ MENU 選單

https://4rdp.blogspot.com/2019/02/rosa-65-menu.html


上一期 ROSA 開發處理了日期與時間,因為 Nokia 5110 LCD 可顯示 48 x 84 pixels,所以它可顯示 6 x 14 字元,因此弄一個功能選單,本文就此說明。

第一行顯示日期時間,第二行顯示選單標題以反黑標示,第三到第六行為選單內容,另外左邊保留一 > 符號,用來表示選擇某個選項。

#include "..\ROSA_DEFINE.h"

#if defined(DISPLAY_NONE)
  #define ledPin 13  // 第13隻接腳控制 LED 明滅。
  byte state = 0;

  void ROSA_DISPLAY_SETUP() {
    // put your setup code here, to run once:
    pinMode(ledPin, OUTPUT);
  }

  void ROSA_DISPLAY_LOOP() {
    state = 1 - state;
    if (state==0)
      digitalWrite(ledPin, LOW); // 設定PIN13腳位為低電位= 0V ,LED 熄滅
    else
      digitalWrite(ledPin, HIGH); // 設定PIN13腳位為高電位= 5V ,LED 發亮
  }

  void ROSA_DISPLAY_DATE() {
  }
  
  void ROSA_DISPLAY_TIME() {
  }
#elif defined(DISPLAY_5110)
  #include
  #include
  Adafruit_PCD8544 display = Adafruit_PCD8544(9, 10, 11, 13, 12);

  char items[5];
  char select;

  void ROSA_DISPLAY_SETUP() {
    // put your setup code here, to run once:
    display.begin();
    // init done

    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(BLACK);
  }

  void ROSA_DISPLAY_LOOP() {
    display.clearDisplay();
    display.setCursor(0,0);
    display.print(PRODUCT);
    display.setTextColor(WHITE, BLACK); // 'inverted' text
    display.println(VERSION);
    display.setTextColor(BLACK);
    display.setCursor(0,30);
    //display.display();
  }
  
  void ROSA_DISPLAY_DATE() {
  char buf[5];
  sprintf(buf, "2%03d.", year);
    display.print(buf);
  sprintf(buf, "%02d.", month);
    display.print(buf);
  sprintf(buf, "%02d", day);
    display.println(buf);
    display.display();
  }
  
  void ROSA_DISPLAY_TIME() {
  char buf[3];
  sprintf(buf, "%02d:", hour);
    display.print(buf);
  sprintf(buf, "%02d:", minute);
    display.print(buf);
  sprintf(buf, "%02d", second);
    display.println(buf);
    display.display();
  }
  
  void ROSA_DISPLAY_DATE_TIME(byte location) {
  char buf[3];
  if (location == 0) {
      display.clearDisplay();
      display.setCursor(0,0);
      display.setTextColor(BLACK);
  }
  sprintf(buf, "%02d.", month);
    display.print(buf);
  sprintf(buf, "%02d ", day);
    display.print(buf);

  sprintf(buf, "%02d:", hour);
    display.print(buf);
  sprintf(buf, "%02d:", minute);
    display.print(buf);
  sprintf(buf, "%02d", second);
    display.print(buf);
    display.display();
  }

  #define ROSA_MENU(hd, it1, it2, it3, it4, sel)  {items[0]=hd; items[1]=it1; items[2]=it2; items[3]=it3; items[4]=it4; select=sel;}

  #define MENU_TEXT(ln)  {for (byte i=0 ; i<12 ; i++){display.print((char) pgm_read_byte(&MENUTEXT[ln][i]));}}

  void ROSA_DISPLAY_MENU() {
    display.setTextColor(WHITE, BLACK); // 'inverted' text
    display.setCursor(0,8);
    display.print("  ");
    MENU_TEXT(items[0]);
    display.setTextColor(BLACK);
    display.setCursor(12,16);
    MENU_TEXT(items[1]);
    display.setCursor(12,24);
    MENU_TEXT(items[2]);
    display.setCursor(12,32);
    MENU_TEXT(items[3]);
    display.setCursor(12,40);
    MENU_TEXT(items[4]);
    display.setCursor(0,(select+1)*8);
    display.print(">");
    display.display();
  }
#endif

2019年2月11日 星期一

ROSA 系統開發 64 ─ 日期時間的顯示

https://4rdp.blogspot.com/2019/02/rosa-64.html


ROSA 系統函式庫化,跟其它 Arduino 函式庫是有些差異的,因為一般的 Arduino 函式庫採取草稿碼 / 匯入程式庫 / 加入 ZIP 程式庫,然後 ino 主程式 #include <...> 就可以使用這些程式庫,而 ROSA 的函式庫是不匯入程式庫的,為什麼要這麼做?因為 ROSA 的函式庫跟其它函式庫不同在它會去含入外部定義的資料,這使得 ROSA 的函式庫必須跟主程式在同一目錄下。

這個新版程式的函式庫先設計兩個:ROSA_TIMER.cpp 及 ROSA_DISPLAY.cpp
顧名思義,TIMER 處理時間與日期,DISPLAY 處理顯示部分,

// (C) 2019, Bridan Wang, CC BY-NC-SA 3.0 TW
// This is a demo program applied Robot Operation System for Arduino
// http://4rdp.blogspot.tw/search/label/ROSA%20(Arduino)

//軟件許可協議
//
//研發養成所 Bridan Wang 提供此軟體供學校教育或個人單獨使用
//對外分享展示本軟體時,請說明來源來自研發養成所
//你可以架構在本軟體基礎上,設計新功能或修改
//本軟體屬於 Bridan 和或其它原始碼供應商,並受適用的法律版權保護
//此軟體按“原樣”提供,可能含有錯誤,不作任何明示,暗示或法律的保證
//本軟體僅限 Arduino 部分微控制器產品,適用於特定用途
// Bridan 在任何情況、環境以及特殊使用不負任何原因損害賠償責任
//
//這是 ROSA 韌體版本的一部分。
//
//建議使用 Arduino-1.8.8 以後版本編譯,因為有發現之前版本有錯誤情形

/***********************************************************
// Include
************************************************************/
#include "ROSA_DEFINE.h"
#include ".\ROSA\ROSA_TIMER.cpp"
#include ".\ROSA\ROSA_DISPLAY.cpp"

/***********************************************************
// Main Program
************************************************************/
void setup() {
  ROSA_TIMER_SETUP();
  ROSA_DISPLAY_SETUP();
}

void loop() 
{
  ROSA_TIMER_RUNNING();
}

/***********************************************************
// SYSTEM TIMER
************************************************************/
void TIMER_2ms()
{
}

void TIMER_10ms()
{
}

void TIMER_100ms()
{
}

void TIMER_1sec()
{
   ROSA_DISPLAY_LOOP();
   ROSA_DISPLAY_DATE();
   ROSA_DISPLAY_TIME();
}

2019年2月3日 星期日

ROSA 系統開發 63 ─ LCD 基本顯示測試

https://4rdp.blogspot.com/2019/02/rosa-63-lcd.html

上一期介紹了 Nokia 5110 LCD 硬體,今天就驅動程式進一步介紹,這裡寫了一個基本顯示測試,下面是 ROSA_DISPLAYcpp 的內容:

#include "..\ROSA_DEFINE.h"

#if defined(DISPLAY_NONE)
  #define ledPin 13  // 第13隻接腳控制 LED 明滅。
  byte state = 0;

  void ROSA_DISPLAY_SETUP() {
    // put your setup code here, to run once:
    pinMode(ledPin, OUTPUT);
  }

  void ROSA_DISPLAY_LOOP() {
    state = 1 - state;
    if (state==0)
      digitalWrite(ledPin, LOW); // 設定PIN13腳位為低電位= 0V ,LED 熄滅
    else
      digitalWrite(ledPin, HIGH); // 設定PIN13腳位為高電位= 5V ,LED 發亮
  }
#elif defined(DISPLAY_5110)
  #include
  #include
  Adafruit_PCD8544 display = Adafruit_PCD8544(9, 10, 11, 13, 12);

  void ROSA_DISPLAY_SETUP() {
    // put your setup code here, to run once:
    display.begin();
    // init done
  }

  void ROSA_DISPLAY_LOOP() {
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(BLACK);
    display.setCursor(0,0);
    display.println(PRODUCT);
    display.setTextColor(WHITE, BLACK); // 'inverted' text
    display.println(VERSION);
    display.display();
  }
#endif


2019年1月26日 星期六

ROSA 系統開發 62 ─ LCD Nokia 5110

https://4rdp.blogspot.com/2019/01/rosa-62-lcd-nokia-5110.html



在應用 Arduino 設計專案時,因為作品屬性不同,不常見有人使用點陣式 LCD,我覺得利用它可以開發很多特殊作品,因此 ROSA 2019 再出發,選擇一個平價 LCD 來設計,找來找去覺得 Nokia 5110 LCD 很適合,上圖左為 LCD 模組正面,上圖右為背面,Google 這個 5110 模組會發現有很多型式,意思是各家廠商所製作的模組排針位置不盡相同,我所買到的昰上圖的型式,所以將依照這樣的模組來設計 ROSA 程式,玩家設計時要注意小心。



2019年1月18日 星期五

ROSA 系統開發 61 ─ System Timer

https://4rdp.blogspot.com/2019/01/rosa-61-system-timer.html

寫任何嵌入式系統,系統時間控制非常重要,但是 Arduino 的使用者有很大比例是非專業程式設計師,基於這樣的情形,促使我想開源設計 Robot Operating System for Arduino,邁入第四個年頭,為了更普及 ROSA,所以將它函式庫化,請看 ROSA 系統開發 60 基本觀念。

以下是主程式,你只要改寫它就可以立即使用

// (C) 2019, Bridan Wang, CC BY-NC-SA 3.0 TW
// This is a demo program applied Robot Operating System for Arduino
// http://4rdp.blogspot.tw/search/label/ROSA%20(Arduino)

//軟件許可協議
//
//研發養成所 Bridan Wang 提供此軟體供學校教育或個人單獨使用
//對外分享展示本軟體時,請說明來源來自研發養成所
//你可以架構在本軟體基礎上,設計新功能或修改
//本軟體屬於 Bridan 和或其它原始碼供應商,並受適用的法律版權保護
//此軟體按“原樣”提供,可能含有錯誤,不作任何明示,暗示或法律的保證
//本軟體僅限 Arduino 部分微控制器產品,適用於特定用途
// Bridan 在任何情況、環境以及特殊使用不負任何原因損害賠償責任
//
//這是 ROSA 韌體版本的一部分。

/***********************************************************
// Include
************************************************************/
#include "ROSA_DEFINE.h"

/***********************************************************
// SYSTEM TIMER
************************************************************/
#define ledPin 13  // 第13隻接腳控制 LED 明滅。
byte state = 0;

void TIMER_2ms()
{
}

void TIMER_10ms()
{
}

void TIMER_100ms()
{
  state = 1 - state;
  if (state==0)
    digitalWrite(ledPin, LOW); // 設定PIN13腳位為低電位= 0V ,LED 熄滅
  else
    digitalWrite(ledPin, HIGH); // 設定PIN13腳位為高電位= 5V ,LED 發亮
}

void TIMER_1sec()
{
}

#include ".\ROSA\ROSA_TIMER.cpp"

void setup() {
  ROSA_TIMER_SETUP();
  pinMode(ledPin, OUTPUT);
}

void loop() 
{
  ROSA_TIMER_RUNNING();
}


執行這個程式只要有一片 UNO 即可,紅字部分的程式碼,放在 100 ms 副程式中,可使 LED 亮 0.1 秒,滅 0.1 秒,如果放在 1 sec 副程式,LED 將亮 1 秒,滅 1 秒。

2019年1月2日 星期三

ROSA 系統開發 60 ─ Arduino 的 #include 與 #define

https://4rdp.blogspot.com/2019/01/rosa-60-arduino-include-define.html

2019 祝大家新年快樂,2018 年時間投入在 4rdp 四格益智拼圖的開發,今年將再度回歸 ROSA  (Robot Operating System for Arduino) 開發,經過一年時間沉潛,沈思若要 ROSA 更加普及,須要將它寫成函式庫型態,所以我就從這地方出發。

ROSA 有一點與其他函式庫不同的地方,它有比較完整的程式架構,希望 Arduino 初階入門的朋友只要改動一些設定就可以立即使用,因此 C/C++ 語言中 #include 和 #define 是非常重要的語法,所以本節說明相關要點。

我們先建立一個 ROSA_Timer.ino 主程式,程式碼如下:

#define ledPin 13  // 第13隻接腳控制 LED 明滅。
// setup() 函數只會於電源啟動時執行1次
void setup() 
{
  // 設定第 13 支腳為輸出模式
  pinMode(ledPin, OUTPUT);
}
// loop() 函數會不斷的重複執行
void loop()
{
  digitalWrite(ledPin, HIGH); // 設定PIN13腳位為高電位= 5V ,LED 發亮
  delay(500); // 等待500 毫秒,也就發亮 0.5 秒
  digitalWrite(ledPin, LOW); // 設定PIN13腳位為低電位= 0V ,LED 熄滅
  delay(500); // 等待500 毫秒,也就熄滅 0.5 秒
}
------------------------------------------------------------

接下來 ROSA_Timer.ino 程式改寫為:

#define ledPin 13  // 第13隻接腳控制 LED 明滅。
// setup() 函數只會於電源啟動時執行1次
void setup() 
{
  // 設定第 13 支腳為輸出模式
  pinMode(ledPin, OUTPUT);
}
// loop() 函數會不斷的重複執行
void loop()
{
  led();
}

另外新增一個 ROSA_LED.cpp 程式

void led()
{
  digitalWrite(ledPin, HIGH); // 設定PIN13腳位為高電位= 5V ,LED 發亮
  delay(500); // 等待500 毫秒,也就發亮 0.5 秒
  digitalWrite(ledPin, LOW); // 設定PIN13腳位為低電位= 0V ,LED 熄滅
  delay(500); // 等待500 毫秒,也就熄滅 0.5 秒
}

程式編譯過程會出現下列錯誤
error: 'led' was not declared in this scope
---------------------------------------------------------------

2018年7月13日 星期五

ROSA 系統開發 59 ─ OTTO like 跳舞控制

https://4rdp.blogspot.com/2018/07/rosa-59-otto-like.html

const byte RUN_4[] PROGMEM = {
//   0, 1,   2, 3, 4,   5, 6,
_TEMPO,  23, idl, idl, idl, idl, idl,    //   23 x 10 ms
_FAST2, LMD, LSD, RMD, RD3, idl, idl,    //
_FAST2, LMD, LSD, RMD, RSD, idl, idl,    //
_JUMP,    1,
};
const byte RUN_5[] PROGMEM = {            // 快腳 15"
//   0, 1,   2, 3, 4,   5, 6,
_GOTO,   18, idl, idl, idl, idl, idl,
_STOP,
};
const byte RUN_6[] PROGMEM = {
//   0, 1,   2, 3, 4,   5, 6,
 _GOSUB,   3, idl, idl, idl, idl, idl,   // 無限重複 極樂淨土
 _LOOP,   58, idl, idl, idl, idl, idl,
 _DANCE, LMD, LSD, RMD, RSD, idl, idl,
 _DANCE, LMD, LSD, RMD, RSD, idl, idl,
 _NEXT,    2, idl, idl, idl, idl, idl,
 _JUMP,    0,
};
const byte RUN_8[] PROGMEM = {                // OTTO speed normal
//   0, 1,   2, 3, 4,   5, 6,
_SPEED,   1, 1, 1,   1, 1, 1, //0
   _RETURN,                                 //1
};


_TEMPO, 23, idl, idl, idl, idl, idl,      一拍節奏 230 ms

2018年5月10日 星期四

ROSA 系統開發 58 ─ Motion_OTTO.h

https://4rdp.blogspot.com/2018/05/rosa-58-motionottoh.html

OTTO like 已經預編行走與跳舞動作,進階玩家可以參考修改或新編動作,開啟 Motion_OTTO.h 檔案,可以看見下列內容:

const byte RUN_1[] PROGMEM = {                // OTTO wave
//   0, 1,   2, 3, 4,   5, 6,
_SERVO, hof, hof, hof, hof, hof, hof,   //0
_LOOP,    3, idl, idl, idl, idl, idl,   //1
_SERVO, LR4, idl, RR4, idl, idl, idl,   //2
_SERVO, LL4, idl, RL4, idl, idl, idl,   //3
_NEXT,    3, idl, idl, idl, idl, idl,   //4
_SERVO, LMD, LSD, RMD, RSD, idl, idl,   //5
_LOOP,    3, idl, idl, idl, idl, idl,   //6
_SERVO, hon, idl, hon, idl, idl, idl,   //7
_NEXT,    2, idl, idl, idl, idl, idl,   //8
_GOTO,   22,                      // goto RUN_N
};


const    代表常數設定
byte     資料是位元組型態
RUN_1[]  動作資料取名為 RUN_1 陣列
PROGMEM  資料儲存在 flash ROM

//   0, 1,   2, 3, 4,   5, 6,
//      左腿上肢 右腿上肢
//           左腿下肢 右腿下肢