Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
769 B
1
Indexable
Never
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(9, 8); // RX | TX

unsigned long previousMillis = 0;
const long interval = 1000; // 每1秒傳送一次

void setup()
{
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-06 current bound rate (default 9600)
}

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;

    BTSerial.write("Hello, Bluetooth!");
    Serial.println("Sent data to Bluetooth.");
  }

  if (BTSerial.available())
  {
    char data = BTSerial.read();
    Serial.write(data);
  }

  if (Serial.available())
  {
    char data = Serial.read();
    BTSerial.write(data);
  }
}