Note that there are some explanatory texts on larger screens.

plurals
  1. POStandard Firmata with Xbee communication - Arduino
    primarykey
    data
    text
    <p>I'm having difficulties in configuring existing either processing or standard firmata code to be able to forward signal via xbee to another arduino.</p> <p>I'm powering a vibration motor off ard uno runnign stdrd firmata through my processing code via usb, but am trying to instead forward the output via xbee to other ard board and its xbee to power the motor on that board rather than the one connected to pc so the one connected with just act 'signal transmitter using xbee'</p> <p>Sorry for the long code, I'm running an 'app' in processing which when I give I input it sends signal via usb to aruino uno and tactile motor powers up. I instead bought a second board and two xbee s2 modules and shield to send signal wirelessly and power motor on second board.</p> <pre><code> 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 () &gt; 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 &lt; 6; i++) { serial.write(REPORT_ANALOG | i); serial.write(1); } for (int i = 0; i &lt; 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 &gt;&gt; 3] &gt;&gt; (pin &amp; 0x07)) &amp; 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 &gt;&gt; 3) &amp; 0x0F; if (value == 0) digitalOutputData[portNumber] &amp;= ~(1 &lt;&lt; (pin &amp; 0x07)); else digitalOutputData[portNumber] |= (1 &lt;&lt; (pin &amp; 0x07)); if (arduinoConnected) { serial.write(DIGITAL_MESSAGE | portNumber); serial.write(digitalOutputData[portNumber] &amp; 0x7F); serial.write(digitalOutputData[portNumber] &gt;&gt; 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 &amp; 0x0F)); serial.write(value &amp; 0x7F); serial.write(value &gt;&gt; 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 &gt; 0 &amp;&amp; inputData &lt; 128) { waitForData--; storedInputData[waitForData] = inputData; if (executeMultiByteCommand != 0 &amp;&amp; waitForData == 0) { //we got everything switch(executeMultiByteCommand) { case DIGITAL_MESSAGE: setDigitalInputs(multiByteChannel, (storedInputData[0] &lt;&lt; 7) + storedInputData[1]); break; case ANALOG_MESSAGE: setAnalogInput(multiByteChannel, (storedInputData[0] &lt;&lt; 7) + storedInputData[1]); break; case REPORT_VERSION: setVersion(storedInputData[1], storedInputData[0]); break; } } } else { if (inputData &lt; 0xF0) { command = inputData &amp; 0xF0; multiByteChannel = inputData &amp; 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"); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload