顯示具有 mega2560 標籤的文章。 顯示所有文章
顯示具有 mega2560 標籤的文章。 顯示所有文章

2026年10月19日 星期一

[DIY專案] 4x4x4 LED 立方體:用 Arduino Mega2560 + 麥克風 + 光感做出「會呼吸的音箱」

這一個 DIY 專案是「4×4×4 64 顆 LED 的互動式立方體」:用 Arduino Mega2560 做控制核心,外接麥克風模組與光感(LDR),讓立體光譜「聽得到、看得見」——音量越大,底層越亮;環境越暗,頂層抬頭;中層隨時間流動,像一盂會呼吸、會應答的水晶球。


為什麼要「多工」而不能直接 64 腳

64 顆 LED 若各自一個 IO,需要 64 腳——任何 Arduino 都給不起。解法是矩陣掃描(multiplexing):陣列在製作時就把 16 顆 LED 的同一面焊在一起(陽極共接=「行」),另一面也共接(陰極共接=「列」)。任一瞬間只有「1 行 × 1 列」交叉的那一顆 LED 有電流通過;但把這 16×16=256 個交點每秒掃幾百遍,人眼殘像就讀成「64 顆同時亮」——和螢幕的「逐行刷新」是同一个道理。

本專案用 Mega2560 剛好:16 行 + 16 列 = 32 個數位腳位,一個不少。


料件清單

料件 數量/規格 說明
4x4x4 LED 陣列 64 顆 3mm LED(本例清/藍)、16pin+16pin 排針 陣列陽極共接成 16 行、陰極共接成 16 列
Arduino Mega2560 1 片 32 個數位腳位剛好吃滿:16 行 + 16 列
聲音感測器(咪頭模組) 1 個(電子元件店的藍板電容咪) 接 A0,模擬量随聲音大小跳動
光感 sensor(LDR) 1 個(10kΩ 光電分壓) 接 A1,亮度 → 電阻 → 模擬值
ULN2003(或 P-MOS 陣列) 1~2 片 列(陰極)需要大電流拉低,IO 直接拉不動 64 顆
電阻 1kΩ × 16 行(陽極)限流,每行串一顆
杜邦線 / 麵包板或 PCB — 引腳 32+2,麵包板要够大
5V 供電 3A 以上 64 顆全亮峰值 ≈ 1.5~2A,USB 口未必穩,建議獨立 5V

接線總覽

接線總覽

物件 目標
陣列 16 行(陽極) 各串 1kΩ → Mega D0~D15
陣列 16 列(陰極) → ULN2003 輸入 → 輸出 → Mega D16~D31
咪頭模組 VCC/GND/OUT 5V / GND / A0
LDR 分壓(10kΩ 下拉) 節點 → A1
GND 全部共地(最重要,浮地 = 亂亮)

控制端程式(重點:掃描)

核心邏輯三件事:(1) 感測 每 30ms 讀一次咪頭/光感,把「音量→底 2 層,暗→頂層」寫進 cube[4][4][4] 模型;(2) 掃描 每幀只點亮「一行」該亮的列,16 次輪巡;(3) 幀間隔 120µs 左右,64 顆全亮的亮度 ≈ 實際的 1/16,所以限流電阻要比一般直驅稍小,或接受稍暗——這是多工的公定代價:

/*
 * 4x4x4 LED 立方體 — 聲音+光度互動版
 * 硬體: Arduino Mega2560
 *   16 行(陽極) → D0..D15   (經 1kΩ x16 限流)
 *   16 列(陰極) → D16..D031 (經 ULN2003 拉低)
 *   麦克风模块 AIN  → A0  (模擬)
 *   光感 (LDR 分壓) → A1
 */

const uint8_t ROWS = 16;
const uint8_t COLS = 16;
const uint16_t ROW_PIN[16]  = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
const uint16_t COL_PIN[16]  = {16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

// cube[x][y][z] → 行 = y*4+z (層內 z 從下到上), 列 = x*4+y
bool cube[4][4][4] = {0};

void setup() {
  for (uint8_t i = 0; i < 16; i++) {
    pinMode(ROW_PIN[i], OUTPUT);
    pinMode(COL_PIN[i], OUTPUT);
  }
  for (uint8_t i = 0; i < 16; i++) { digitalWrite(ROW_PIN[i], HIGH); digitalWrite(COL_PIN[i], HIGH); }
  Serial.begin(115200);
}

uint16_t scanRow = 0;

void loop() {
  /* ---------- 1. 讀感測 → 更新 cube ---------- */
  static uint32_t lastSense = 0;
  uint32_t now = millis();
  if (now - lastSense > 30) {          // 30ms 感測一次
    lastSense = now;

    int16_t mic  = analogReadA0avg(20);   // 平均20次降噪
    int16_t light= analogReadA1avg(20);
    uint16_t micDB   = map(mic,   200, 800, 0, 1023);  // 依你的音量範圍校正
    uint16_t darkPct = map(light, 1023, 0, 0, 1023);   // 越暗越大

    /* 規則: 底層=音量, 頂層=光線, 中層=呼吸 */
    uint8_t vol  = (uint8_t)(micDB   / 1024.0 * 4);      // 0..3(音量4層)
    uint8_t lig  = (uint8_t)(darkPct/ 1024.0 * 4);       // 0..3
    uint32_t t = now / 200;                                // 呼吸節拍
    for (uint8_t x = 0; x < 4; x++)
      for (uint8_t y = 0; y < 4; y++)
        for (uint8_t z = 0; z < 4; z++) {
          if      (z < 2) cube[x][y][z] = (z < vol);                          // 音量填底2層
          else if (z == 3) cube[x][y][z] = (lig <= 1);                        // 暗 → 頂層亮
          else            cube[x][y][z] = ((t + x + y) % 4 == 0);             // 中層流動
        }
  }

  /* ---------- 2. 掃描:一次一行,快掃讓人眼殘像為「64 顆同時亮」 ---------- */
  for (uint8_t i = 0; i < 16; i++) digitalWrite(ROW_PIN[i], HIGH);   // 全熄
  uint8_t r = scanRow;
  scanRow = (scanRow + 1) & 0b1111;

  uint8_t y = r >> 2, z = r & 3;
  digitalWrite(ROW_PIN[r], LOW);                          // 選行 (common-anode: 拉低 = 供電)
  for (uint8_t c = 0; c < 16; c++) {
    uint8_t x = c >> 2, y2 = c & 3;
    bool on = (y2 == y) && cube[x][y][z];
    digitalWrite(COL_PIN[c], on ? HIGH : LOW);            // 拉高 = 通電
  }
  delayMicroseconds(120);                                  // 16x120us ≈ 1.9ms/幀 → ~500 fps
}

/* 20 次類比取平均, 擋住模組的抖動 */
int16_t analogReadA0avg(uint8_t n){ long s=0; for(uint8_t i=0;i<n;i++){s+=analogRead(A0); delayMicroseconds(60);} return s/n; }
int16_t analogReadA1avg(uint8_t n){ long s=0; for(uint8_t i=0;i<n;i++){s+=analogRead(A1); delayMicroseconds(60);} return s/n; }

調試時最常遇到的三個坑

  • 浮地=亂亮:陣列、ULN2003、感測模組的 GND 沒有共地,會看到「隨機一兩顆閃」——不是程式錯,是地線錯。共地是最優先的一條線。
  • 電流不夠,亮一半:USB 直供的 500mA 撐不住 64 顆峰值,會看到「上半截亮、下半截暗」或 Mega 重新開機。獨立 5V/3A 一接就好。
  • 掃描太慢,閃爍肉眼可見:16 幀總時間 > 50ms 就会看到「一行一行跑」。把 delayMicroseconds 收到 80~150µs 區間,或降低亮層數。

玩法建議(感測→效果)

  • 音量柱:底層亮 = 音量高;拍手/說話 = 立方體「長高」,最直觀的音頻視覺化。
  • 暗室模式:LDR 越暗,頂層越亮 + 掃描頻率越慢,變成一盂「夜燈」;手遮光 → 瞬間熄滅。
  • 風暴模式:音量 > 閾值 → 随机閃爍 2 秒(類似警報燈),閾值在 map() 裡調。
  • 呼吸模式:中層随 sin(t) 慢速起伏,不用感測也好看,做「常亮桌面小物」用。

結語

這個專案把「多工、電流、時序」三個電子學核心一次走完:64 顆 LED 靠「快」而不是「多」跑起來——人眼看不見的是 500fps 的掃描,感覺到的是 64 顆同時亮。做完它,LED 矩陣、OLED、甚至 VGA framebuffer 的「逐行刷新」就全部通了。

2023年12月6日 星期三

雷達產品的CAN Bus測試

Purpose:
CAN Bus test of radar products
在雷達產品的測試上, 為了驗證產品上的CAN Bus Function是否正常. 因此在測試的環境中, 用最便宜跟能夠搭配產測程式的方案(Mega2560+MCP2515+RelayBoard).
並且利用ESP32+MCP2515 module和 PCAN-USB-FD去做驗證.
In the test of radar products, in order to verify whether the CAN Bus Function on the product is normal. Therefore, in the test environment, the cheapest solution that can be matched with the production test program (Mega2560+MCP2515+RelayBoard) is used.
And use ESP32+MCP2515 module and PCAN-USB-FD for verification.
Architectures:


Fundamental:
PCAN-USB-FD
The PCAN-USB FD interface enables simple connection to CAN FD and CAN networks. Its compact plastic casing makes it suitable for mobile applications. A galvanic isolation of up to 500 Volts decouples the PC from the CAN bus.


Reference the previous article link on blog:
ESP32 + MCP2515 use CanHacker on CAN Bus system

YouTube Demo:




2023年10月29日 星期日

Arduino Mega2560 Test Fixture Application

Purpose:

1. 兩個按鍵作為進出平台的開關 另一按鍵為平台上另一上下動作的開關
2. 完成所有動作後按任一進出按鍵就進行退出動作在退出時 蜂鳴器要發聲
3. 完成進入動作 COUNTER 要記數加一次為了控制頂針壓合次數
4. 手動跟程式控制同時可以動作

1. Two buttons serve as switches for entering and exiting the platform. The other button serves as another switch for up and down movements on the platform.
2. After completing all actions, press any entry or exit button to exit. The buzzer will sound when exiting.
3. After completing the entry action COUNTER, the count needs to be added once in order to control the number of times the ejection pin is pressed.
4. Manual and program control can be performed at the same time

Architecture:

零件配置:
1. Arduino Mega2560 控制板

2. 8 Port Relay board
3. 3 個 push button
4. 3個氣壓控制閥
5. 1個蜂鳴器
6. 次數記數器
7. 4個氣壓缸
8. ACto12VDC變壓器
YouTube Demo;
Mega2560 Code:
#include <Arduino_FreeRTOS.h>
//---------------structure ----------------------------------------
//--------- Flag structure --------------------------------------
typedef struct _vFlag
{
  uint8_t LEDFlag=1;
  uint8_t BTFlag = 0;
  uint8_t ServoFlag = 0;
  uint8_t CloseFlag=0;
  uint8_t InFlag=0;
  uint8_t OutFlag=0;
  uint8_t UpFlag=0;
  uint8_t Up1Flag=0;
  uint8_t Up2Flag=0;
  uint8_t Buzzer_Flag=0;
 
} vFlag;
vFlag *flag_Ptr;
vFlag flag;
//----------uart--------------
#define LINE_BUFFER_LENGTH 64
//--------- uart structure --------------------------------------
typedef struct _vUart
{
  char c;
  int lineIndex = 0;
  int line1Index = 0;
  int BTlineIndex = 0;
  bool lineIsComment;
  bool lineSemiColon;
  char line[128];
  char BTline[20];
  String inputString;
  String BTinputString;
  String S1inputString;
  int V[16];
  char ctemp[30];
  char I2C_Data[80];
  int DC_Spped = 50;
  float Voltage[16];
  int Buffer[128];
  int StartCnt = 0;
  int ReadCnt = 0;
  int sensorValue = 0;
} vUart;
vUart *Uart_Ptr;
vUart Uart;
//------------------------------------------------
const uint8_t LED_PIN = 13;
const uint8_t F_PIN = 12;
int buzzerPin=11;
//----relay--------
uint8_t DIO_1 = 22;
uint8_t DIO_2 = 24;
uint8_t DIO_3 = 26;
uint8_t DIO_4 = 28;
uint8_t DIO_5 = 30;
uint8_t DIO_6 = 32;
uint8_t DIO_7 = 34;
uint8_t DIO_8 = 36;
//---------治具定義------------------------------------------
uint8_t In1_B_Pin = 47;  
uint8_t In2_B_Pin = 49;
uint8_t Up_B_Pin = 51;

uint8_t In_S_Pin = 23;
uint8_t Out_S_Pin = 25;
uint8_t UpDown1_S_Pin = 27;
uint8_t UpDown2_S_Pin = 29;
//----relay--------
uint8_t InOut_V_Pin = 22;
uint8_t UpDown1_V_Pin = 24;
uint8_t UpDown2_V_Pin = 26;

uint8_t SPK_O_Pin = 28;  //30預留 12V relay
//uint8_t Count_O_Pin = 30;
uint8_t Count_O_Pin = 32;
//----------------------------------------------------
uint8_t DUTPin = 23;
uint8_t Sensor1Pin = 23;
uint8_t Sensor2Pin = 25;
uint8_t Sensor3Pin = 27;
uint8_t Sensor4Pin = 29;
//-------------------------------------------
char ctemp[20];
//------------------------------
TaskHandle_t hled;
TaskHandle_t huart0;
//------------------------------------------------------------------------------
void initial()
{
  Serial.println(F("Create Task"));
  //----------------------------------------------------------------------
  // create UART task
  xTaskCreate(vUARTTask, "UART Task", configMINIMAL_STACK_SIZE, NULL, 1, &huart0);
  // Check the results
  // create blink task
  xTaskCreate(vLEDFlashTask, "LED Task", configMINIMAL_STACK_SIZE, NULL, 2, &hled);
 
  //-------------------------------------------------------------------

}
void setup()
{
  Uart.inputString.reserve(60);
  initial();
  Serial.begin(9600);
  Serial.setTimeout(2000);
  Serial.println(F("init"));
  //-------------------------------------
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); // Turn LED off.
  //---------------IO setting-----------------
  pinMode(DIO_1, INPUT);
  pinMode(DIO_2, INPUT);
  pinMode(DIO_3, INPUT);
  pinMode(DIO_4, INPUT);
  pinMode(DIO_5, INPUT);
  pinMode(DIO_6, INPUT);
  pinMode(DIO_7, INPUT);
  pinMode(DIO_8, INPUT);
  pinMode(Sensor1Pin,INPUT_PULLUP); //23
  pinMode(Sensor2Pin,INPUT_PULLUP); //25
  pinMode(Sensor3Pin,INPUT_PULLUP); //27
  pinMode(Sensor4Pin,INPUT_PULLUP); //29
  //----治具 ----------------
  pinMode(In_S_Pin,INPUT_PULLUP); //23
  pinMode(Out_S_Pin,INPUT_PULLUP); //25
  pinMode(UpDown1_S_Pin,INPUT_PULLUP); //27
  pinMode(UpDown2_S_Pin,INPUT_PULLUP); //29
  pinMode(In1_B_Pin,INPUT_PULLUP); //47
  pinMode(In2_B_Pin,INPUT_PULLUP); //49
  pinMode(Up_B_Pin,INPUT_PULLUP); //51
  //----relay--------
  pinMode(InOut_V_Pin, INPUT);   //22
  pinMode(UpDown1_V_Pin, INPUT); //24
  pinMode(UpDown2_V_Pin, INPUT); //26
  pinMode(SPK_O_Pin, INPUT);     //28 //30預留 12V relay
  pinMode(Count_O_Pin, INPUT);   //32
 // start FreeRTOS
  Serial.println("Systom On!");
  vTaskStartScheduler();
  //if the scheduler start the code don't came here
  Serial.println(F("Die"));
  while (1)
    ;
}
//------------------------------------------------------------------------------
void loop()
{
 
}
//------------------------------------------------------------------------------
// high priority for blinking LED
void vLEDFlashTask(void *pvParameters)
{
  (void)pvParameters;

  pinMode(LED_PIN, OUTPUT);
  for (;;)
  {
    digitalWrite(LED_PIN, HIGH);                    // Turn LED on.
    vTaskDelay((150L * configTICK_RATE_HZ) / 1000L); // Sleep for 50 milliseconds.
    digitalWrite(LED_PIN, LOW);                      // Turn LED off.
    vTaskDelay((150L * configTICK_RATE_HZ) / 1000L); // Sleep for 150 milliseconds.
  }
}
//------------------------------------------------------------------------------
void vUARTTask(void *pvParameters)
{
  Uart.lineIsComment = false;
  Uart.lineSemiColon = false;

  while(1)
  {
//---------Fixture -------------------------
    vFixtureTask();   
while (Serial.available() > 0)
    {
      Uart.c = Serial.read();

      if ((Uart.c == '\n') || (Uart.c == '\r'))
      { // End of line reached
        if (Uart.lineIndex > 0)
        { // Line is complete. Then execute!
          Uart.line[Uart.lineIndex] = '\0'; // Terminate string
          //Serial.println( F("Debug") );
          //Serial.println( Uart.inputString );
          processCommand(Uart.line); // do something with the command
          //傳輸給藍芽
         
          Uart.lineIndex = 0;
          Uart.inputString = "";
        }
        else
        {
          // Empty or comment line. Skip block.
        }
        Uart.lineIsComment = false;
        Uart.lineSemiColon = false;
        Serial.println(F("ok>"));
      }
      else
      {
        //Serial.println( c );
        if ((Uart.lineIsComment) || (Uart.lineSemiColon))
        {
          if (Uart.c == ')')
            Uart.lineIsComment = false; // End of comment. Resume line.
        }
        else
        {
          if (Uart.c == '/')
          { // Block delete not supported. Ignore character.
          }
          else if (Uart.c == '~')
          { // Enable comments flag and ignore all characters until ')' or EOL.
            Uart.lineIsComment = true;
          }
          else if (Uart.c == ';')
          {
            Uart.lineSemiColon = true;
          }
          else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
          {
            Serial.println("ERROR - lineBuffer overflow");
            Uart.lineIsComment = false;
            Uart.lineSemiColon = false;
          }
          else if (Uart.c >= 'a' && Uart.c <= 'z')
          { // Upcase lowercase
            Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += (char)(Uart.c - 'a' + 'A');
          }
          else
          {
            Uart.line[Uart.lineIndex] = Uart.c;
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += Uart.c;
          }
        }
      }
    } //while (Serial.available() > 0)
  }
}
//------------------------------------------------------------------------------
//----reset-----------
void (*resetFunc)(void) = 0;
//---------------------------------------------------------
void processCommand(char *data)
{

  int len, xlen, ylen, zlen, alen;
  int tempDIO;
  String stemp;
  unsigned int i, j, Comma;

  int val, maxv, minv;
  unsigned long duration;
  float Vpp;

  len = Uart.inputString.length();

  //-------------RESET---------------
  if (strstr(data, "VER") != NULL)
  {
    //Serial.println(F("W_ATE_Board_20201021"));
    Serial.println(F("Mega_SERIAL_20230723"));
  }
  if (strstr(data, "RESET") != NULL)
  {
    Serial.println(F("Reset"));
    resetFunc();
  }
  //===-----------------DIO port 1----------------
  if (strstr(data, "DIO1") != NULL)
  {
    //-DIO11_LOW
    if (data[3] == '1')
    {
      for (int i = 0; i < len; i++)
      {
        if (data[i] == '_')
        {
          //Serial.println("test");
          //Serial.println(i);
          xlen = i;
        }
        //ctemp[i-4]=data[i];
      }
      for (int i = 4; i < xlen; i++)
      {
        ctemp[i - 4] = data[i];
      }
      ctemp[xlen - 4] = '\0';
      tempDIO = atoi(ctemp);
      //Serial.println(tempDIO);
      for (int i = (xlen + 1); i < len; i++)
      {
        ctemp[i - (xlen + 1)] = data[i];
      }
      ctemp[len - (xlen + 1)] = '\0';

      tempDIO = tempDIO * 2 + 20;
      pinMode(tempDIO, OUTPUT);
      //if(strstr(ctemp, "ON") != NULL)
      //if(strcmp(ctemp, "ON")==0)
      //if (strcmp(Uart.ctemp, "LOW") == 0)
      if (strstr(ctemp, "LOW") != NULL)
      {
        digitalWrite(tempDIO, LOW);
      }
      else if (strstr(ctemp, "ON") != NULL)
      {
        digitalWrite(tempDIO, LOW);
      }
      else if (strstr(ctemp, "HIGH") != NULL)
      {
        digitalWrite(tempDIO, HIGH);
      }
      else if (strstr(ctemp, "OFF") != NULL)
      {
        digitalWrite(tempDIO, HIGH);
      }
    }
  }
  //------fixture --------
  if(strstr(data, "IN") != NULL )
  {
     if( (digitalRead(Out_S_Pin) == LOW) && flag.Up1Flag==0 && flag.Up2Flag==0)
     {
       flag.InFlag=3;
       pinMode(InOut_V_Pin , OUTPUT);
       digitalWrite(InOut_V_Pin , HIGH);    //in進
     }
     else
     {
       Serial.println(F("PLS OUT Fixture first!!"));
     }
  }
 
  if(strstr(data, "OUT") != NULL)
  {
     if( (digitalRead(In_S_Pin) == LOW) && flag.Up1Flag==0 && flag.Up2Flag==0)
     {
       flag.InFlag=5;
       pinMode(InOut_V_Pin , OUTPUT);
       digitalWrite(InOut_V_Pin , LOW); //出
     }
     else
     {
      Serial.println(F("PLS IN Fixture first!!"));
     }
  }
 
  if(strstr(data, "UP1") != NULL)
  {
     //if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==1 && flag.Up2Flag==0 )
     if((flag.InFlag==2 || flag.InFlag==3) && (digitalRead(UpDown1_S_Pin ) == LOW) && flag.Up2Flag==0 )
     {
      pinMode(UpDown1_V_Pin , OUTPUT);
      digitalWrite(UpDown1_V_Pin , HIGH); //UP1
      flag.Up1Flag=0;
     }
     else
     {
      Serial.println(F("PLS DWON1 Fixture first!!"));
     }
  }

  if(strstr(data, "DOWN1") != NULL)
  {
     if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==0 && flag.Up2Flag==0 )
     {
      pinMode(UpDown1_V_Pin , OUTPUT);
      digitalWrite(UpDown1_V_Pin , LOW); //UP1
      flag.Up1Flag=1;
     }
     else
     {
      Serial.println(F("PLS UP1 Fixture first!!"));
     }
  }

  if(strstr(data, "UP2") != NULL)
  {
     if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==1 )
     {
       //Serial.println("flag.InFlag"+String(flag.InFlag));
       //Serial.println("flag.Up1Flag"+String(flag.Up1Flag));
       //Serial.println("flag.Up2Flag"+String(flag.Up2Flag));
       
       if((digitalRead(UpDown2_S_Pin) == LOW))
       {
         pinMode(UpDown2_V_Pin , OUTPUT);
         digitalWrite(UpDown2_V_Pin , HIGH); //UP1
         flag.Up2Flag=0;
       }
       
     }
     else
     {
       Serial.println(F("PLS DWON2 Fixture first!!"));
     }
  }

  if(strstr(data, "DOWN2") != NULL)
  {
     //Serial.println("flag.InFlag"+String(flag.InFlag));
     //Serial.println("flag.Up1Flag"+String(flag.Up1Flag));
     //Serial.println("flag.Up2Flag"+String(flag.Up2Flag));
     
     if((flag.InFlag==2 || flag.InFlag==3) && flag.Up1Flag==1 && flag.Up2Flag==0 )
     {
       pinMode(UpDown2_V_Pin , OUTPUT);
       digitalWrite(UpDown2_V_Pin , LOW); //UP1
       flag.Up2Flag=1;
       pinMode(Count_O_Pin , OUTPUT);
       digitalWrite(Count_O_Pin  , LOW);  
       delay(500);
       digitalWrite(Count_O_Pin  , HIGH);
     }
     else
     {
       Serial.println(F("PLS UP2 Fixture first!!"));
     }
  }
  //-----------fixture end---------------------------------------------
}
//------ vFixtureTask start --------------------------------
void vFixtureTask()
{
 
  if( (digitalRead(In1_B_Pin) == LOW && digitalRead(In2_B_Pin) == LOW ) && (flag.InFlag==5 || flag.InFlag==1 || flag.InFlag==9) )
  {
    flag.InFlag=1;
    pinMode(InOut_V_Pin , OUTPUT);
    digitalWrite(InOut_V_Pin  , HIGH);   //In 因為氣壓跟之前反接 所以 HIGH進

    if(digitalRead(In_S_Pin) == LOW)  
    {
      flag.InFlag=2;
      //Serial.println(flag.InFlag);
      //Serial.println(F("ok>"));
    }
  }

  if( (digitalRead(In_S_Pin) == LOW ) && ( flag.InFlag==2 ) )
  {
    pinMode(UpDown1_V_Pin, OUTPUT);
    digitalWrite(UpDown1_V_Pin , LOW);   //DOWN
    if(digitalRead(UpDown1_S_Pin) == LOW)  
    {
      flag.Up1Flag = 1;  //1在下面 0在上面
      //Serial.println(F("UpDown1_S_Pin"));
      //Serial.println(F("ok>"));
      pinMode(UpDown2_V_Pin, OUTPUT);
      digitalWrite(UpDown2_V_Pin , LOW);   //DOWN
      if(digitalRead(UpDown2_S_Pin) == LOW)  
      {
        flag.Up2Flag = 1;
        //Serial.println(F("UpDown2_S_Pin"));
        //Serial.println(F("ok>"));
        pinMode(Count_O_Pin , OUTPUT);
        digitalWrite(Count_O_Pin  , LOW);  
        delay(1000);
        flag.InFlag = 3;
        //Serial.println(flag.InFlag);
        //Serial.println(F("ok>"));
        digitalWrite(Count_O_Pin  , HIGH);
        //flag.OutFlag = 0;          
      }
    }
  }
  //if( (digitalRead(In1_B_Pin) == HIGH || digitalRead(In2_B_Pin) == HIGH ) && (flag.InFlag==0 || flag.InFlag==1 ) )
  if( (digitalRead(In1_B_Pin) == HIGH || digitalRead(In2_B_Pin) == HIGH ) && ( flag.InFlag == 1 ) )
  {
    //flag.InFlag=8;
    //Serial.println(flag.InFlag);
    //Serial.println(F("ok>"));
   
    pinMode(SPK_O_Pin , OUTPUT);
    digitalWrite(SPK_O_Pin , LOW);   //SPK
   

    pinMode(UpDown2_V_Pin, OUTPUT);
    digitalWrite(UpDown2_V_Pin, HIGH);   //上
    delay(300);
    pinMode(UpDown1_V_Pin, OUTPUT);
    digitalWrite(UpDown1_V_Pin , HIGH);   //上
    delay(300);
    pinMode(InOut_V_Pin , OUTPUT);
    digitalWrite(InOut_V_Pin  , LOW);   //出
   
    digitalWrite(SPK_O_Pin , HIGH);   //SPK

    if(digitalRead(Out_S_Pin) == LOW)  
    {
      digitalWrite(SPK_O_Pin , HIGH);   //SPK
      flag.InFlag=9;
      flag.Up1Flag = 0;
      flag.Up2Flag = 0;
    }
  }

  if( (digitalRead(In1_B_Pin) == LOW || digitalRead(In2_B_Pin) == LOW ) && (digitalRead(In_S_Pin) == LOW && flag.InFlag==0) )
  {
    //Serial.println(F("Out_S_Pin"));
    //Serial.println(F("ok>"));
    flag.InFlag=5;
    pinMode(InOut_V_Pin , OUTPUT);
    digitalWrite(InOut_V_Pin  , LOW);   //出
  }

  //if( (digitalRead(In1_B_Pin) == LOW || digitalRead(In2_B_Pin) == LOW ) && (flag.InFlag==2 || flag.InFlag==3) )
  if( (digitalRead(In1_B_Pin) == LOW || digitalRead(In2_B_Pin) == LOW ) && (flag.InFlag==3) )
  {
    flag.InFlag=1;
  }

  if( (digitalRead(Up_B_Pin ) == LOW ) && flag.Up2Flag == 1  && flag.Up1Flag == 1)
  {
    pinMode(UpDown2_V_Pin, OUTPUT);
    digitalWrite(UpDown2_V_Pin, HIGH);   //上
    delay(300);
    flag.Up2Flag=0;
  }
  else if( (digitalRead(Up_B_Pin ) == LOW )  && flag.Up2Flag == 0 && flag.Up1Flag == 1)
  {
    pinMode(UpDown2_V_Pin, OUTPUT);
    digitalWrite(UpDown2_V_Pin, LOW);   //下
    if(digitalRead(UpDown2_S_Pin) == LOW)  
    {
        flag.Up2Flag=1;
    }
  }
}
//-------------------------------------------



2023年10月27日 星期五

Electrical Safety Compliance Automatic Test System

Purpose:

(Automation for Electrical Safety & Compliance Testing)

安規綜合分析儀7440-自動測試架構, 適合工廠端量產測試實現量化生產減少人工插拔的自動化生產流程, 安排在組裝後走流線測試更顯效益!

Electrical Safety Compliance Analyzer 7440 Automatic Test Architecture, It is suitable for factory-side mass production testing to achieve quantitative production and reduce the automated production process of manual plugging and unplugging. It is more efficient to arrange streamline testing after assembly!

Finished product picture

Detailed architecture

Portion architecture


Fundamental:

(一)儀器介紹

EXTECH7440是台灣華儀公司生產的, 具有交直流耐壓測試, 絕緣電阻測試, 接地電阻測試四功能合一的安規分析儀, 可以程控設置, 並配有RS232和GPIB接口, 可實現自動測試.
EXTECH7440 is produced by Taiwan Extech Electronics Ltd.,Co. Company. It is a safety analyzer with four functions in one: AC and DC withstand voltage testing, insulation resistance testing, and grounding resistance testing. It can be programmed and set up, and is equipped with RS232 and GPIB interfaces to enable automatic testing.

Hipot and Gound Test (耐壓、接地測試)
1. 交流耐壓測試(AC, AC Hipot):適合吃插座電的器具
2. 直流耐壓測試(DC, DC Hipot):適合電池或電容等吃直流電的
3. 絕緣阻抗測試(IR, Insulation Resistance):適合不能太高壓但想知道絕緣能力的,或已經測完高壓想再測絕緣的
4. 接地阻抗測試(GB, Ground Bond):適合測試接地較大的金屬件
5. 接地導通測試(Ground Continuity):適合測試接地較小或細的金屬件
6. 洩漏電流測試(Touch Current):適合人體會時常接觸到的部件,如醫療器材

FRONT PANEL OF MODEL 7440

之前相關文章連結(Links to previous related articles) :

2023年7月29日 星期六

ESP32 communicates with MEGA2560 through uart serial port

Purpose:

Serial data communication is a very popular communication protocol among different data communication. Data communication between ESP32 and Arduino can be done by serial communication. Most microcontrollers can transmit and receive data between them using UART communication.

In this project I have read some data from Arduino Mega TX1 and RX1 from ESP32 pin17 TX2 and pin16 RX2 serially using UART communication protocol.

Fundamental:

Serial communication

Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART), and some have several.

Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.

You can use the Arduino environment’s built-in serial monitor to communicate with an Arduino board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to begin().

In every Arduino board, there is at least one serial pin available. The pins are named TX and RX pins. TX stands for transmitting; RX stands for receive. 

In Arduino Mega, there are additional three serial pins. That means there are four serial communication pins available in Arduino Mega.

ESP32 Serial pins:

ESP32 boards also come with TX and RX pins. Depending on the model, the number of serial pins may vary.

Microcontroller boards like the "Arduino Mega" rely on the ATmega2560 microcontroller. It includes 54 digital input/output pins, of which 16 are analog inputs, 14 are used as PWM outputs, hardware serial port (UART) - 4, crystal oscillator - 16MHz, one ICSP header, one power supply jack, a USB connection, and the RST button. This board basically contains everything needed to support a microcontroller. Therefore, the board can be powered by connecting it to a PC via a USB cable, battery, or AC-DC adapter. By installing a backplane, the board can be protected from accidental discharges.

The SCL and SDA pins of the Mega 2560 R3 board are connected next to the AREF pin. Also, the two newest pins are located near the RST pin. One pin is IOREF, which allows the shield to set the voltage supplied by the Arduino board. The other pin is unassigned and will be reserved for future use. These boards can be used with any existing shield, but can accommodate the latest shields that use these extra pins.

圖一: Mega2560 pin definition
圖二:Mega2560 Specification
圖三: ESP32 功能方塊圖
圖四: ESP32 Pin Definition

Circuit:

圖五: 原理圖

圖六: 線路圖

Code Introduce:

ESP32

//--------- Flag structure --------------------------------------
typedef struct _vFlag
{
  uint8_t BTFlag = 0;
  uint8_t DC_Flag = 0;
  uint8_t CANFlag = 0;
  uint8_t I2C_Flag = 0;
  uint8_t BMP180Flag = 0;
  uint8_t JSONFlag = 0;
  uint8_t Radar_L_Flag = 0;
  uint8_t Radar_R_Flag = 0;
  uint8_t sensor_Flag = 0;
  uint8_t sensor1_Flag = 0;
  uint8_t initial_Flag = 0;
  uint8_t Tone_Flag = -1;
  uint8_t IR_RECV_Flag=0;
  uint8_t IR_SEND_Flag=0;
  uint8_t FunctionFlag = 3;
  uint8_t SendFlag = 0;
  uint8_t BMPCnt = 0;
} vFlag;
vFlag *flag_Ptr;
vFlag flag;
//----------uart--------------
#define RXD2 16
#define TXD2 17

