Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Usb 带Xbee通信的标准Firmata-Arduino_Usb_Arduino_Processing_Xbee_Firmata - Fatal编程技术网

Usb 带Xbee通信的标准Firmata-Arduino

Usb 带Xbee通信的标准Firmata-Arduino,usb,arduino,processing,xbee,firmata,Usb,Arduino,Processing,Xbee,Firmata,我很难配置现有的处理或标准firmata代码,以便能够通过xbee将信号转发到另一个arduino 我正在通过usb处理代码为ard uno runnign stdrd firmata的振动电机供电,但我试图通过xbee将输出转发到其他ard板及其xbee,从而为该板上的电机供电,而不是连接到pc的电机,因此连接到pc的电机只需执行“使用xbee的信号发射器”即可 对不起,代码太长了,我正在运行一个“应用程序”,当我输入时,它通过usb向aruino uno发送信号,触觉电机启动。我买了第二块板

我很难配置现有的处理或标准firmata代码,以便能够通过xbee将信号转发到另一个arduino

我正在通过usb处理代码为ard uno runnign stdrd firmata的振动电机供电,但我试图通过xbee将输出转发到其他ard板及其xbee,从而为该板上的电机供电,而不是连接到pc的电机,因此连接到pc的电机只需执行“使用xbee的信号发射器”即可

对不起,代码太长了,我正在运行一个“应用程序”,当我输入时,它通过usb向aruino uno发送信号,触觉电机启动。我买了第二块板和两个xbee s2模块和屏蔽,用于无线发送信号,并为第二块板上的电机供电

        Arduino arduino;
    int vibroPin = 11;

    boolean verbose=false; // set true if we want more messages when Arduino not connected

    int sensorPin=0;
    int analogueMax=255;

    class VibroBox {

      Timer pulseTimer;

      VibroBox(PApplet app) {
        String[]devices = Serial.list();
        println(devices);
        arduino = new Arduino(app, devices[0], 111111);
        arduino.pinMode(vibroPin, Arduino.OUTPUT);
      }

      void turnOn() {
        println("Vibration On"); 
        arduino.pinMode(vibroPin, Arduino.OUTPUT);
        arduino.digitalWrite(vibroPin, Arduino.HIGH);
      }
      void turnOff() {
        println("Vibration Off"); 
        arduino.pinMode(vibroPin, Arduino.OUTPUT);
        arduino.digitalWrite(vibroPin, Arduino.LOW);
      }

      void pulse(int duration, int period) {
        println("Starting pulses - duration:"+ duration + "  period:"+period); 
        final int d=duration;
        pulseTimer = new Timer();
        TimerTask tt= new TimerTask() {
          public void run() {
            turnOnFor(d);
          }
        };
        pulseTimer.scheduleAtFixedRate(tt, 0, period);
      }

      void stopPulses() {
        println("Stopping pulses"); 
        pulseTimer.cancel();
      }

      void turnOnFor(int millis) {

        println("Vibration On For: "+ millis); 
        turnOn();
        Timer t = new Timer();
        TimerTask tt= new TimerTask() {
          public void run() {
            turnOff();
          }
        };
        t.schedule(tt, millis);
      }

      void turnOnVal(int value) {
        println("Vibration Val: "+ value);
        arduino.pinMode(vibroPin, Arduino.ANALOG);
        arduino.analogWrite(vibroPin, constrain(value, 0, analogueMax));
      }

      int getSensorValue() {
        return arduino.analogRead(sensorPin);
      }
    }

   //package cc.arduino;

      import processing.core.PApplet;
      import processing.serial.Serial;

      /**
       * Together with the Firmata 2 firmware (an Arduino sketch uploaded to the
       * Arduino board), this class allows you to control the Arduino board from
       * Processing: reading from and writing to the digital pins and reading the
       * analog inputs.
       */
      public class Arduino {
        /**
         * Constant to set a pin to input mode (in a call to pinMode()).
         */
        public static final int INPUT = 0;
        /**
         * Constant to set a pin to output mode (in a call to pinMode()).
         */
        public static final int OUTPUT = 1;
        /**
         * Constant to set a pin to analog mode (in a call to pinMode()).
         */
        public static final int ANALOG = 2;
        /**
         * Constant to set a pin to PWM mode (in a call to pinMode()).
         */
        public static final int PWM = 3;
        /**
         * Constant to set a pin to servo mode (in a call to pinMode()).
         */
        //  public static final int SERVO = 4;
        /**
         * Constant to set a pin to shiftIn/shiftOut mode (in a call to pinMode()).
         */
        //  public static final int SHIFT = 5;
        /**
         * Constant to set a pin to I2C mode (in a call to pinMode()).
         */
        //  public static final int I2C = 6;

        /**
         * Constant to write a high value (+5 volts) to a pin (in a call to
         * digitalWrite()).
         */
        public static final int LOW = 0;
        /**
         * Constant to write a low value (0 volts) to a pin (in a call to
         * digitalWrite()).
         */
        public static final int HIGH = 1;

        private final int MAX_DATA_BYTES = 32;

        private final int DIGITAL_MESSAGE        = 0x90; // send data for a digital 
port
        private final int ANALOG_MESSAGE         = 0xE0; // send data for an analog pin 
(or PWM)
        private final int REPORT_ANALOG          = 0xC0; // enable analog input by pin 
#
        private final int REPORT_DIGITAL         = 0xD0; // enable digital input by 
port
        private final int SET_PIN_MODE           = 0xF4; // set a pin to 
  INPUT/OUTPUT/PWM/etc
        private final int REPORT_VERSION         = 0xF9; // report firmware version
        //  private final int SYSTEM_RESET           = 0xFF; // reset from MIDI
        //  private final int START_SYSEX            = 0xF0; // start a MIDI SysEx 
   message
        //  private final int END_SYSEX              = 0xF7; // end a MIDI SysEx 
  message

        PApplet parent;
        Serial serial;
        SerialProxy serialProxy;

        int waitForData = 0;
        int executeMultiByteCommand = 0;
        int multiByteChannel = 0;
        int[] storedInputData = new int[MAX_DATA_BYTES];
        boolean parsingSysex;
        int sysexBytesRead;

        boolean arduinoConnected=true;

        int[] digitalOutputData = { 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        };
        int[] digitalInputData  = { 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        };
        int[] analogInputData   = { 
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        };

        int majorVersion = 0;
        int minorVersion = 0;

        // We need a class descended from PApplet so that we can override the
        // serialEvent() method to capture serial data.  We can't use the Arduino
        // class itself, because PApplet defines a list() method that couldn't be
        // overridden by the static list() method we use to return the available
        // serial ports.  This class needs to be public so that the Serial class
        // can access its serialEvent() method.
        public class SerialProxy extends PApplet {
          public SerialProxy() {
            // Create the container for the registered dispose() methods, so that
            // our Serial instance can register its dispose() method (which it does
            // automatically).
            //  disposeMethods = new RegisteredMethods();
          }

          public void serialEvent(Serial which) {
            // Notify the Arduino class that there's serial data for it to process.
            while (available () > 0)
              processInput();
          }
        }

        public void dispose() {
          if (arduinoConnected) {
            this.serial.dispose();
          }
        }

        /**
         * Get a list of the available Arduino boards; currently all serial devices
         * (i.e. the same as Serial.list()).  In theory, this should figure out
         * what's an Arduino board and what's not.
         */
        //  public static String[] list() {
        //    return Serial.list();
        //  }

        /**
         * Create a proxy to an Arduino board running the Firmata 2 firmware at the
         * default baud rate of 57600.
         *
         * @param parent the Processing sketch creating this Arduino board
         * (i.e. "this").
         * @param iname the name of the serial device associated with the Arduino
         * board (e.g. one the elements of the array returned by Arduino.list())
         */
        public Arduino(PApplet parent, String iname) {
          this(parent, iname, 111111);
        }

        /**
         * Create a proxy to an Arduino board running the Firmata 2 firmware.
         *
         * @param parent the Processing sketch creating this Arduino board
         * (i.e. "this").
         * @param iname the name of the serial device associated with the Arduino
         * board (e.g. one the elements of the array returned by Arduino.list())
         * @param irate the baud rate to use to communicate with the Arduino board
         * (the firmata library defaults to 57600, and the examples use this rate,
         * but other firmwares may override it)
         */
        public Arduino(PApplet parent, String iname, int irate) {
          this.parent = parent;
          this.serialProxy = new SerialProxy();
          try {
            this.serial = new Serial(serialProxy, iname, irate);

            try {
              Thread.sleep(3000);
            } 
            catch (InterruptedException e) {
            }

            for (int i = 0; i < 6; i++) {
              serial.write(REPORT_ANALOG | i);
              serial.write(1);
            }

            for (int i = 0; i < 2; i++) {
              serial.write(REPORT_DIGITAL | i);
              serial.write(1);
            }
          } 
          catch(Exception e) {
            println("No Arduino Connected.");
            arduinoConnected=false;
          }

          parent.registerDispose(this);
        }

        /**
         * Returns the last known value read from the digital pin: HIGH or LOW.
         *
         * @param pin the digital pin whose value should be returned (from 2 to 13,
         * since pins 0 and 1 are used for serial communication)
         */
        public int digitalRead(int pin) {
          return (digitalInputData[pin >> 3] >> (pin & 0x07)) & 0x01;
        }

        /**
         * Returns the last known value read from the analog pin: 0 (0 volts) to
         * 1023 (5 volts).
         *
         * @param pin the analog pin whose value should be returned (from 0 to 5)
         */
        public int analogRead(int pin) {
          return analogInputData[pin];
        }

        /**
         * Set a digital pin to input or output mode.
         *
         * @param pin the pin whose mode to set (from 2 to 13)
         * @param mode either Arduino.INPUT or Arduino.OUTPUT
         */
        public void pinMode(int pin, int mode) {
          if (arduinoConnected) {
            serial.write(SET_PIN_MODE);
            serial.write(pin);
            serial.write(mode);
  //serial.write('D'); this is what I've been trying to add to be done when an output   
 takes place
          } 
          else {
            if(verbose){
            println("Pin: "+pin+" mode: "+mode); }
          }
        }

        /**
         * Write to a digital pin (the pin must have been put into output mode with
         * pinMode()).
         *
         * @param pin the pin to write to (from 2 to 13)
         * @param value the value to write: Arduino.LOW (0 volts) or Arduino.HIGH
         * (5 volts)
         */
        public void digitalWrite(int pin, int value) {
          int portNumber = (pin >> 3) & 0x0F;

          if (value == 0)
            digitalOutputData[portNumber] &= ~(1 << (pin & 0x07));
          else
            digitalOutputData[portNumber] |= (1 << (pin & 0x07));


          if (arduinoConnected) {
            serial.write(DIGITAL_MESSAGE | portNumber);
            serial.write(digitalOutputData[portNumber] & 0x7F);
            serial.write(digitalOutputData[portNumber] >> 7);
          } 
          else {

            if(verbose){ println("Pin: "+pin+" digital value: "+value); }
          }
        }

        /**
         * Write an analog value (PWM-wave) to a digital pin.
         *
         * @param pin the pin to write to (must be 9, 10, or 11, as those are they
         * only ones which support hardware pwm)
         * @param the value: 0 being the lowest (always off), and 255 the highest
         * (always on)
         */
        public void analogWrite(int pin, int value) {
          pinMode(pin, PWM);
          if (arduinoConnected) {

            serial.write(ANALOG_MESSAGE | (pin & 0x0F));
            serial.write(value & 0x7F);
            serial.write(value >> 7);
          } 
          else {
            if(verbose){ println("Pin: "+pin+" analogue value: "+value); }
          }
        }

        private void setDigitalInputs(int portNumber, int portData) {
          //System.out.println("digital port " + portNumber + " is " + portData);
          digitalInputData[portNumber] = portData;
        }

        private void setAnalogInput(int pin, int value) {
          //System.out.println("analog pin " + pin + " is " + value);
          analogInputData[pin] = value;
        }

        private void setVersion(int majorVersion, int minorVersion) {
          //System.out.println("version is " + majorVersion + "." + minorVersion);
          this.majorVersion = majorVersion;
          this.minorVersion = minorVersion;
        }

        private int available() {
          if (arduinoConnected) {
            return serial.available();
          } 
          else {
            if(verbose){ println("Available?"); }
            return 0;
          }
        }

        private void processInput() {

          if (arduinoConnected) {
            int inputData = serial.read();
            int command;

            //    if (parsingSysex) {
            //      if (inputData == END_SYSEX) {
            //        parsingSysex = false;
            //        //processSysexMessage();
            //      } else {
            //        storedInputData[sysexBytesRead] = inputData;
            //        sysexBytesRead++;
            //      }
            //    } else
            if (waitForData > 0 && inputData < 128) {
              waitForData--;
              storedInputData[waitForData] = inputData;

              if (executeMultiByteCommand != 0 && waitForData == 0) {
                //we got everything
                switch(executeMultiByteCommand) {
                case DIGITAL_MESSAGE:
   setDigitalInputs(multiByteChannel, (storedInputData[0] << 7) + storedInputData[1]);
                  break;
                case ANALOG_MESSAGE:
    setAnalogInput(multiByteChannel, (storedInputData[0] << 7) + storedInputData[1]);
                  break;
                case REPORT_VERSION:
                  setVersion(storedInputData[1], storedInputData[0]);
                  break;
                }
              }
            } 
            else {
              if (inputData < 0xF0) {
                command = inputData & 0xF0;
                multiByteChannel = inputData & 0x0F;
              } 
              else {
                command = inputData;
                // commands in the 0xF* range don't use channel data
              }
              switch (command) {
              case DIGITAL_MESSAGE:
              case ANALOG_MESSAGE:
              case REPORT_VERSION:
                waitForData = 2;
                executeMultiByteCommand = command;
                break;
              }
            }
          } 
          else {
            if(verbose){   println("Processing Input"); }
          }
        }
      }
