wiki:wade/embedded/arduino/example/example_4

回到 Arduino

/*
 * Arduino 由 XBee 接收資料。輸入 1、2、3、4 會亮不同 led,按 2 會打開 relay
 */
 
 int serial_data = 0;    // 由 serial 讀入的資料
 
void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  clear_statue();
  //Serial.print("debug : serial_data =   ");
  //Serial.println(serial_data);
  //delay(500);
  if (Serial.available() > 0)
  {
    serial_data = Serial.read();
    Serial.print("I receive: ");
    Serial.println(serial_data);
  }
  switch (serial_data)
  {
    case 49:
      digitalWrite(2, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(10, HIGH);
      break;
    case 50:
      digitalWrite(3, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(11, HIGH);
      break;
    case 51:
      digitalWrite(4, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(12, HIGH);
      break;
    case 52:
      digitalWrite(5, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(13, HIGH);
      break;
  }
  delay(200);
}

void clear_statue()
{
  serial_data = 0;
  for (int i = 2; i <= 13; i++)
    digitalWrite(i, LOW);
}


Last modified 15 years ago Last modified on Mar 5, 2010, 2:35:38 PM