---
# System prepended metadata

title: arduino 分享

---


arduino 分享
===
- [arduino分享](https://hackmd.io/@use-j/arduino分享)

# 1 //--- Serial
---
## 1-1 硬體 Serial 
- `EX: Arduino Due 多UART`
```cpp=
char code2 = '0', code3 = '0';
void setup(){
    Serial.begin(9600);
    //
    Serial2.begin(9600);
    Serial3.begin(9600);
}

void loop(){
    Serial.print("s2=");
    Serial.println(code2);
    Serial2.println("2 OK 222");

    Serial.print("s3=");
    Serial.println(code3);
    Serial3.println("3 OK 333");
}

//---
void serialEvent2(){
    while (Serial2.available() > 0)    {
        code2 = Serial2.read();
    }
}

void serialEvent3(){
    while (Serial3.available())    {
        code3 = Serial3.read();
    }
}


```
## 1-2 SoftwareSerial
```cpp=
#include <SoftwareSerial.h>

SoftwareSerial portOne(10, 11);
SoftwareSerial portTwo(8, 9);

void setup() {
  Serial.begin(9600);
  while (!Serial) {;}
  // Start each software serial port
  portOne.begin(9600);
  portTwo.begin(9600);
}

void loop() {
//---
  portOne.listen();
  Serial.println("Data from port one:");
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (portOne.available() > 0) {
    char inByte = portOne.read();
    Serial.write(inByte);
  }

  Serial.println();
//---
  portTwo.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  Serial.println("Data from port two:");
  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
  }

  Serial.println();
}
```
- [SoftwareSerial](https://www.arduino.cc/en/Reference/SoftwareSerial)
-- [SoftwareSerialExample](https://www.arduino.cc/en/Tutorial/SoftwareSerialExample)
-- [TwoPortReceive](https://www.arduino.cc/en/Tutorial/TwoPortReceive)
- 中文
-- [软件模拟串口通信](https://www.arduino.cn/thread-47262-1-1.html)
-- [軟體串列埠](http://yhhuang1966.blogspot.com/2015/09/arduino.html)


# 2 //--- Timer
---
- `Timer ex1`
```c=
#include "Timer.h"
Timer t;
Timer t2;
Timer t3;

void setup(){
  Serial.begin(9600);
  
  int tickEvent = t.every(100, doSomething);
  Serial.print("s1");
  Serial.println(tickEvent);
  
  tickEvent = t2.every(100, doSomething2);
  Serial.print("s2");
  Serial.println(tickEvent);
  
  tickEvent = t3.every(100, doSomething3);
  Serial.print("s3");
  Serial.println(tickEvent);
}

void loop(){
  t.update();
  t2.update();
  t3.update();
}

void doSomething(){
  Serial.println("ds1");
}

void doSomething2(){
  Serial.println("ds2");
}

void doSomething3(){
  Serial.println("ds3");
}
```
- `Timer ex2`
```c=
#include "Timer.h"
Timer t;
int ledEvent;

void setup(){
  Serial.begin(9600);
  int tickEvent = t.every(2000, doSomething);
  Serial.print("2 second tick started id=");
  Serial.println(tickEvent);
  
  pinMode(13, OUTPUT);
  ledEvent = t.oscillate(13, 50, HIGH);
  Serial.print("LED event started id=");
  Serial.println(ledEvent);
  
  int afterEvent = t.after(10000, doAfter);
  Serial.print("After event started id=");
  Serial.println(afterEvent); 
}

void loop(){
  t.update();
}

void doSomething(){
  Serial.print("2 second tick: millis()=");
  Serial.println(millis());
}

void doAfter(){
  Serial.println("stop the led event");
  t.stop(ledEvent);
  t.oscillate(13, 500, HIGH, 5);
}
```
- [Arduino 入門教學：Timer 使用](https://lolikitty.pixnet.net/blog/post/168715383-arduino-%E5%85%A5%E9%96%80%E6%95%99%E5%AD%B8%EF%BC%9Atimer-%E4%BD%BF%E7%94%A8)
- [Arduino一個好用的計時器程式庫](http://yehnan.blogspot.com/2012/03/arduino.html)
- [arduino Timer 中文API解釋](http://tnuazyy.blogspot.com/2014/02/arduinotimer.html)
- [Arduino Timer Library](http://www.doctormonk.com/2012/01/arduino-timer-library.html)
- [JChristensen/Timer](https://github.com/JChristensen/Timer#installation)

## 2-1 Timer+function =>multithreading
## 2-2 Timer+SoftwareSerial =>multi Serial
## 2-3 Timer+GPIO =>Software外部中斷
## 2-4 其他(我沒用過)達到相同效果的 Timer lib
- [#include <SimpleTimer.h>](https://playground.arduino.cc/Code/SimpleTimer/#Usage)
- [#include "timer.h"
#include "timerManager.h"](https://github.com/brunocalou/Timer)


# 3 //--- 多設備&任務
---
- I2C ? SPI ?
- [RTOS](https://www.hackster.io/feilipu/using-freertos-multi-tasking-in-arduino-ebc3cc)

# 4 //--- 程式記憶體寫爆?
---
## 4-1 程式優化
- [F()](https://vmaker.tw/archives/13258)
- PROGMEM
-- [Arduino：關於記憶體之二三事](http://yehnan.blogspot.com/2013/08/arduino.html)
-- [Arduino 淺談記憶體](http://pizgchen.blogspot.com/2017/03/arduino-1.html)

## 4-2 EEPROM
### 內部 EEPROM
- [可讀寫任何資料的 EEPROM 函式](http://coopermaa2nd.blogspot.com/2011/03/eeprom.html)
- [自訂整組資料讀寫EEPROM](http://yehnan.blogspot.com/2014/03/arduinoeeprom.html)

### 外接 EEPROM
- [Arduino I2C 超頻 & I2C 外部擴充 EEPROM](http://blog.udn.com/awei0905/28767312)
- [AT24C256 EEPROM 儲存模組/記憶體模組 Arduino 擴充儲存記憶體專用](
https://www.taiwaniot.com.tw/product/at24c256-eeprom-%E5%84%B2%E5%AD%98%E6%A8%A1%E7%B5%84%E8%A8%98%E6%86%B6%E9%AB%94%E6%A8%A1%E7%B5%84-arduino-%E6%93%B4%E5%85%85%E5%84%B2%E5%AD%98%E8%A8%98%E6%86%B6%E9%AB%94%E5%B0%88%E7%94%A8/)

# 5 //--- Arduino Bootloader
---
- [將Bootloader寫進ATMega晶片](http://gcyrobot.blogspot.com/2011/06/diy-arduino-bootloaderatmega3.html)
- [Arduino bootloader 燒錄](https://iammic.pixnet.net/blog/post/63686476-arduino-bootloader-%E7%87%92%E9%8C%84)
- [ATmega328P 燒錄 Bootloader ](http://wyj-learning.blogspot.com/2018/11/arduino-08-atmega328p-bootloader.html)
- [自製 Arduino](http://www.powenko.com/wordpress/3-%E8%87%AA%E8%A3%BD-arduino-%E6%9D%BF%E5%AD%90/)
