Reading a serial ASCII-encoded string.
This sketch demonstrates the Serial parseInt() function.
It looks for an ASCII string of comma-separated values.
It parses them into ints, and uses those to fade an RGB LED.
Circuit: Common-anode RGB LED wired like so:
* Red cathode: digital pin 3
* Green cathode: digital pin 5
* blue cathode: digital pin 6
* anode: +5V
created 13 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available()
> 0) {
// look for the next valid integer in the incoming serial stream:
int red = Serial.parseInt();
/ do it again:/
int green = Serial.parseInt();
// do it again:
int blue = Serial.parseInt();
// look for the newline. That's the end of your
// sentence:
if (Serial.read() == '
') {
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
red = 255 - constrain(red, 0, 255);
green = 255 - constrain(green, 0, 255);
blue = 255 - constrain(blue, 0, 255);
// fade the red, green, and blue legs of the LED:
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
// print the three numbers in one string as hexadecimal:
Serial.print(red, HEX);
Serial.print(green, HEX);
Serial.println(blue, HEX);
}
}
}
Reading a serial ASCII-encoded string. This sketch demonstrates the Serial parseInt() function. It looks for an ASCII string of comma-separated values. It parses them into ints, and uses those to fade an RGB LED. Circuit: Common-anode RGB LED wired like so: * Red cathode: digital pin 3 * Green cathode: digital pin 5 * blue cathode: digital pin 6 * anode: +5V created 13 Apr 2012 by Tom Igoe This example code is in the public domain. */// pins for the LEDs:const int redPin = 3;const int greenPin = 5;const int bluePin = 6;void setup() { // initialize serial: Serial.begin(9600); // make the pins outputs: pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT);}void loop() { // if there's any serial available, read it: while (Serial.available() > 0) { // look for the next valid integer in the incoming serial stream: int red = Serial.parseInt(); / do it again:/ int green = Serial.parseInt(); // do it again: int blue = Serial.parseInt(); // look for the newline. That's the end of your // sentence: if (Serial.read() == '
') { // constrain the values to 0 - 255 and invert // if you're using a common-cathode LED, just use "constrain(color, 0, 255);" red = 255 - constrain(red, 0, 255); green = 255 - constrain(green, 0, 255); blue = 255 - constrain(blue, 0, 255); // fade the red, green, and blue legs of the LED: analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); // print the three numbers in one string as hexadecimal: Serial.print(red, HEX); Serial.print(green, HEX); Serial.println(blue, HEX); } }}
การแปล กรุณารอสักครู่..

. อ่านสตริง ASCII เข้ารหัสแบบอนุกรม
ร่างนี้แสดงให้เห็นถึง parseInt อนุกรม () ฟังก์ชัน.
จะมองหาสตริง ASCII ของค่าคั่นด้วยเครื่องหมายจุลภาค.
มันแยกพวกเขาเข้าไปใน ints และใช้เหล่านั้นจะจางหายไปไฟ LED RGB.
Circuit: Common-ขั้วบวก ไฟ LED RGB สายเช่นดังนั้น:
* แคโทดสีแดง: ดิจิตอลขา 3
* แคโทดสีเขียว: พินดิจิตอล 5
* แคโทดสีน้ำเงิน: พินดิจิตอล 6
* ขั้วบวก + 5V
สร้าง 13 เมษายน 2012
โดยทอม Igoe
. รหัสตัวอย่างนี้อยู่ในโดเมนสาธารณะ
* /
// หมุดสำหรับไฟ LED:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
โมฆะการตั้งค่า () {
// เริ่มต้นอนุกรม:
Serial.begin (9600);
// ทำให้ผลหมุด:
pinMode (redPin เอาท์พุท);
pinMode (greenPin เอาท์พุท);
pinMode (bluePin เอาท์พุท);
}
โมฆะห่วง () {
// ถ้ามีใด ๆ ที่พร้อมอนุกรมอ่าน:
ในขณะที่ (Serial.available ()
> 0) {
// มองหาเลขที่ถูกต้องต่อไปในกระแสอนุกรมที่เข้ามา:
int สีแดง = Serial.parseInt ();
/ ทำมันอีกครั้ง: /
int สีเขียว = Serial.parseInt ();
// ทำมันอีกครั้ง:
int สีฟ้า = Serial.parseInt () ;
// มองหาการขึ้นบรรทัดใหม่ นั่นคือจุดสิ้นสุดของคุณ
ประโยค //:
ถ้า (Serial.read () == ' n') {
// จำกัด ค่าที่ 0-255 และกลับ
// ถ้าคุณกำลังใช้ทั่วไปแคโทด LED เพียงใช้ " จำกัด (สี, 0, 255);"
สีแดง = 255 - จำกัด (สีแดง, 0, 255);
สีเขียว = 255 - จำกัด (สีเขียว, 0, 255);
สีฟ้า = 255 - จำกัด (สีฟ้า, 0, 255);
// จางสีแดง, สีเขียว, สีฟ้าและขาของ LED:
analogWrite (redPin, สีแดง);
analogWrite (greenPin, สีเขียว);
analogWrite (bluePin, สีฟ้า);
// พิมพ์ตัวเลขที่สามในสายหนึ่งเป็นเลขฐานสิบหก:
อนุกรม การพิมพ์ (สีแดง, Hex);
Serial.print (สีเขียว, Hex);
Serial.println (สีฟ้า, Hex);
}
}
}
การแปล กรุณารอสักครู่..
