Last active
April 8, 2016 03:19
-
-
Save hunandy14/f00145a13707c47a3213 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include <Wire.h> | |
#define Uart_Tx 2 | |
#define Uart_Rx 3 | |
#define Led 13 | |
SoftwareSerial Uart(Uart_Tx,Uart_Rx); | |
char str[32]=""; | |
/*================================================*/ | |
void setup(){ | |
Serial.begin(9600); | |
Uart.begin(38400); | |
Serial.println("Welcom Uart CTRL"); | |
Uart.println("Welcom Uart CTRL"); | |
pinMode(Led, OUTPUT); | |
} | |
/*================================================*/ | |
void loop(){ | |
ScanUartStr(); | |
ScanSerialStr(); | |
} | |
/*================================================*/ | |
void ScanUartStr(){ | |
if(Uart.available()) { | |
int strnum=0; | |
memset( str, 0, strlen(str) ); | |
while (Uart.available() > 0){ | |
str[strnum++] = Uart.read(); | |
delay(3); | |
} | |
/* Aaduino印出Uart接收到的 */ | |
Serial.print("Uart => :"); | |
Serial.println(str); | |
/* Aaduino回傳Uart接收到的 */ | |
Uart.print("Arduino Return:"); | |
Uart.println(str); | |
CtrlLED(); | |
} | |
} | |
void ScanSerialStr(){ | |
if(Serial.available()) { | |
int strnum=0; | |
memset( str, 0, strlen(str) ); | |
while (Serial.available() > 0){ | |
str[strnum++] = Serial.read(); | |
delay(3); | |
} | |
/* Arduino印出自己輸入的 */ | |
Serial.print("Arduino Input :"); | |
Serial.println(str); | |
/* Arduino送出自己輸入的 */ | |
Uart.print("Arduino => : "); | |
Uart.println(str); | |
CtrlLED(); | |
} | |
} | |
void CtrlLED(){ | |
Serial.print("CtrlLED => "); | |
if(strcmp(str,"1") == 0){ | |
Serial.print("LED 13 is ON"); | |
digitalWrite(Led, HIGH); | |
} | |
if(strcmp(str,"0") == 0){ | |
Serial.print("LED 13 is OFF"); | |
digitalWrite(Led, LOW); | |
} | |
Serial.println(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment