| 
 | 
 
單獨一顆可以用 
#include <SoftwareSerial.h> 
 
const int sio =2;           // ColorPAL connected to pin 2 
const int unused = 255;         // Non-existant pin # for SoftwareSerial 
const int sioBaud = 4800; 
const int waitDelay =200; 
 
 
const int sio2 = 3;            // ColorPAL connected to pin 2 
const int unused2 = 255;         // Non-existant pin # for SoftwareSerial 
const int sioBaud2 = 7200; 
const int waitDelay2 =200; 
 
// Received RGB values from ColorPAL 
int red; 
int grn; 
int blu; 
 
 
int red2; 
int grn2; 
int blu2; 
 
// Set up two software serials on the same pin. 
SoftwareSerial serin(sio, unused); 
SoftwareSerial serout(unused, sio); 
 
SoftwareSerial serin2(sio2, unused2); 
SoftwareSerial serout2(unused2, sio2); 
 
void setup() { 
  Serial.begin(9600); 
  reset();                
  serout.begin(sioBaud); 
  pinMode(sio, OUTPUT); 
  serout.print("= (00 $ m) !"); 
  serout.end();               
  serin.begin(sioBaud);             
  pinMode(sio, INPUT); 
 
  reset2();                
  serout2.begin(sioBaud2); 
  pinMode(sio2, OUTPUT); 
  serout2.print("= (00 $ m) !"); 
  serout2.end();               
  serin2.begin(sioBaud2);             
  pinMode(sio2, INPUT); 
 
 
} 
 
void loop() { 
  serin2.begin(sioBaud2);     
  serin.begin(sioBaud);     
   readData();  
}   
 
// Reset ColorPAL; see ColorPAL documentation for sequence 
void reset() { 
  delay(200); 
  pinMode(sio, OUTPUT); 
  digitalWrite(sio, LOW); 
  pinMode(sio, INPUT); 
  while (digitalRead(sio) != HIGH); 
  pinMode(sio, OUTPUT); 
  digitalWrite(sio, LOW); 
  delay(80); 
  pinMode(sio, INPUT); 
  delay(waitDelay); 
} 
void reset2() { 
  delay(200); 
  pinMode(sio2, OUTPUT); 
  digitalWrite(sio2, LOW); 
  pinMode(sio2, INPUT); 
  while (digitalRead(sio2) != HIGH); 
  pinMode(sio2, OUTPUT); 
  digitalWrite(sio2, LOW); 
  delay(80); 
  pinMode(sio2, INPUT); 
  delay(waitDelay2); 
} 
 
void readData() { 
  char buffer[32]; 
 
  if (serin.available() > 0) { 
    // Wait for a $ character, then read three 3 digit hex numbers 
    buffer[0] = serin.read(); 
    if (buffer[0] == '$') { 
      for(int i = 0; i < 9; i++) { 
        while (serin.available() == 0);     // Wait for next input character 
        buffer[i] = serin.read(); 
        if (buffer[i] == '$')               // Return early if $ character encountered 
          return; 
      } 
      parseAndPrint(buffer); 
      delay(10); 
    } 
  } 
} 
 
// Parse the hex data into integers 
void parseAndPrint(char * data) { 
  sscanf (data, "%3x%3x%3x", &red, &grn, &blu); 
  sscanf (data, "%3x%3x%3x", &red2, &grn2, &blu2); 
  char buffer[32]; 
  sprintf(buffer, "R%4.4d G%4.4d B%4.4d", red, grn, blu); 
  sprintf(buffer, "2R%4.4d 2G%4.4d 2B%4.4d", red2, grn2, blu2); 
  Serial.println(buffer); 
  serin.end(); 
   serin2.end();  
} 
 
這樣跑起來很慢,求前輩解釋! |   
 
 
 
 |