【问题标题】:Arduino (processing) Library in Netbeans and controlNetbeans 和控制中的 Arduino(处理)库
【发布时间】:2011-02-12 04:19:07
【问题描述】:

我正在尝试控制 4 个 LED 并从 4 个触点获取模拟输入。该程序是用java编写的,因此要访问arduino的功能,例如AnalogRead()并将LED设置为高或低,导入处理库是否可以让程序使用这些功能?

我也想知道,如果程序会自己传输到 arduino,还是 java 程序只会从引脚中提取数据?

【问题讨论】:

    标签: java arduino led


    【解决方案1】:

    更新:我的建议是首先尝试自己与 Processing 通信 Arduino。这就是我在下面描述的。如果您想直接使用 Processing 直接控制 Arduino,Binary Nerd 提供的链接是您入门的最佳选择。

    更新 2:也可以试试这个链接:Netbeans and Processing


    Arduino 代码在 Arduino 上运行,而 Processing 代码在您的计算机上运行。如果你想通过 Processing 控制你的 Arduino,你很可能会使用串口,​​并创建两个程序。一,在 Arduino 上,可能会接收命令并执行操作(打开或关闭 LED),或发回答案。另一个,在处理中,可能会向 Arduino 发送必要的命令并以某种方式处理它的答案。

    这是我为一个 LED 和一个模拟输入制作的一个简单示例。这是未经测试的代码。按照步骤。成功后,您可以尝试在 Netbeans 中直接使用 Arduino 处理。

    第 1 步。Arduino

    1. 购买一块 Arduino 板。
    2. 下载 Arduino IDE (http://www.arduino.cc)
    3. 将 Arduino 连接到您的计算机。
    4. 将 Arduino 代码(如下)复制到 Aruino IDE 中。
    5. 上传到 Arduino。

    第 2 步。处理

    1. 下载处理 IDE。
    2. 将处理代码(如下)复制到处理 IDE 中。
    3. 确保代码中的 COM 端口是 Arduino 连接的端口。
    4. 运行处理代码。

    Arduino 代码:

    int ledPin = 13;
    int analogPin = 0;
    char c = 0;
    
    void setup()
    {
        pinMode( ledPin, OUTPUT );
        Serial.begin( 9600 );
    }
    
    void loop()
    {
        // Wait for a character to arrive at the serial port.
        if( Serial.available() > 0 )
        {
            // Read one byte (character).
            c = Serial.read();
            switch( c )
            {
                case '1':
                    // Turn LED on.
                    digitalWrite( ledPin, HIGH );
                    break;
                case '0':
                    // Turn LED off.
                    digitalWrite( ledPin, LOW );
                    break;
                case 'q':
                case 'Q':
                    // Send the reading from the analog pin throught the serial port.
                    Serial.println( analogRead( analogPin ) );
                    break;
            }
        }
    }
    

    处理代码(在您的计算机上运行)。

    import processing.serial.*;
    
    Serial serial;
    String str;
    
    void setup()
    {
        size(400, 400);
        serial = new Serial(this, "COM1", 9600);    // Use the serial port connected
                                                    // to your Arduino.
        while( true )
        {
            serial.write( '1' );        // Turn LED on.
            delay( 1000 );              // Wait one second
            serial.write( '0' );        // Turn LED off.
            delay( 1000 );
            serial.write( 'Q' );        // Get analog reading
            serial.bufferUntil( 10 );   // Wait for the data from the Arduino.
                                        // This captures characters until a newline
                                        // is received, the runs serialEvent()...
        }
    }
    
    void draw()
    {
        background(0);
    }
    
    void serialEvent(Serial s)
    {
        println( s.readString() );
    }
    

    【讨论】:

    • 非常感谢,这个回答很有用,但是让我们不知道怎么处理 所以如果我导入处理库,我将获得处理的功能,从而阅读模拟, 并控制 LED。但是 arduino 程序,那将如何使用呢?它必须同时运行才能工作吗?由于处理是基于 Java 的,我想知道,如果语法和功能不会很相似?那么实际的函数,是用java写的,而analogread等调用是特定的处理函数?
    • 我在答案中添加了一些步骤。如果我回答了你的问题,请告诉我。
    • 我已经成功连接了 arduino 和处理,但我想使用 Netbeans IDE,所以我可以加载处理 jar 文件和整个库,在那个 IDE 中执行它,但是功能java的,它们会与处理代码一起工作吗?非常感谢代码示例,它们让事情变得非常清楚(:
    • 是的,你可以。我从未在 NetBeans 中做过,但我在 Eclipse 中做过。这里的想法是您在 Java 中运行处理代码,而不是相反。试试我在顶部添加的新链接。
    • 我阅读了您提供的链接,并感谢它,但我仍然无法使用 serial.read 和 serial.write。我导入了 RXTX 库,以及 arduino 和处理 lib 目录中的所有文件,但它们似乎都不允许我使用这些功能。
    【解决方案2】:

    使用处理库的想法是,您将标准固件程序上传到 arduino,然后使用串行通信通过 java 和处理访问 arduino 功能。

    这篇文章应该可以帮助您:Arduino and Processing

    因此,在您的 Java 程序中,您可以从输入读取模拟值,并根据该读数更改输出引脚上的值。

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多