Advertisement
Guest User

juxXionArduino2+ULTRASONIC

a guest
Apr 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.83 KB | None | 0 0
  1. /*
  2.    Two way data transfer between ArduinoUno and junXion v5.3, using a more efficient data communication protocol
  3.  
  4.    junXionArduino.ino
  5.    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.
  6.    this new sketch allows for bidirectional comms with junXion v5.3
  7.    in this version the digital pins can be controlled by junXion, MAKE SURE TO DEFINE THE PROPER INPUT AND OUTPUT FOR THE DIGITAL PINS!!!!!!!
  8. */
  9.  
  10. void sendIntAs2ByteValue(unsigned int data);
  11.  
  12. #define baudrate      115200        // this is the default baudrate for jX-Ar communication, it should also be set to this value in the junXIon
  13. // preferences. Other choices are 57600, 19200 and 9600 (sometimes needed for more secure communication)
  14. #define kJunXid       308
  15.  
  16. #define kType 0
  17. #define kPin  1
  18. #define kRes  2
  19.  
  20.  
  21. /**************************************************************************
  22.  
  23.                Configuration of Inputs
  24.  
  25. ***************************************************************************/
  26. #define kBoardID       0       // change if multiple arduino boards are connected to use as identification of
  27. // the single boards (allowed value range: 0 - 9).
  28.  
  29.  
  30. // 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
  31. // Arduino board you are using, check this out on http://arduino.cc
  32. // An input is defined by 3 parameters: {type, pin, resolution}
  33. // standard types are: analog = 'a'; digital = 'd';  for these inputs the data of the given pin (2nd paramter) is send;
  34. // new types can be defined by using an other char, e.g. 'o', and defining a function to generate data (e.g., dataFunc)
  35. // the bit resolution of the send data has to be set in the 3rd parameter (e.g. digital: 1(-bit) and analog: 10(-bit))
  36.  
  37.  
  38. //int   inputN[3] = {inputType, pinNo, dataResolution}
  39.  
  40. int  pin2[3] = {'d', 2, 1};    // digital input: {'d', x, 1} pin x is on or off, digital connection pin 0 - 13
  41. int  pin3[3] = {'d', 3, 1};
  42. int  pin4[3] = {'d', 4, 1};
  43. int  pin5[3] = {'d', 5, 1};
  44. int  pin6[3] = {'d', 6, 1};
  45. int  pin7[3] = {'d', 7, 1};
  46. int  pin8[3] = {'d', 8, 1};
  47. int  pin9[3] = {'d', 9, 1};
  48. int  pin10[3] = {'d', 10, 1};
  49. int  pin11[3] = {'d', 11, 1};
  50. int  pin12[3] = {'d', 12, 1};
  51. int  pin13[3] = {'d', 13, 1};
  52.  
  53. int an0[3] = {'a', 0, 10};    // analog input  {'a', x, 10} pin x is variable, 10 bit resolution, analog connection pin 0 - 5
  54. int an1[3] = {'a', 1, 10};
  55. int an2[3] = {'a', 2, 10};
  56. int an3[3] = {'a', 3, 10};
  57. int an4[3] = {'a', 4, 10};
  58. int an5[3] = {'a', 5, 10};
  59.  
  60. int own0[3] = {'o', 0, 10};   // self defined input, pin number is negelected, can be used for inputs that are decoded in Arduino
  61. int own1[3] = {'o', 1, 8};    // sketch but that data needs to be send to junXion.
  62.  
  63. // When communicating  with junXion, the Arduino first will send all defined switches packed within 16bit data. After that it will
  64. // 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,
  65. // if 30 switches are available, then 2 16-bit messages are send.
  66. // 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.
  67. // e.g. int* inputs[kUsedInputs] = {input15, input16, input1};
  68.  
  69. // IMPORTANT: always first define the digital inputs, then the analog ones!
  70. // 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......
  71.  
  72. #define kMaxPins     64
  73.  
  74. #define kUsedInputs  17
  75.  
  76. int* inputs[kUsedInputs] = {pin2, pin3, pin4, pin5, pin6, pin7, pin8, pin9, pin10, pin11, pin12, an0, an1, an2, an3, an4, an5};
  77. bool pins[kMaxPins];                              // if pins[x] == 0 > input, if pins[x] = 1 > output, so make sure to define your output in here!
  78. int numUsedInputs         = kUsedInputs;
  79. int numUsedAnalogInputs   = 6;                    // the 'own' inputs are also listed as analog inputs....
  80. int numUsedDigitalInputs  = kUsedInputs - numUsedAnalogInputs;
  81.  
  82. unsigned short switches[4] = {0, 0, 0, 0};       // memory space for up to 64 switches
  83. unsigned short bits[8] = {1, 2, 4, 8, 16, 32, 64, 128}; // used for bit extraction in pin on/off decoding
  84.  
  85. int numDataBytes;
  86.  
  87. // --------------------------------------------------------------------------------------------------------------------------------------------
  88. // self defined data funtion that is executed for input type 'o'
  89. int dataFunc(int i)
  90. {
  91.   // put your own code here.....
  92.   return 0;
  93. }
  94.  
  95. // --------------------------------------------------------------------------------------------------------------------------------------------
  96. // function to send data of the active inputs, called from the main loop
  97. void sendInputData()
  98. {
  99.   int ax, nr, i, j;
  100.  
  101.   Serial.write(0xFF);                    // header of the package
  102.   Serial.write(0xFF);
  103.   Serial.write(numDataBytes);           // how many bytes of data are attached
  104.   Serial.print('d');                // cmd byte to identify what kind of package it is
  105.  
  106.   for (i = 0; i < 4; i++)                     // clear the switches....
  107.     switches[i] = 0;
  108.  
  109.   j = 0;
  110.   nr = 0;
  111.  
  112.   for (i = 0; i < numUsedInputs; i++)                                 // first check the digital inputs, encode them and send needed bytes
  113.   {
  114.     if (inputs[i][kType] == 'd')
  115.     {
  116.       switches[nr] += ((!(digitalRead(inputs[i][kPin]))) << (j++));
  117.       if (j == 16)                                 // 16 bits encoded, move to next slot in buffer
  118.       {
  119.         nr++;
  120.         j = 0;
  121.       }
  122.     }
  123.   }
  124.  
  125.   if (numUsedDigitalInputs)                                          // check if digital encoding needs to be sent, these bytes are always
  126.   { // send out last....
  127.     for (i = 0; i < (nr + 1); i++)
  128.       sendIntAs2ByteValue(switches[i]);
  129.   }
  130.  
  131.   for (i = 0; i < numUsedInputs; i++)
  132.   {
  133.     if (inputs[i][kType] == 'a')
  134.     {
  135.       ax = analogRead(inputs[i][kPin]);
  136.       sendIntAs2ByteValue(ax);                        // send data as 16-bit (2 bytes)
  137.     }
  138.     else if (inputs[i][kType] == 'o')
  139.     {
  140.       ax = dataFunc(i);
  141.       sendIntAs2ByteValue(ax);                        // send data as 16-bit (2 bytes)
  142.     }
  143.   }
  144. }
  145.  
  146. // --------------------------------------------------------------------------------------------------------------------------------------------
  147.  
  148.  
  149. /**************************************************************************
  150.  
  151.                Communication with junXion
  152.  
  153. ***************************************************************************/
  154.  
  155. /*
  156.      each package begins with 0xFF 0xFF followed by a byte that contains rest length of package
  157.      package size is max 255 bytes long (exclusiv header, size and cmd bytes)
  158.  
  159.      received packages:
  160.      ------------------
  161.                       header   size   cmd   data
  162.      cmd \ byte      1  |  2  |  3  |  4  | ....
  163.  
  164.      setInputSet    0xFF| 0xFF| 2*N | 'i' |type0| pin0| ... |typeN-1| pinN-1|
  165.  
  166.      startData      0xFF| 0xFF| 0x00| 'D' |
  167.  
  168.      stopData       0xFF| 0xFF| 0x00| 'S' |
  169.  
  170.      getBaudrate    0xFF| 0xFF| 0x00| 'B' |
  171.  
  172.      getNumInput    0xFF| 0xFF| 0x00| 'N' |
  173.  
  174.      getInputSet    0xFF| 0xFF| 0x00| 'I' |
  175.  
  176.      setPinData     0xFF| 0xFF| 0x02| 'K' | bank| bank pin settings|
  177.  
  178.      setPWMdata     0xFF| 0xFF| 0x02| 'P' | pin id| pwm value 0 - 255|
  179.  
  180.  
  181.      send packages:
  182.      --------------
  183.      sendInputSet   0xFF| 0xFF| 3*N | 'i' |type0| pin0| res0| ... |typeN-1|pinN-1|resN-1|
  184.  
  185.      sendBaudrate   0xFF| 0xFF| 0x01| 'b' | rate|
  186.  
  187.      sendData(16-b) 0xFF| 0xFF| 2*N | 'd' |    data0  |    data1  | ... |   dataN-1   |
  188. */
  189.  
  190. // --------------------------------------------------------------------------------------------------------------------------------------------
  191.  
  192.  
  193. #define kNoHeader       1
  194. #define kHeaderStart    2
  195. #define kHeaderDetected 3
  196.  
  197. bool sendData = false;
  198. bool headerExpected = true;
  199. int  packageSize = 0;
  200. unsigned char  bank = 0;
  201. unsigned char  pin = 0;
  202.  
  203. // --------------------------------------------------------------------------------------------------------------------------------------------
  204.  
  205. void blink()
  206. {
  207.   digitalWrite(13, HIGH);   // set the LED on
  208.   delay(500);              // wait
  209.   digitalWrite(13, LOW);    // set the LED off
  210.   delay(200);              // wait
  211. }
  212.  
  213. // --------------------------------------------------------------------------------------------------------------------------------------------
  214.  
  215. const int trigPin = 9;
  216. const int echoPin = 10;
  217. // defines variables
  218. long duration;
  219. int distance;
  220.  
  221. void setup()                              // this routine is called when user presses the reset button on the arduino board
  222. {
  223.  
  224.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  225.   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  226.  
  227.  
  228.   Serial.begin(baudrate);               // 57600 is the default  Arduino comm speed
  229.   digitalWrite(13, HIGH);               //startup blink
  230.   delay(1000);
  231.   digitalWrite(13, LOW);
  232.   delay(500);
  233.  
  234.   for (int i = 2; i < 13; i++)         // for now, all the Arduino Uno type board pins will be used as input pullup
  235.   {
  236.     pinMode (i, INPUT_PULLUP);
  237.   }
  238.  
  239.   pinMode(13, OUTPUT);                // here you define which digital pins you want to use as outputs
  240.   pins[13] = HIGH;
  241.  
  242.   numUsedInputs = kUsedInputs;
  243.   sendData = false;
  244.   headerExpected = true;
  245.   packageSize = 0;
  246.  
  247.   sendJunXid();
  248.   sendInputPrefs();
  249. }
  250.  
  251. // --------------------------------------------------------------------------------------------------------------------------------------------
  252.  
  253. void loop()                              // this routine is called when the Arduino is running, so it takes care of communication woth junXion
  254. {
  255.  
  256.   // Clears the trigPin
  257.   digitalWrite(trigPin, LOW);
  258.   delayMicroseconds(2);
  259.   // Sets the trigPin on HIGH state for 10 micro seconds
  260.   digitalWrite(trigPin, HIGH);
  261.   delayMicroseconds(10);
  262.   digitalWrite(trigPin, LOW);
  263.   // Reads the echoPin, returns the sound wave travel time in microseconds
  264.   duration = pulseIn(echoPin, HIGH);
  265.   // Calculating the distance
  266.   distance = duration * 0.034 / 2;
  267.   // Prints the distance on the Serial Monitor
  268.   Serial.print("Distance: ");
  269.   Serial.println(distance);
  270.  
  271.   int pin = 2;
  272.   int data = 0;
  273.  
  274.   if (headerExpected && Serial.available() > 2) // Check serial buffer for characters
  275.   {
  276.     if (Serial.read() == 0xFF) // check header
  277.     {
  278.       if (Serial.read() == 0xFF)
  279.       {
  280.         headerExpected = false;
  281.         packageSize = Serial.read(); // number of data bytes after cmd byte
  282.       }
  283.     }
  284.   }
  285.  
  286.   if ((!headerExpected) && Serial.available() > packageSize)
  287.   {
  288.     int cmd = Serial.read();
  289.  
  290.     switch (cmd)   // specifies content/purpose of package
  291.     {
  292.       case 'D':
  293.         sendData = true;
  294.         break;
  295.       case 'S':
  296.         sendData = false;
  297.         break;
  298.       case 'I':
  299.         sendInputPrefs();
  300.         break;
  301.       case 'B':
  302.         sendBoardID();
  303.         break;
  304.       case 'J':
  305.         sendJunXid();
  306.         break;
  307.       case 'K':
  308.         if (packageSize == 2)
  309.         {
  310.           bank = Serial.read();
  311.           data = Serial.read();
  312.         }
  313.         setPinData(bank, data);
  314.         break;
  315.       case 'P':
  316.         if (packageSize == 2)
  317.         {
  318.           pin = Serial.read();
  319.           data = Serial.read();
  320.         }
  321.         setPWMData(pin, data);
  322.         break;
  323.     }
  324.  
  325.     headerExpected = true;
  326.   }
  327.  
  328.   if (sendData) // send data
  329.     sendInputData();
  330.  
  331.   delay(5);
  332. }
  333.  
  334. // ------------------------------------------------------- other functions that can be called from the loop -----------------------------------
  335. // --------------------------------------------------------------------------------------------------------------------------------------------
  336.  
  337. void setPinData(unsigned char bank, unsigned char data)
  338. {
  339.   unsigned char offset = bank * 8;
  340.   int i, state;
  341.  
  342.   for (i = 0; i < 8; i++)
  343.   {
  344.     if (pins[offset + i])                                 // only write data to the defined OUTPUT pins!!!!!
  345.     {
  346.       state = (data & bits[i]) / bits[i];                 // the pin settings are encoded in 8 banks of 8 pins, for a total of 64 pins
  347.       digitalWrite(offset + i, state);                    // here the individual bits of (char)b and in bank (char)a are decoded.
  348.     }
  349.   }
  350. }
  351.  
  352. // --------------------------------------------------------------------------------------------------------------------------------------------
  353.  
  354. void setPWMData(unsigned char pin, unsigned char value)
  355. {
  356.   analogWrite(pin, value);
  357. }
  358.  
  359. // --------------------------------------------------------------------------------------------------------------------------------------------
  360. // send package containing the pristine input settings when starting up the board, also used to initialize some variables
  361. void sendInputPrefs()
  362. {
  363.   int i;
  364.   int numBytes;
  365.  
  366.   numBytes = 3 * numUsedInputs;
  367.  
  368.   Serial.write(0xFF);
  369.   Serial.write(0xFF);
  370.   Serial.write(numBytes);
  371.   Serial.print('p');
  372.  
  373.   for (i = 0; i < numUsedInputs; i++)
  374.   {
  375.     Serial.write((char)inputs[i][kType]);  // type of input i
  376.     Serial.write(inputs[i][kPin]);  // pin of input i
  377.     Serial.write(inputs[i][kRes]);   // resolution of input i
  378.   }
  379.  
  380.   numDataBytes = 2 * numUsedAnalogInputs;                         // analog data is send as 16-bit -> 2 bytes per input
  381.  
  382.   if (numUsedDigitalInputs > 0)
  383.     numDataBytes += (2 * ((numUsedDigitalInputs / 16) + 1));                  // digital data is send as 16-bit bitmaps -> 2 bytes per 16 inputs
  384. }
  385.  
  386. // --------------------------------------------------------------------------------------------------------------------------------------------
  387. // send 16-bit data as two bytes
  388. void sendIntAs2ByteValue(unsigned int data)             // function to send the pin  value followed by a "space".
  389. {
  390.   int b1 = data / 256;
  391.   int b2 = data % 256;
  392.   Serial.write(b1);
  393.   Serial.write(b2);
  394. }
  395.  
  396. // --------------------------------------------------------------------------------------------------------------------------------------------
  397.  
  398. void sendJunXid()
  399. {
  400.   Serial.write(0xFF);
  401.   Serial.write(0xFF);
  402.   Serial.write(0x02);            // # bytes after cmd byte
  403.   Serial.print('j');                  // cmd byte
  404.   sendIntAs2ByteValue(kJunXid);
  405.   digitalWrite(13, HIGH);             ///startup blink
  406.   delay(300);
  407.   digitalWrite(13, LOW);
  408.   delay(500);
  409. }
  410.  
  411. // --------------------------------------------------------------------------------------------------------------------------------------------
  412.  
  413. void sendBoardID()
  414. {
  415.   Serial.write(0xFF);
  416.   Serial.write(0xFF);
  417.   Serial.write(0x01);    // # bytes after cmd byte
  418.   Serial.print('b');          // cmd byte
  419.   Serial.print(kBoardID);
  420. }
  421.  
  422. // --------------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement