【问题标题】:Processing/Bluetooth to Arduino处理/蓝牙到 Arduino
【发布时间】:2023-03-17 14:07:01
【问题描述】:

我想通过处理以无线方式点亮 LED。

到目前为止我所拥有的。

  1. 我可以(无线)使用名为“Bluterm”的串行终端打开我的 LED。
  2. 我可以通过按 1 或 0 来打开我的 LED,以便在处理过程中打开和关闭 LED。

我怎样才能将 Bluterm 排除在我的等式之外并使用处理通过蓝牙发送 1 和 0。

这是我的处理代码:

import processing.serial.*;

Serial port;


String string;
void setup(){
    String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
    port = new Serial(this, portName, 9600);
    port.bufferUntil('\n');
}

void draw() {

  printArray(string); 
}

void keyPressed() { 
  if (key =='1'){port.write('1');}
    if (key=='0') {port.write('0');}
    }

    void serialEvent(Serial port) {
      string = port.readStringUntil('\n');}

和 Arduino 代码


char data;
int led = 13;

void setup() { 
  pinMode(led, OUTPUT);
  Serial.begin(9600); 

}

void loop() { 
  if (Serial.available()>0){
    data = Serial.read(); 
  }


  if (data=='1'){
    Serial.println("HELLO");
    digitalWrite(led, HIGH);


    }

    else if (data=='0'){
      digitalWrite(led, LOW);
      Serial.println("BYE");}
    }

我有点迷茫,处理过程可以与蓝牙通话还是我总是需要终端?

如果有不清楚的地方,请不要犹豫,

感谢您的宝贵时间,

朱莉安

【问题讨论】:

    标签: bluetooth arduino serial-port processing


    【解决方案1】:

    处理代码有意义。

    它可以做一些格式化和错误检查,但几乎都在那里:

    import processing.serial.*;
    
    Serial port;
    
    
    String string = "";
    void setup() {
      String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
      try{
        port = new Serial(this, portName, 9600);
        port.bufferUntil('\n');
      }catch(Exception e){
        e.printStackTrace();
      }
    }
    
    void draw() {
      background(0);
      text(string,10,15);
    }
    
    void keyPressed() { 
      if(port != null){
        if (key =='1') {
          port.write('1');
        }
        if (key=='0') {
          port.write('0');
        }
      }
    }
    
    void serialEvent(Serial port) {
      string = port.readString();
      if(string == null){
        println("null serial string");
        string = "";
      }
    }
    

    Arduino 代码看起来也合法。

    尚不清楚您使用的是什么蓝牙模块以及如何设置它。

    例如,如果您使用的是 BlueSmirf 之类的东西,请务必use the guide 提供。

    主要有以下几点:

    1. 确保您使用的是 SerialPortProfile (SPP) Bluetooth Profile
    2. 仔细检查您的接线:您的 Arduino 代码读取方式是将 BT 模块的 TX 连接到 Arduino 的 RX 引脚 0,将 BT 模块的 RX 引脚连接到 Arduino 的 TX 引脚 1。注意您可能想要要做到这一点你用 Arduino 上传你的固件(因为 pin 的 0 和 1 是 Arduino 的硬件串行),否则转到第 3 点 :)(推荐)
    3. 如果您使用具有多个硬件串行端口(如 Arduino mega)的 Arduino,请使用那些(例如 Serial1 而不是 Serial),否则请使用具有低波特率(如 9600)的 SoftwareSerial 库,避免高波特率费率。

    更新

    HC-05 模块使用 3.3V 逻辑,而 Arduino 使用 5V 逻辑。 使用双向 3.3V 5V 逻辑电平转换器或电阻器,否则可能会烧毁 HC-05 模块:

    快速搜索返回详细的HowToMechatronics.com Arduino and HC-05 Bluetooth Module Tutorial

    【讨论】:

    • 您好,乔治,我使用的是 HC05。它是这样连接的:5V-5V,GRN-GRN,TX-RX,RX-TX。我将如何在我的代码中暗示 SPP?
    • 足够接近,虽然 HC05 是 3.3V 逻辑(不是 5V)。希望你还没有炒掉你的板子,你可以继续我上面链接的教程。
    【解决方案2】:

    我看到你正在使用一个 hc05 蓝牙设备,我自己有这个,但我真的没有得到你想要用来将 1 和 0 发送到你的 hc05 的东西,你是否只使用一个 LED,因为如果它是我将能够提供帮助(如果您想通过移动应用发送蓝牙信号,请尝试应用商店或谷歌 Play 商店中的 blynk 应用)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      相关资源
      最近更新 更多