#define LINE_BUFFER_LENGTH 128
//--------- uart structure --------------------------------------
typedef struct _vUart
{
  char c;
  int lineIndex = 0;
  int line1Index = 0;
  int BTlineIndex = 0;
  bool lineIsComment;
  bool lineSemiColon;
  char line[128];
  char line1[128];
  char BTline[20];
  String inputString;
  String BTinputString;
  String S1inputString;
  int V[16];
  char ctemp[30];
  char I2C_Data[80];
  int DC_Spped = 50;
  float Voltage[16];
  int Buffer[128];
  int StartCnt = 0;
  int ReadCnt = 0;
  int sensorValue = 0;
} vUart;
vUart *Uart_Ptr;
vUart Uart;

//-------------------------------------------------
void setup()
{
  Uart.inputString.reserve(60);
  Serial.begin(9600);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  Serial.println(F("init"));

}
//-----------------------------------------
void loop()
{
  Serial.print(F("Main at core:"));
  Serial.println(xPortGetCoreID());
  while(1)
  {
    while (Serial.available() > 0)
    {
      Uart.c = Serial.read();

      if ((Uart.c == '\n') || (Uart.c == '\r'))
      { // End of line reached
        if (Uart.lineIndex > 0)
        { // Line is complete. Then execute!
          Uart.line[Uart.lineIndex] = '\0'; // Terminate string
         
          Serial2.write(Uart.line);
          Serial2.write("\r\n");

          processCommand(Uart.line); // do something with the command

          Uart.lineIndex = 0;
          Uart.inputString = "";
        }
        else
        {
          // Empty or comment line. Skip block.
        }
        Uart.lineIsComment = false;
        Uart.lineSemiColon = false;
        Serial.println(F("ok>"));
      }
      else
      {
        //Serial.println( c );
        if ((Uart.lineIsComment) || (Uart.lineSemiColon))
        {
          if (Uart.c == ')')
            Uart.lineIsComment = false; // End of comment. Resume line.
        }
        else
        {
          if (Uart.c == '/')
          { // Block delete not supported. Ignore character.
          }
          else if (Uart.c == '~')
          { // Enable comments flag and ignore all characters until ')' or EOL.
            Uart.lineIsComment = true;
          }
          else if (Uart.c == ';')
          {
            Uart.lineSemiColon = true;
          }
          else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
          {
            Serial.println("ERROR - lineBuffer overflow");
            Uart.lineIsComment = false;
            Uart.lineSemiColon = false;
          }
          else if (Uart.c >= 'a' && Uart.c <= 'z')
          { // Upcase lowercase
            Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += (char)(Uart.c - 'a' + 'A');
          }
          else
          {
            Uart.line[Uart.lineIndex] = Uart.c;
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += Uart.c;
          }
        }
      }
    } //while (Serial.available() > 0)
    while (Serial2.available() > 0)
    {
      Uart.c = Serial2.read();

      if ((Uart.c == '\n') || (Uart.c == '\r'))
      { // End of line reached
        if (Uart.lineIndex > 0)
        { // Line is complete. Then execute!
          Uart.line[Uart.lineIndex] = '\0'; // Terminate string
         
          processCommand(Uart.line); // do something with the command
          Serial.println(Uart.line);
         
          Uart.lineIndex = 0;
          Uart.inputString = "";
        }
        else
        {
          // Empty or comment line. Skip block.
        }
        Uart.lineIsComment = false;
        Uart.lineSemiColon = false;
        Serial.println(F("ok>"));
      }
      else
      {
        //Serial.println( c );
        if ((Uart.lineIsComment) || (Uart.lineSemiColon))
        {
          if (Uart.c == ')')
            Uart.lineIsComment = false; // End of comment. Resume line.
        }
        else
        {
          if (Uart.c == '/')
          { // Block delete not supported. Ignore character.
          }
          else if (Uart.c == '~')
          { // Enable comments flag and ignore all characters until ')' or EOL.
            Uart.lineIsComment = true;
          }
          else if (Uart.c == ';')
          {
            Uart.lineSemiColon = true;
          }
          else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
          {
            Serial.println("ERROR - lineBuffer overflow");
            Uart.lineIsComment = false;
            Uart.lineSemiColon = false;
          }
          else if (Uart.c >= 'a' && Uart.c <= 'z')
          { // Upcase lowercase
            Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += (char)(Uart.c - 'a' + 'A');
          }
          else
          {
            Uart.line[Uart.lineIndex] = Uart.c;
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += Uart.c;
          }
        }
      }
    } //while (Serial2.available() > 0)
   

  }
}
//----------------------------------------
void processCommand(char *data)
{
  int len, xlen, ylen, zlen, alen;
  int tempDIO;
  String stemp;

  len = Uart.inputString.length();
  //---------------------------------------
  if (strstr(data, "VER") != NULL)
  {
    Serial.println(F("ESP32_SEARIAL_20230724"));
  }

}
//-----------------------------------------

Mega2560

#include <Arduino_FreeRTOS.h>
//---------------structure ----------------------------------------
//--------- Flag structure --------------------------------------
typedef struct _vFlag
{
  uint8_t LEDFlag=1;
  uint8_t BTFlag = 0;
  uint8_t ServoFlag = 0;
 
} vFlag;
vFlag *flag_Ptr;
vFlag flag;
//----------uart--------------
#define LINE_BUFFER_LENGTH 64
//--------- uart structure --------------------------------------
typedef struct _vUart
{
  char c;
  int lineIndex = 0;
  int line1Index = 0;
  int BTlineIndex = 0;
  bool lineIsComment;
  bool lineSemiColon;
  char line[128];
  char BTline[20];
  String inputString;
  String BTinputString;
  String S1inputString;
  int V[16];
  char ctemp[30];
  char I2C_Data[80];
  int DC_Spped = 50;
  float Voltage[16];
  int Buffer[128];
  int StartCnt = 0;
  int ReadCnt = 0;
  int sensorValue = 0;
} vUart;
vUart *Uart_Ptr;
vUart Uart;
//------------------------------------------------
const uint8_t LED_PIN = 13;
//----relay--------
uint8_t DIO_1 = 22;
uint8_t DIO_2 = 24;
uint8_t DIO_3 = 26;
uint8_t DIO_4 = 28;
uint8_t DIO_5 = 30;
uint8_t DIO_6 = 32;
uint8_t DIO_7 = 34;
uint8_t DIO_8 = 36;
//-------------------------------------------
char ctemp[20];
//------------------------------
TaskHandle_t hled;
TaskHandle_t huart0;
//------------------------------------------------------------------------------
void initial()
{
  Serial.println(F("Create Task"));
  //----------------------------------------------------------------------
  // create UART task
  xTaskCreate(vUARTTask, "UART Task", configMINIMAL_STACK_SIZE, NULL, 1, &huart0);
  // create blink task
  xTaskCreate(vLEDFlashTask, "LED Task", configMINIMAL_STACK_SIZE, NULL, 2, &hled);
 
  //-------------------------------------------------------------------

}
void setup()
{
  Uart.inputString.reserve(60);
  initial();
  Serial.begin(9600);
  Serial1.begin(115200);
  Serial.setTimeout(2000);
  Serial.println(F("init"));
  //-------------------------------------
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); // Turn LED off.
  //---------------IO setting-----------------
  pinMode(DIO_1, INPUT);
  pinMode(DIO_2, INPUT);
  pinMode(DIO_3, INPUT);
  pinMode(DIO_4, INPUT);
  pinMode(DIO_5, INPUT);
  pinMode(DIO_6, INPUT);
  pinMode(DIO_7, INPUT);
  pinMode(DIO_8, INPUT);
 // start FreeRTOS
  Serial.println("Systom On!");
  vTaskStartScheduler();
  //if the scheduler start the code don't came here
  Serial.println(F("Die"));
  while (1)
    ;
}
//------------------------------------------------------------------------------
void loop()
{
 
}
//------------------------------------------------------------------------------
// high priority for blinking LED
void vLEDFlashTask(void *pvParameters)
{
  (void)pvParameters;

  pinMode(LED_PIN, OUTPUT);
  for (;;)
  {
    digitalWrite(LED_PIN, HIGH);                    // Turn LED on.
    vTaskDelay((150L * configTICK_RATE_HZ) / 1000L); // Sleep for 50 milliseconds.
    digitalWrite(LED_PIN, LOW);                      // Turn LED off.
    vTaskDelay((150L * configTICK_RATE_HZ) / 1000L); // Sleep for 150 milliseconds.
  }
}
//------------------------------------------------------------------------------
void vUARTTask(void *pvParameters)
{
  Uart.lineIsComment = false;
  Uart.lineSemiColon = false;

  while(1)
  {
    while (Serial.available() > 0)
    {
      Uart.c = Serial.read();

      if ((Uart.c == '\n') || (Uart.c == '\r'))
      { // End of line reached
        if (Uart.lineIndex > 0)
        { // Line is complete. Then execute!
          Uart.line[Uart.lineIndex] = '\0'; // Terminate string
           
          Serial1.write(Uart.line);
          Serial1.write("\r\n");

          processCommand(Uart.line); // do something with the command
                   
          Uart.lineIndex = 0;
          Uart.inputString = "";
        }
        else
        {
          // Empty or comment line. Skip block.
        }
        Uart.lineIsComment = false;
        Uart.lineSemiColon = false;
        Serial.println(F("ok>"));
      }
      else
      {
        //Serial.println( c );
        if ((Uart.lineIsComment) || (Uart.lineSemiColon))
        {
          if (Uart.c == ')')
            Uart.lineIsComment = false; // End of comment. Resume line.
        }
        else
        {
          if (Uart.c == '/')
          { // Block delete not supported. Ignore character.
          }
          else if (Uart.c == '~')
          { // Enable comments flag and ignore all characters until ')' or EOL.
            Uart.lineIsComment = true;
          }
          else if (Uart.c == ';')
          {
            Uart.lineSemiColon = true;
          }
          else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
          {
            Serial.println("ERROR - lineBuffer overflow");
            Uart.lineIsComment = false;
            Uart.lineSemiColon = false;
          }
          else if (Uart.c >= 'a' && Uart.c <= 'z')
          { // Upcase lowercase
            Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += (char)(Uart.c - 'a' + 'A');
          }
          else
          {
            Uart.line[Uart.lineIndex] = Uart.c;
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += Uart.c;
          }
        }
      }
    } //while (Serial.available() > 0)
    while (Serial1.available() > 0)
    {
      Uart.c = Serial1.read();

      if ((Uart.c == '\n') || (Uart.c == '\r'))
      { // End of line reached
        if (Uart.lineIndex > 0)
        { // Line is complete. Then execute!
          Uart.line[Uart.lineIndex] = '\0'; // Terminate string
         
          processCommand(Uart.line); // do something with the command
          Serial.println(Uart.line);
         
          Uart.lineIndex = 0;
          Uart.inputString = "";
        }
        else
        {
          // Empty or comment line. Skip block.
        }
        Uart.lineIsComment = false;
        Uart.lineSemiColon = false;
        Serial.println(F("ok>"));
      }
      else
      {
        //Serial.println( c );
        if ((Uart.lineIsComment) || (Uart.lineSemiColon))
        {
          if (Uart.c == ')')
            Uart.lineIsComment = false; // End of comment. Resume line.
        }
        else
        {
          if (Uart.c == '/')
          { // Block delete not supported. Ignore character.
          }
          else if (Uart.c == '~')
          { // Enable comments flag and ignore all characters until ')' or EOL.
            Uart.lineIsComment = true;
          }
          else if (Uart.c == ';')
          {
            Uart.lineSemiColon = true;
          }
          else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
          {
            Serial.println("ERROR - lineBuffer overflow");
            Uart.lineIsComment = false;
            Uart.lineSemiColon = false;
          }
          else if (Uart.c >= 'a' && Uart.c <= 'z')
          { // Upcase lowercase
            Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += (char)(Uart.c - 'a' + 'A');
          }
          else
          {
            Uart.line[Uart.lineIndex] = Uart.c;
            Uart.lineIndex = Uart.lineIndex + 1;
            Uart.inputString += Uart.c;
          }
        }
      }
    } //while (Serial1.available() > 0)
  }
}
//------------------------------------------------------------------------------
//----reset-----------
void (*resetFunc)(void) = 0;
//---------------------------------------------------------
void processCommand(char *data)
{

  int len, xlen, ylen, zlen, alen;
  int tempDIO;
  String stemp;
  unsigned int i, j, Comma;

  int val, maxv, minv;
  unsigned long duration;
  float Vpp;

  len = Uart.inputString.length();

  //-------------RESET---------------
  if (strstr(data, "VER") != NULL)
  {
    //Serial.println(F("W_ATE_Board_20201021"));
    Serial.println(F("Mega_SERIAL_20230723"));
  }
  if (strstr(data, "RESET") != NULL)
  {
    Serial.println(F("Reset"));
    resetFunc();
  }
  //===-----------------DIO port 1----------------
  if (strstr(data, "DIO1") != NULL)
  {
    //-DIO11_LOW
    if (data[3] == '1')
    {
      for (int i = 0; i < len; i++)
      {
        if (data[i] == '_')
        {
          //Serial.println("test");
          //Serial.println(i);
          xlen = i;
        }
        //ctemp[i-4]=data[i];
      }
      for (int i = 4; i < xlen; i++)
      {
        ctemp[i - 4] = data[i];
      }
      ctemp[xlen - 4] = '\0';
      tempDIO = atoi(ctemp);
      //Serial.println(tempDIO);
      for (int i = (xlen + 1); i < len; i++)
      {
        ctemp[i - (xlen + 1)] = data[i];
      }
      ctemp[len - (xlen + 1)] = '\0';

      tempDIO = tempDIO * 2 + 20;
      pinMode(tempDIO, OUTPUT);
      //if(strstr(ctemp, "ON") != NULL)
      //if(strcmp(ctemp, "ON")==0)
      //if (strcmp(Uart.ctemp, "LOW") == 0)
      if (strstr(ctemp, "LOW") != NULL)
      {
        digitalWrite(tempDIO, LOW);
      }
      else if (strstr(ctemp, "ON") != NULL)
      {
        digitalWrite(tempDIO, LOW);
      }
      else if (strstr(ctemp, "HIGH") != NULL)
      {
        digitalWrite(tempDIO, HIGH);
      }
      else if (strstr(ctemp, "OFF") != NULL)
      {
        digitalWrite(tempDIO, HIGH);
      }
    }
  }
}

YouTube Demonstration: