Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Two way data transfer between ArduinoUno and junXion v5.3, using a more efficient data communication protocol
- junXionArduino.ino
- this sketch can be used by all the Arduino boards, you will need to dpt the Sketch for your deisred amount of analog and digital inputs ans outputs.
- this new sketch allows for bidirectional comms with junXion v5.3
- in this version the digital pins can be controlled by junXion, MAKE SURE TO DEFINE THE PROPER INPUT AND OUTPUT FOR THE DIGITAL PINS!!!!!!!
- */
- void sendIntAs2ByteValue(unsigned int data);
- #define baudrate 115200 // this is the default baudrate for jX-Ar communication, it should also be set to this value in the junXIon
- // preferences. Other choices are 57600, 19200 and 9600 (sometimes needed for more secure communication)
- #define kJunXid 308
- #define kType 0
- #define kPin 1
- #define kRes 2
- /**************************************************************************
- Configuration of Inputs
- ***************************************************************************/
- #define kBoardID 0 // change if multiple arduino boards are connected to use as identification of
- // the single boards (allowed value range: 0 - 9).
- // INPUTS: in this example we are using 10 digital inputs and 6 analog inputs. How many of these inputs are available depends on the type of
- // Arduino board you are using, check this out on http://arduino.cc
- // An input is defined by 3 parameters: {type, pin, resolution}
- // standard types are: analog = 'a'; digital = 'd'; for these inputs the data of the given pin (2nd paramter) is send;
- // new types can be defined by using an other char, e.g. 'o', and defining a function to generate data (e.g., dataFunc)
- // the bit resolution of the send data has to be set in the 3rd parameter (e.g. digital: 1(-bit) and analog: 10(-bit))
- //int inputN[3] = {inputType, pinNo, dataResolution}
- int pin2[3] = {'d', 2, 1}; // digital input: {'d', x, 1} pin x is on or off, digital connection pin 0 - 13
- int pin3[3] = {'d', 3, 1};
- int pin4[3] = {'d', 4, 1};
- int pin5[3] = {'d', 5, 1};
- int pin6[3] = {'d', 6, 1};
- int pin7[3] = {'d', 7, 1};
- int pin8[3] = {'d', 8, 1};
- int pin9[3] = {'d', 9, 1};
- int pin10[3] = {'d', 10, 1};
- int pin11[3] = {'d', 11, 1};
- int pin12[3] = {'d', 12, 1};
- int pin13[3] = {'d', 13, 1};
- int an0[3] = {'a', 0, 10}; // analog input {'a', x, 10} pin x is variable, 10 bit resolution, analog connection pin 0 - 5
- int an1[3] = {'a', 1, 10};
- int an2[3] = {'a', 2, 10};
- int an3[3] = {'a', 3, 10};
- int an4[3] = {'a', 4, 10};
- int an5[3] = {'a', 5, 10};
- int own0[3] = {'o', 0, 10}; // self defined input, pin number is negelected, can be used for inputs that are decoded in Arduino
- int own1[3] = {'o', 1, 8}; // sketch but that data needs to be send to junXion.
- // When communicating with junXion, the Arduino first will send all defined switches packed within 16bit data. After that it will
- // send the user defined analog channels in 16 bit resolution. So if less than 16 switches are used, only one 16 bit message needs to be send,
- // if 30 switches are available, then 2 16-bit messages are send.
- // The user creates a list of all inputs that can be modified to define pins that should be used, allowing you to only process data from those inputs.
- // e.g. int* inputs[kUsedInputs] = {input15, input16, input1};
- // IMPORTANT: always first define the digital inputs, then the analog ones!
- // Make sure that when using any of the digital pins as output, you remove them from the inputs array, so they won't be shown in junXion......
- #define kMaxPins 64
- #define kUsedInputs 17
- int* inputs[kUsedInputs] = {pin2, pin3, pin4, pin5, pin6, pin7, pin8, pin9, pin10, pin11, pin12, an0, an1, an2, an3, an4, an5};
- bool pins[kMaxPins]; // if pins[x] == 0 > input, if pins[x] = 1 > output, so make sure to define your output in here!
- int numUsedInputs = kUsedInputs;
- int numUsedAnalogInputs = 6; // the 'own' inputs are also listed as analog inputs....
- int numUsedDigitalInputs = kUsedInputs - numUsedAnalogInputs;
- unsigned short switches[4] = {0, 0, 0, 0}; // memory space for up to 64 switches
- unsigned short bits[8] = {1, 2, 4, 8, 16, 32, 64, 128}; // used for bit extraction in pin on/off decoding
- int numDataBytes;
- // --------------------------------------------------------------------------------------------------------------------------------------------
- // self defined data funtion that is executed for input type 'o'
- int dataFunc(int i)
- {
- // put your own code here.....
- return 0;
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- // function to send data of the active inputs, called from the main loop
- void sendInputData()
- {
- int ax, nr, i, j;
- Serial.write(0xFF); // header of the package
- Serial.write(0xFF);
- Serial.write(numDataBytes); // how many bytes of data are attached
- Serial.print('d'); // cmd byte to identify what kind of package it is
- for (i = 0; i < 4; i++) // clear the switches....
- switches[i] = 0;
- j = 0;
- nr = 0;
- for (i = 0; i < numUsedInputs; i++) // first check the digital inputs, encode them and send needed bytes
- {
- if (inputs[i][kType] == 'd')
- {
- switches[nr] += ((!(digitalRead(inputs[i][kPin]))) << (j++));
- if (j == 16) // 16 bits encoded, move to next slot in buffer
- {
- nr++;
- j = 0;
- }
- }
- }
- if (numUsedDigitalInputs) // check if digital encoding needs to be sent, these bytes are always
- { // send out last....
- for (i = 0; i < (nr + 1); i++)
- sendIntAs2ByteValue(switches[i]);
- }
- for (i = 0; i < numUsedInputs; i++)
- {
- if (inputs[i][kType] == 'a')
- {
- ax = analogRead(inputs[i][kPin]);
- sendIntAs2ByteValue(ax); // send data as 16-bit (2 bytes)
- }
- else if (inputs[i][kType] == 'o')
- {
- ax = dataFunc(i);
- sendIntAs2ByteValue(ax); // send data as 16-bit (2 bytes)
- }
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- /**************************************************************************
- Communication with junXion
- ***************************************************************************/
- /*
- each package begins with 0xFF 0xFF followed by a byte that contains rest length of package
- package size is max 255 bytes long (exclusiv header, size and cmd bytes)
- received packages:
- ------------------
- header size cmd data
- cmd \ byte 1 | 2 | 3 | 4 | ....
- setInputSet 0xFF| 0xFF| 2*N | 'i' |type0| pin0| ... |typeN-1| pinN-1|
- startData 0xFF| 0xFF| 0x00| 'D' |
- stopData 0xFF| 0xFF| 0x00| 'S' |
- getBaudrate 0xFF| 0xFF| 0x00| 'B' |
- getNumInput 0xFF| 0xFF| 0x00| 'N' |
- getInputSet 0xFF| 0xFF| 0x00| 'I' |
- setPinData 0xFF| 0xFF| 0x02| 'K' | bank| bank pin settings|
- setPWMdata 0xFF| 0xFF| 0x02| 'P' | pin id| pwm value 0 - 255|
- send packages:
- --------------
- sendInputSet 0xFF| 0xFF| 3*N | 'i' |type0| pin0| res0| ... |typeN-1|pinN-1|resN-1|
- sendBaudrate 0xFF| 0xFF| 0x01| 'b' | rate|
- sendData(16-b) 0xFF| 0xFF| 2*N | 'd' | data0 | data1 | ... | dataN-1 |
- */
- // --------------------------------------------------------------------------------------------------------------------------------------------
- #define kNoHeader 1
- #define kHeaderStart 2
- #define kHeaderDetected 3
- bool sendData = false;
- bool headerExpected = true;
- int packageSize = 0;
- unsigned char bank = 0;
- unsigned char pin = 0;
- // --------------------------------------------------------------------------------------------------------------------------------------------
- void blink()
- {
- digitalWrite(13, HIGH); // set the LED on
- delay(500); // wait
- digitalWrite(13, LOW); // set the LED off
- delay(200); // wait
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- const int trigPin = 9;
- const int echoPin = 10;
- // defines variables
- long duration;
- int distance;
- void setup() // this routine is called when user presses the reset button on the arduino board
- {
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- Serial.begin(baudrate); // 57600 is the default Arduino comm speed
- digitalWrite(13, HIGH); //startup blink
- delay(1000);
- digitalWrite(13, LOW);
- delay(500);
- for (int i = 2; i < 13; i++) // for now, all the Arduino Uno type board pins will be used as input pullup
- {
- pinMode (i, INPUT_PULLUP);
- }
- pinMode(13, OUTPUT); // here you define which digital pins you want to use as outputs
- pins[13] = HIGH;
- numUsedInputs = kUsedInputs;
- sendData = false;
- headerExpected = true;
- packageSize = 0;
- sendJunXid();
- sendInputPrefs();
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- void loop() // this routine is called when the Arduino is running, so it takes care of communication woth junXion
- {
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- distance = duration * 0.034 / 2;
- // Prints the distance on the Serial Monitor
- Serial.print("Distance: ");
- Serial.println(distance);
- int pin = 2;
- int data = 0;
- if (headerExpected && Serial.available() > 2) // Check serial buffer for characters
- {
- if (Serial.read() == 0xFF) // check header
- {
- if (Serial.read() == 0xFF)
- {
- headerExpected = false;
- packageSize = Serial.read(); // number of data bytes after cmd byte
- }
- }
- }
- if ((!headerExpected) && Serial.available() > packageSize)
- {
- int cmd = Serial.read();
- switch (cmd) // specifies content/purpose of package
- {
- case 'D':
- sendData = true;
- break;
- case 'S':
- sendData = false;
- break;
- case 'I':
- sendInputPrefs();
- break;
- case 'B':
- sendBoardID();
- break;
- case 'J':
- sendJunXid();
- break;
- case 'K':
- if (packageSize == 2)
- {
- bank = Serial.read();
- data = Serial.read();
- }
- setPinData(bank, data);
- break;
- case 'P':
- if (packageSize == 2)
- {
- pin = Serial.read();
- data = Serial.read();
- }
- setPWMData(pin, data);
- break;
- }
- headerExpected = true;
- }
- if (sendData) // send data
- sendInputData();
- delay(5);
- }
- // ------------------------------------------------------- other functions that can be called from the loop -----------------------------------
- // --------------------------------------------------------------------------------------------------------------------------------------------
- void setPinData(unsigned char bank, unsigned char data)
- {
- unsigned char offset = bank * 8;
- int i, state;
- for (i = 0; i < 8; i++)
- {
- if (pins[offset + i]) // only write data to the defined OUTPUT pins!!!!!
- {
- state = (data & bits[i]) / bits[i]; // the pin settings are encoded in 8 banks of 8 pins, for a total of 64 pins
- digitalWrite(offset + i, state); // here the individual bits of (char)b and in bank (char)a are decoded.
- }
- }
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- void setPWMData(unsigned char pin, unsigned char value)
- {
- analogWrite(pin, value);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- // send package containing the pristine input settings when starting up the board, also used to initialize some variables
- void sendInputPrefs()
- {
- int i;
- int numBytes;
- numBytes = 3 * numUsedInputs;
- Serial.write(0xFF);
- Serial.write(0xFF);
- Serial.write(numBytes);
- Serial.print('p');
- for (i = 0; i < numUsedInputs; i++)
- {
- Serial.write((char)inputs[i][kType]); // type of input i
- Serial.write(inputs[i][kPin]); // pin of input i
- Serial.write(inputs[i][kRes]); // resolution of input i
- }
- numDataBytes = 2 * numUsedAnalogInputs; // analog data is send as 16-bit -> 2 bytes per input
- if (numUsedDigitalInputs > 0)
- numDataBytes += (2 * ((numUsedDigitalInputs / 16) + 1)); // digital data is send as 16-bit bitmaps -> 2 bytes per 16 inputs
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- // send 16-bit data as two bytes
- void sendIntAs2ByteValue(unsigned int data) // function to send the pin value followed by a "space".
- {
- int b1 = data / 256;
- int b2 = data % 256;
- Serial.write(b1);
- Serial.write(b2);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- void sendJunXid()
- {
- Serial.write(0xFF);
- Serial.write(0xFF);
- Serial.write(0x02); // # bytes after cmd byte
- Serial.print('j'); // cmd byte
- sendIntAs2ByteValue(kJunXid);
- digitalWrite(13, HIGH); ///startup blink
- delay(300);
- digitalWrite(13, LOW);
- delay(500);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
- void sendBoardID()
- {
- Serial.write(0xFF);
- Serial.write(0xFF);
- Serial.write(0x01); // # bytes after cmd byte
- Serial.print('b'); // cmd byte
- Serial.print(kBoardID);
- }
- // --------------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement