Netbeans 中的 Arduino(处理)库和控制 [英] Arduino (processing) Library in Netbeans and control

查看:25
本文介绍了Netbeans 中的 Arduino(处理)库和控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试控制 4 个 LED 并从 4 个触点获取模拟输入.程序是用java编写的,所以为了获得arduino的功能,比如AnalogRead()和设置一个LED的高电平或低电平,导入处理库让程序可以使用这些功能吗?

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?

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

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?

推荐答案

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

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.

更新 2:也试试这个链接:Netbeans 和处理

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

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.

这是我为一个 LED 和一个模拟输入制作的快速示例.这是未经测试的代码.按照步骤.一旦成功,您可以尝试在 Netbeans 中直接将 Processing 与 Arduino 一起使用.

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.

第 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 代码:

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

这篇关于Netbeans 中的 Arduino(处理)库和控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