1. Trang chủ
  2. » Công Nghệ Thông Tin

Serial Monitor read the character enter in Serial monitor

12 5 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 12
Dung lượng 102,44 KB

Nội dung

Serial Monitor read the character enter in Serial monitor and print ou t char sendChar; void setup() { Serial begin(9600); } void loop() { if (Serial available()) { sendChar = Serial read(); Serial.Serial Monitor read the character enter in Serial monitor and print ou t char sendChar; void setup() { Serial begin(9600); } void loop() { if (Serial available()) { sendChar = Serial read(); Serial.

Serial Monitor // read the character enter in Serial monitor and print ou t char sendChar; void setup() { Serial.begin(9600); } void loop() { if (Serial.available()) { sendChar = Serial.read(); Serial.println(sendChar); } } Result Type “Hello World” in monitor, print out: Hello World (words by words) To print in serial monitor in one line char sendChar; void setup() { Serial.begin(9600); } void loop() { if (Serial.available()) { sendChar = Serial.read(); Serial.print(sendChar); } } Result type “hello world”, print out “hello world” in one line Use serial monitor to control LED Type H, L in serial monitor to control led 13 in arduino Uno R3 const int ledPin = 13; int incomingByte; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { if (Serial.available() > 0) { incomingByte = Serial.read(); if (incomingByte == 'H') { digitalWrite(ledPin, HIGH); } if (incomingByte == 'L') { digitalWrite(ledPin, LOW); } } } Reference: Arduino cc, Physical Pixel Transmit data between 2 arduino Connection Arduino 1 Arduino 2 5V GND 5V GND RX TX TX RX Send 9, 8 turn on/off led in slave arduino Uploading the firmware to the Arduino before connecting the 2 wire TX RX between them Connecting these 2 wire during uploading time might led to the uploading problem Just power one Arduino Master void setup() { Serial.begin(9600); } void loop() { Serial.println(9); delay(2000); Serial.println(8); delay(2000); } Slave char received; void setup() { Serial.begin(9600); pinMode(16, OUTPUT); } void loop() { if (Serial.available()) { received = Serial.read() - 48; Serial.println(received); if (received == 9) { digitalWrite(16, HIGH); } else if (received == 8) { digitalWrite(16, LOW); } } } Controlling by words Master void setup() { Serial.begin(9600); } void loop() { Serial.println('H'); delay(2000); Serial.println('L'); delay(2000); } Slave char received; void setup() { Serial.begin(9600); pinMode(16, OUTPUT); } void loop() { if (Serial.available()) { received = Serial.read(); Serial.println(received); if (received == 'H') { digitalWrite(16, HIGH); } else if (received == 'L') { digitalWrite(16, LOW); } } } NOTE If in Master received == 'High' received == 'LOW' then in Slave if (received == 'High') { digitalWrite(16, HIGH); } else if (received == 'Low') { digitalWrite(16, LOW); } In Serial monitor of master: 2835 2647 In Serial monitor of slave: So it won’t work as the receive and send variable are char (mean one character) Like all times, " " is for string, ’ ’ is for char ASCII: ‘A’ = 65; ‘0’ = 48’,… To send the whole words to control LED Master void setup() { Serial.begin(9600); } void loop() { Serial.println("High"); delay(2000); Serial.println("Low"); delay(2000); } Slave If you change to received = Serial.read(); Serial.println(received); if (received == "High") { digitalWrite(16, HIGH); } else if (received == "Low") { digitalWrite(16, LOW); } You still reveive the ASCII number SoftwareSerial Type H, L in master Arduino,, turn on, off led in slave Arduino Connection Master 5V GND Slave 5V GND 2 (RX) TX 3 (TX) RX Master #include SoftwareSerial Arduino(2,3); // RX, TX char sendChar; void setup() { Serial.begin(9600); Arduino.begin(9600); } void loop() { sendChar = Serial.read(); Serial.print(sendChar); Arduino.println(sendChar); delay(1000); } Slave char received; void setup() { Serial.begin(9600); pinMode(16, OUTPUT); } void loop() { if (Serial.available()) { received = Serial.read(); Serial.println(received); if (received == 'H') { digitalWrite(16, HIGH); } else if (received == 'L') { digitalWrite(16, LOW); } } }

Ngày đăng: 29/08/2022, 22:09

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w