Netbeans 中的 Arduino(处理)库和控制

发布于 2024-08-30 08:53:26 字数 163 浏览 9 评论 0原文

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

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

I am trying to control 4 LEDs and getting analog input from 4 contacts. The program is written in java, so to gain acces to the functions of arduino, such as AnalogRead() and setting an LED to high or low, would importing the processing library let the program use those functions?

I was also wondering, if the program, will be transferred to the arduino it self, or the java program will just pull the data from the pins?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-09-06 08:53:26

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

更新 2:也尝试此链接:Netbeans 和处理


Arduino 代码在 Arduino 上运行,处理代码在您的计算机上运行。如果您想通过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() );
}

Update: My suggestion is to first try communicating the Arduino with Processing by youself. This is what I describe below. If you want to jump straight to controlling the Arduino directly with Processing, the link provided by Binary Nerd is your best choice to get you started.

Update 2: Try this link as well: Netbeans and Processing


Arduino code runs on the Arduino, and Processing code runs on your computer. If you want to control your Arduino through Processing, you will most likely use the serial port, and create two programs. One, on the Arduino, might receive commands and perform actions (turn LEDs on or off), or send back answers. The other, in Processing, might send the Arduino the necessary commands and process it's answers in some way.

Here is a quick example I made for one LED and one analog input. This is untested code. Follow the steps. Once this has worked, you could try using Processing directly with the Arduino in Netbeans.

Step 1. Arduino

  1. Buy an Arduino board.
  2. Download the Arduino IDE (http://www.arduino.cc)
  3. Connect the Arduino to you computer.
  4. Copy the Arduino code (below) into the Aruino IDE.
  5. Upload to the Arduino.

Step 2. Processing

  1. Download the Processing IDE.
  2. Copy the Processing code (below) into the Processing IDE.
  3. Make sure the COM port in the code is the one the Arduino is connected to.
  4. Run the Processing code.

Arduino code:

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;
        }
    }
}

Processing code (runs on your computer).

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() );
}
天气好吗我好吗 2024-09-06 08:53:26

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

这篇文章应该可以帮助您入门: Arduino 和处理

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

The idea with using the processing library is that you upload a standard firmware program to the arduino and then use serial comms to access arduino functions using java and processing.

This article should get you going: Arduino and Processing

So in your Java program you could read the analog value from an input and change the value on an output pin based on that reading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文