Arduino-Arduino;
int-vibroPin=11;
布尔详细值=false;//如果在Arduino未连接时需要更多消息,请设置true
int-sensorPin=0;
int analogueMax=255;
类振动筛{
定时器脉冲定时器;
Vibrabox(PApplet应用程序){
String[]devices=Serial.list();
println(设备);
arduino=新的arduino(应用程序,设备[0],111111);
arduino.pinMode(振动输入,arduino.OUTPUT);
}
无效开启(){
println(“振动开启”);
arduino.pinMode(振动输入,arduino.OUTPUT);
arduino.digitalWrite(振动蛋白,arduino.HIGH);
}
无效关闭(){
println(“振动关闭”);
arduino.pinMode(振动输入,arduino.OUTPUT);
arduino.digitalWrite(振动蛋白,arduino.LOW);
}
无效脉冲(整数持续时间,整数周期){
println(“开始脉冲-持续时间:+持续时间+”周期:+周期);
最终int d=持续时间;
脉冲定时器=新定时器();
TimerTask tt=新的TimerTask(){
公开募捐{
转换为(d);
}
};
脉冲定时固定时间(tt,0,周期);
}
无效停止(){
println(“停止脉冲”);
pulseTimer.cancel();
}
无效开启(整数毫秒){
println(“振动开启:“+millis”);
打开();
定时器t=新定时器();
TimerTask tt=新的TimerTask(){
公开募捐{
关闭();
}
};
t、 附表(tt,毫秒);
}
无效转换值(int值){
println(“振动值:”+值);
arduino.pinMode(振动泵、arduino.ANALOG);
analogWrite(Vibrapin,Constraint(值,0,analogueMax));
}
int getSensorValue(){
返回arduino.analogRead(传感器引脚);
}
}
//包cc.arduino;
导入处理.core.PApplet;
导入处理.serial.serial;
/**
*连同Firmata 2固件(Arduino草图上传至
*Arduino板),该类允许您从
*处理:读取和写入数字管脚,并读取
*模拟输入。
*/
公共级Arduino{
/**
*常量将pin设置为输入模式(在对pinMode()的调用中)。
*/
公共静态最终整数输入=0;
/**
*常量将pin设置为输出模式(在对pinMode()的调用中)。
*/
公共静态最终整数输出=1;
/**
*常量将管脚设置为模拟模式(在调用pinMode()时)。
*/
公共静态最终int模拟=2;
/**
*常量将引脚设置为PWM模式(在调用pinMode()时)。
*/
公共静态最终int PWM=3;
/**
*常量将引脚设置为伺服模式(在调用pinMode()时)。
*/
//公共静态最终int伺服=4;
/**
*常量将pin设置为shiftIn/SHIFTOUTOT模式(在对pinMode()的调用中)。
*/
//公共静态最终整数移位=5;
/**
*常量将管脚设置为I2C模式(在调用pinMode()时)。
*/
//公共静态最终int I2C=6;
/**
*将高值(+5伏)写入引脚的常数(在调用
*digitalWrite()。
*/
公共静态最终整数低=0;
/**
*将低值(0伏)写入引脚的常数(在调用
*digitalWrite()。
*/
公共静态最终int高=1;
私有最终整数最大数据字节=32;
私有最终整数数字_MESSAGE=0x90;//为数字签名发送数据
港口
专用最终int模拟_消息=0xE0;//发送模拟管脚的数据
(或PWM)
私有最终int报告\u模拟=0xC0;//通过引脚启用模拟输入
#
私有最终整数报告\u DIGITAL=0xD0;//通过启用数字输入
港口
私有最终int SET_PIN_MODE=0xF4;//将PIN设置为
输入/输出/PWM/etc
私有最终int报告\u版本=0xF9;//报告固件版本
//私有最终整数系统_RESET=0xFF;//从MIDI重置
//private final int START_SYSEX=0xF0;//启动MIDI SYSEX
消息
//私有final int END_SYSEX=0xF7;//结束MIDI SYSEX
消息
丘疹亲本;
连载;
串行代理串行代理;
int waitForData=0;
int executeMultiByteCommand=0;
int multiByteChannel=0;
int[]storedInputData=新int[MAX_DATA_BYTES];
布尔parsingSysex;
int sysexBytesRead;
布尔值=真;
int[]digitalOutputData={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int[]digitalInputData={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int[]analogInputData={
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int主版本=0;
int minorVersion=0;