Guest User

firmata-firefly

a guest
Nov 5th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 24.15 KB | None | 0 0
  1. /*
  2.  Created by Andrew Payne
  3.  Modified from original Firefly Firmata written by Jason Kelly Johnson and Andrew Payne
  4.  Latest Update September 10th, 2012
  5.  Copyright 2012 | All Rights Reserved
  6.  
  7.  This Firmata allows you to control an Arduino board from Rhino/Grasshopper/Firefly.
  8.  Updates, Questions, Suggestions visit: http://www.fireflyexperiments.com
  9.  
  10.  1. Plug Arduino boards into your USB port; confirm that your Arduino's green power LED in on
  11.  2. Select your specific Arduino Board and Serial Port (Tools > Board; Tools > Serial Port) *Take note of your Serial Port COM #
  12.  3. Verify (play button) and Upload (upload button) this program to your Arduino, close the Arduino program
  13.  4. then open ... Rhino/Grasshopper/Firefly
  14.  
  15.  Note: The Firefly Firmata sets the following pins to perform these functions:
  16.  
  17.  *****ON STANDARD BOARDS (ie. Uno, Diecimila, Duemilanove, Lillypad, Mini, etc.)*****
  18.  ANALOG IN pins 0-5 are set to return values (from 0 to 1023) for analog sensors
  19.  DIGITAL IN pins 2,4,7 will return 0's or 1's; for 3 potential digital sensors (buttons, switches, on/off, true/false, etc.)
  20.  DIGITAL/ANALOG OUT pins 3,5,6,11 (marked with a ~) can be used to digitalWrite, analogWrite, or Servo.write depending on the input status of that Firefly pin
  21.  DIGITAL OUT pins 8,9,10,12,13 can be used to digitalWrite, Servo.write, or analogWrite depending on the input status of that Firefly pin
  22.  
  23.  *****ON MEGA BOARDS (ie. ATMEGA1280, ATMEGA2560)*****
  24.  ANALOG IN pins 0-15 will return values (from 0 to 1023) for 16 potential analog sensors
  25.  DIGITAL IN pins 34-41 will return 0's or 1's; for 8 potential digital sensors (buttons, switches, on/off, true/false, etc.)
  26.  DIGITAL/ANALOG OUT pins 2-13 can be used to digitalWrite, analogWrite, or Servo.write depending on the input status of that Firefly pin
  27.  DIGITAL OUT pins 22-33 can be used to digitalWrite, Servo.write, or analogWrite depending on the input status of that Firefly pin
  28.  
  29.  *****ON LEONARDO BOARDS*****
  30.  ANALOG IN pins 0-5 are set to return values (from 0 to 1023) for analog sensors
  31.  DIGITAL IN pins 2,4,7 will return 0's or 1's; for 3 potential digital sensors (buttons, switches, on/off, true/false, etc.)
  32.  DIGITAL/ANALOG OUT pins 3,5,6,11 (marked with a ~) can be used to digitalWrite, analogWrite, or Servo.write depending on the input status of that Firefly pin
  33.  DIGITAL OUT pins 8,9,10,12,13 can be used to digitalWrite, Servo.write, or analogWrite depending on the input status of that Firefly pin
  34.  */
  35.  
  36. #include <Servo.h>            // attach Servo library (http://www.arduino.cc/playground/ComponentLib/Servo)
  37. #include <pins_arduino.h>     // attach arduino pins header file to determine which board type is being used
  38.  
  39. #define BAUDRATE 115200       // Set the Baud Rate to an appropriate speed
  40. #define BUFFSIZE 512          // buffer one command at a time, 12 bytes is longer than the max length
  41.  
  42. /*==============================================================================
  43.  * GLOBAL VARIABLES
  44.  *============================================================================*/
  45.  
  46. char buffer[BUFFSIZE];        // declare buffer
  47. uint8_t bufferidx = 0;        // a type of unsigned integer of length 8 bits
  48. char *parseptr;
  49. char buffidx;
  50.  
  51. int counter = 0;
  52. int numcycles = 1000;
  53. int pingPin = 2;
  54. long duration, inches, cm;
  55.  
  56.  
  57. #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)                         // declare variables for STANDARD boards
  58.   uint16_t APin0, APin1, APin2, APin3, APin4, APin5, DPin2, DPin4, DPin7;             // declare input variables
  59.   uint16_t DPin3, DPin5, DPin6, DPin8, DPin9, DPin10, DPin11, DPin12, DPin13;         // declare output variables  
  60.   Servo Servo3, Servo5, Servo6, Servo8, Servo9, Servo10, Servo11, Servo12, Servo13;   // declare Servo objects
  61. #endif
  62.  
  63. #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)                        // declare variables for LEONARDO board
  64.   uint16_t APin0, APin1, APin2, APin3, APin4, APin5, DPin2, DPin4, DPin7;             // declare input variables
  65.   uint16_t DPin3, DPin5, DPin6, DPin8, DPin9, DPin10, DPin11, DPin12, DPin13;         // declare output variables  
  66.   Servo Servo3, Servo5, Servo6, Servo8, Servo9, Servo10, Servo11, Servo12, Servo13;   // declare Servo objects
  67. #endif
  68.  
  69. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)                        // declare variables for MEGA boards
  70.   uint16_t APin0, APin1, APin2, APin3, APin4, APin5, APin6, APin7, APin8, APin9, APin10, APin11, APin12, APin13, APin14, APin15, DPin22, DPin23, DPin24, DPin25, DPin26, DPin27, DPin28, DPin29, DPin30, DPin31;  // declare input variables
  71.   uint16_t DPin2, DPin3, DPin4, DPin5, DPin6, DPin7, DPin8, DPin9, DPin10, DPin11, DPin12, DPin13, DPin32, DPin33, DPin34, DPin35, DPin36, DPin37, DPin38, DPin39, DPin40, DPin41, DPin42, DPin43, DPin44, DPin45, DPin46, DPin47, DPin48, DPin49, DPin50, DPin51, DPin52, DPin53;  // declare output variables  
  72.   Servo Servo2, Servo3, Servo4, Servo5, Servo6, Servo7, Servo8, Servo9, Servo10, Servo11, Servo12, Servo13, Servo32, Servo33, Servo34, Servo35, Servo36, Servo37, Servo38, Servo39, Servo40, Servo41, Servo42, Servo43, Servo44, Servo45, Servo46, Servo47, Servo48, Servo49, Servo50, Servo51, Servo52, Servo53;  // declare Servo objects
  73. #endif
  74.  
  75.  
  76. /*==============================================================================
  77.  * SETUP() This code runs once
  78.  *============================================================================*/
  79. void setup()
  80. {
  81.   Init();                  //set initial pinmodes
  82.   Serial.begin(BAUDRATE);  // Start Serial communication
  83. }
  84.  
  85. /*==============================================================================
  86.  * LOOP() This code loops
  87.  *============================================================================*/
  88. void loop()
  89. {
  90.   if(Serial){
  91.     ReadSerial();                       // read and parse string from serial port and write to pins
  92.     if (counter >= numcycles){          // Wait every nth loop
  93.       ReadInputs();                     // get input data
  94.       PrintToPort();                    // print data to serial port
  95.       counter = 0;                      // reset the counter
  96.     }
  97.     counter ++;                         // increment the writecounter
  98.   }
  99. }
  100.  
  101. /*==============================================================================
  102.  * FUNCTIONS()
  103.  *============================================================================*/
  104.  
  105. void Init(){
  106.   #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)    //set pin mode for STANDARD boards
  107.    
  108.    
  109.     pinMode(pingPin, OUTPUT);
  110.     digitalWrite(pingPin, LOW);
  111.     delayMicroseconds(2);
  112.     digitalWrite(pingPin, HIGH);
  113.     delayMicroseconds(5);
  114.     digitalWrite(pingPin, LOW);
  115.    
  116.       pinMode(pingPin, INPUT);
  117.     duration = pulseIn(pingPin, HIGH);
  118.  
  119.      
  120.  
  121.    // pinMode(2, INPUT);
  122.     pinMode(4, INPUT);
  123.     pinMode(7, INPUT);
  124.   #endif
  125.  
  126.   #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)   //set pin mode for LEONARDO board
  127.     pinMode(2, INPUT);
  128.     pinMode(4, INPUT);
  129.     pinMode(7, INPUT);
  130.   #endif
  131.  
  132.   #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)  //set pin mode for MEGA boards
  133.     pinMode(22, INPUT);
  134.     pinMode(23, INPUT);
  135.     pinMode(24, INPUT);
  136.     pinMode(25, INPUT);
  137.     pinMode(26, INPUT);
  138.     pinMode(27, INPUT);
  139.     pinMode(28, INPUT);
  140.     pinMode(29, INPUT);
  141.     pinMode(30, INPUT);
  142.     pinMode(31, INPUT);
  143.   #endif
  144. }
  145.  
  146. void ReadInputs(){
  147.   #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)    //read pins on STANDARD boards
  148.     APin0 = analogRead(0);  
  149.     APin1 = analogRead(1);  
  150.     APin2 = analogRead(2);
  151.     APin3 = analogRead(3);
  152.     APin4 = analogRead(4);
  153.     APin5 = analogRead(5);
  154. //    DPin2 = digitalRead(2);  
  155.     DPin4 = digitalRead(2);  
  156.     DPin7 = digitalRead(7);
  157.   #endif
  158.  
  159.   #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)   //read pins on LEONARDO board
  160.     APin0 = analogRead(0);  
  161.     APin1 = analogRead(1);  
  162.     APin2 = analogRead(2);
  163.     APin3 = analogRead(3);
  164.     APin4 = analogRead(4);
  165.     APin5 = analogRead(5);
  166.     DPin2 = digitalRead(2);  
  167.     DPin4 = digitalRead(4);  
  168.     DPin7 = digitalRead(7);
  169.   #endif
  170.  
  171.   #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)  //read pins on MEGA boards
  172.     APin0 = analogRead(0);
  173.     APin1 = analogRead(1);
  174.     APin2 = analogRead(2);
  175.     APin3 = analogRead(3);
  176.     APin4 = analogRead(4);
  177.     APin5 = analogRead(5);
  178.     APin6 = analogRead(6);
  179.     APin7 = analogRead(7);
  180.     APin8 = analogRead(8);
  181.     APin9 = analogRead(9);
  182.     APin10 = analogRead(10);
  183.     APin11 = analogRead(11);
  184.     APin12 = analogRead(12);
  185.     APin13 = analogRead(13);
  186.     APin14 = analogRead(14);
  187.     APin15 = analogRead(15);
  188.     DPin22 = digitalRead(22);  
  189.     DPin23 = digitalRead(23);  
  190.     DPin24 = digitalRead(24);  
  191.     DPin25 = digitalRead(25);  
  192.     DPin26 = digitalRead(26);
  193.     DPin27 = digitalRead(27);
  194.     DPin28 = digitalRead(28);  
  195.     DPin29 = digitalRead(29);  
  196.     DPin30 = digitalRead(30);  
  197.     DPin31 = digitalRead(31);
  198.   #endif
  199. }
  200.  
  201. void PrintToPort(){
  202.   #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)    //print formatted string for STANDARD boards
  203.     Serial.print(APin0); Serial.print(",");  
  204.     Serial.print(APin1); Serial.print(",");
  205.     Serial.print(APin2); Serial.print(",");
  206.     Serial.print(APin3); Serial.print(",");
  207.     Serial.print(APin4); Serial.print(",");
  208.     Serial.print(APin5); Serial.print(",");
  209.     Serial.print(DPin2); Serial.print(",");
  210.     Serial.print(DPin4); Serial.print(",");
  211.     Serial.print(DPin7); Serial.print(",");
  212.     Serial.println("eol");  //end of line marker
  213.   #endif
  214.  
  215.   #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)   //print formatted string for LEONARDO boards
  216.     Serial.print(APin0); Serial.print(",");  
  217.     Serial.print(APin1); Serial.print(",");
  218.     Serial.print(APin2); Serial.print(",");
  219.     Serial.print(APin3); Serial.print(",");
  220.     Serial.print(APin4); Serial.print(",");
  221.     Serial.print(APin5); Serial.print(",");
  222.     Serial.print(DPin2); Serial.print(",");
  223.     Serial.print(DPin4); Serial.print(",");
  224.     Serial.print(DPin7); Serial.print(",");
  225.     Serial.println("eol");  //end of line marker
  226.   #endif
  227.  
  228.   #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)  //print formatted string for MEGA boards
  229.     Serial.print(APin0); Serial.print(",");
  230.     Serial.print(APin1); Serial.print(",");
  231.     Serial.print(APin2); Serial.print(",");
  232.     Serial.print(APin3); Serial.print(",");  
  233.     Serial.print(APin4); Serial.print(",");
  234.     Serial.print(APin5); Serial.print(",");  
  235.     Serial.print(APin6); Serial.print(",");
  236.     Serial.print(APin7); Serial.print(",");  
  237.     Serial.print(APin8); Serial.print(",");  
  238.     Serial.print(APin9); Serial.print(",");
  239.     Serial.print(APin10); Serial.print(",");  
  240.     Serial.print(APin11); Serial.print(",");
  241.     Serial.print(APin12); Serial.print(",");
  242.     Serial.print(APin13); Serial.print(",");
  243.     Serial.print(APin14); Serial.print(",");  
  244.     Serial.print(APin15); Serial.print(",");
  245.     Serial.print(DPin22); Serial.print(",");
  246.     Serial.print(DPin23); Serial.print(",");
  247.     Serial.print(DPin24); Serial.print(",");
  248.     Serial.print(DPin25); Serial.print(",");  
  249.     Serial.print(DPin26); Serial.print(",");
  250.     Serial.print(DPin27); Serial.print(",");  
  251.     Serial.print(DPin28); Serial.print(",");
  252.     Serial.print(DPin29); Serial.print(",");
  253.     Serial.print(DPin30); Serial.print(",");
  254.     Serial.print(DPin31); Serial.print(",");  
  255.     Serial.println("eol");  //end of line marker
  256.   #endif
  257. }
  258.  
  259. void ReadSerial(){
  260.  
  261.   char c;    // holds one character from the serial port
  262.   if (Serial.available()) {
  263.     c = Serial.read();         // read one character
  264.     buffer[bufferidx] = c;     // add to buffer
  265.  
  266.     if (c == '\n') {  
  267.       buffer[bufferidx+1] = 0; // terminate it
  268.       parseptr = buffer;       // offload the buffer into temp variable
  269.  
  270.       #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)  //parse string for STANDARD boards
  271.         DPin13 = parsedecimal(parseptr);     // parse the first number
  272.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  273.        
  274.         DPin12 = parsedecimal(parseptr);     // parse the second number
  275.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  276.  
  277.         DPin11 = parsedecimal(parseptr);     // parse the third number
  278.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  279.  
  280.         DPin10 = parsedecimal(parseptr);     // parse the fourth number
  281.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  282.  
  283.         DPin9 = parsedecimal(parseptr);      // parse the fifth number
  284.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  285.  
  286.         DPin8 = parsedecimal(parseptr);      // parse the sixth number
  287.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  288.  
  289.         DPin6 = parsedecimal(parseptr);      // parse the seventh number
  290.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  291.  
  292.         DPin5 = parsedecimal(parseptr);      // parse the eighth number
  293.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  294.  
  295.         DPin3 = parsedecimal(parseptr);      // parse the ninth number
  296.        
  297.         WriteToPin(13, DPin13, Servo13);     //send value out to pin on arduino board
  298.         WriteToPin(12, DPin12, Servo12);
  299.         WriteToPin(11, DPin11, Servo11);
  300.         WriteToPin(10, DPin10, Servo10);
  301.         WriteToPin(9, DPin9, Servo9);
  302.         WriteToPin(8, DPin8, Servo8);
  303.         WriteToPin(6, DPin6, Servo6);
  304.         WriteToPin(5, DPin5, Servo5);
  305.         WriteToPin(3, DPin3, Servo3);
  306.          
  307.       #endif
  308.      
  309.       #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__)  //parse string for LEONARDO boards
  310.         DPin13 = parsedecimal(parseptr);     // parse the first number
  311.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  312.  
  313.         DPin12 = parsedecimal(parseptr);     // parse the second number
  314.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  315.  
  316.         DPin11 = parsedecimal(parseptr);     // parse the third number
  317.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  318.  
  319.         DPin10 = parsedecimal(parseptr);     // parse the fourth number
  320.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  321.  
  322.         DPin9 = parsedecimal(parseptr);      // parse the fifth number
  323.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  324.  
  325.         DPin8 = parsedecimal(parseptr);      // parse the sixth number
  326.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  327.  
  328.         DPin6 = parsedecimal(parseptr);      // parse the seventh number
  329.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  330.  
  331.         DPin5 = parsedecimal(parseptr);      // parse the eighth number
  332.         parseptr = strchr(parseptr, ',')+1;  // move past the ","
  333.  
  334.         DPin3 = parsedecimal(parseptr);      // parse the ninth number
  335.        
  336.         WriteToPin(13, DPin13, Servo13);     //send value out to pin on arduino board
  337.         WriteToPin(12, DPin12, Servo12);
  338.         WriteToPin(11, DPin11, Servo11);
  339.         WriteToPin(10, DPin10, Servo10);
  340.         WriteToPin(9, DPin9, Servo9);
  341.         WriteToPin(8, DPin8, Servo8);
  342.         WriteToPin(6, DPin6, Servo6);
  343.         WriteToPin(5, DPin5, Servo5);
  344.         WriteToPin(3, DPin3, Servo3);
  345.        
  346.       #endif
  347.      
  348.       #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)  //parse string for MEGA boards
  349.         DPin2 = parsedecimal(parseptr);       // parse the first number
  350.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  351.  
  352.         DPin3 = parsedecimal(parseptr);       // parse the second number
  353.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  354.  
  355.         DPin4 = parsedecimal(parseptr);       // parse the third number
  356.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  357.  
  358.         DPin5 = parsedecimal(parseptr);       // parse the fourth number
  359.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  360.  
  361.         DPin6 = parsedecimal(parseptr);       // parse the fifth number
  362.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  363.  
  364.         DPin7 = parsedecimal(parseptr);       // parse the sixth number
  365.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  366.  
  367.         DPin8 = parsedecimal(parseptr);       // parse the seventh number
  368.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  369.  
  370.         DPin9 = parsedecimal(parseptr);       // parse the eighth number
  371.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  372.  
  373.         DPin10 = parsedecimal(parseptr);      // parse the ninth number
  374.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  375.        
  376.         DPin11 = parsedecimal(parseptr);      // parse the tenth number
  377.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  378.        
  379.         DPin12 = parsedecimal(parseptr);      // parse the eleventh number
  380.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  381.        
  382.         DPin13 = parsedecimal(parseptr);      // parse the twelvth number
  383.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  384.        
  385.         DPin32 = parsedecimal(parseptr);      // parse the thirteenth number
  386.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  387.  
  388.         DPin33 = parsedecimal(parseptr);      // parse the fourteenth number
  389.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  390.        
  391.         DPin34 = parsedecimal(parseptr);      // parse the fifthteenth number
  392.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  393.        
  394.         DPin35 = parsedecimal(parseptr);      // parse the sixteenth number
  395.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  396.        
  397.         DPin36 = parsedecimal(parseptr);      // parse the seventeenth number
  398.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  399.        
  400.         DPin37 = parsedecimal(parseptr);      // parse the eightteenth number
  401.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  402.        
  403.         DPin38 = parsedecimal(parseptr);      // parse the nineteenth number
  404.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  405.        
  406.         DPin39 = parsedecimal(parseptr);      // parse the twentieth number
  407.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  408.        
  409.         DPin40 = parsedecimal(parseptr);      // parse the twenty first number
  410.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  411.        
  412.         DPin41 = parsedecimal(parseptr);      // parse the twenty second number
  413.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  414.        
  415.         DPin42 = parsedecimal(parseptr);      // parse the twenty third number
  416.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  417.        
  418.         DPin43 = parsedecimal(parseptr);      // parse the twenty fourth number
  419.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  420.        
  421.         DPin44 = parsedecimal(parseptr);      // parse the twenty fifth number
  422.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  423.        
  424.         DPin45 = parsedecimal(parseptr);      // parse the twenty sixth number
  425.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  426.        
  427.         DPin46 = parsedecimal(parseptr);      // parse the twenty seventh number
  428.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  429.        
  430.         DPin47 = parsedecimal(parseptr);      // parse the twenty eigth number
  431.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  432.        
  433.         DPin48 = parsedecimal(parseptr);      // parse the twenty ninth number
  434.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  435.        
  436.         DPin49 = parsedecimal(parseptr);      // parse the thirtieth number
  437.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  438.        
  439.         DPin50 = parsedecimal(parseptr);      // parse the thirty one number
  440.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  441.        
  442.         DPin51 = parsedecimal(parseptr);      // parse the thirty second number
  443.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  444.        
  445.         DPin52 = parsedecimal(parseptr);      // parse the thirty third number
  446.         parseptr = strchr(parseptr, ',')+1;   // move past the ","
  447.        
  448.         DPin53 = parsedecimal(parseptr);      // parse the thirty fourth number
  449.        
  450.         WriteToPin(2, DPin2, Servo2);         //send value out to pin on arduino board
  451.         WriteToPin(3, DPin3, Servo3);
  452.         WriteToPin(4, DPin4, Servo4);
  453.         WriteToPin(5, DPin5, Servo5);
  454.         WriteToPin(6, DPin6, Servo6);
  455.         WriteToPin(7, DPin7, Servo7);
  456.         WriteToPin(8, DPin8, Servo8);
  457.         WriteToPin(9, DPin9, Servo9);
  458.         WriteToPin(10, DPin10, Servo10);
  459.         WriteToPin(11, DPin11, Servo11);
  460.         WriteToPin(12, DPin12, Servo12);
  461.         WriteToPin(13, DPin13, Servo13);
  462.         WriteToPin(32, DPin32, Servo32);
  463.         WriteToPin(33, DPin33, Servo33);
  464.         WriteToPin(34, DPin34, Servo34);
  465.         WriteToPin(35, DPin35, Servo35);
  466.         WriteToPin(36, DPin36, Servo36);
  467.         WriteToPin(37, DPin37, Servo37);
  468.         WriteToPin(38, DPin38, Servo38);
  469.         WriteToPin(39, DPin39, Servo39);
  470.         WriteToPin(40, DPin40, Servo40);
  471.         WriteToPin(41, DPin41, Servo41);
  472.         WriteToPin(42, DPin42, Servo42);
  473.         WriteToPin(43, DPin43, Servo43);
  474.         WriteToPin(44, DPin44, Servo44);
  475.         WriteToPin(45, DPin45, Servo45);
  476.         WriteToPin(46, DPin46, Servo46);
  477.         WriteToPin(47, DPin47, Servo47);
  478.         WriteToPin(48, DPin48, Servo48);
  479.         WriteToPin(49, DPin49, Servo49);
  480.         WriteToPin(50, DPin50, Servo50);
  481.         WriteToPin(51, DPin51, Servo51);
  482.         WriteToPin(52, DPin52, Servo52);
  483.         WriteToPin(53, DPin53, Servo53);
  484.        
  485.       #endif
  486.      
  487.       bufferidx = 0;                             // reset the buffer for the next read
  488.       return;                                    // return so that we don't trigger the index increment below
  489.     }                                            // didn't get newline, need to read more from the buffer
  490.     bufferidx++;                                 // increment the index for the next character
  491.     if (bufferidx == BUFFSIZE-1) bufferidx = 0;  // if we get to the end of the buffer reset for safety
  492.   }
  493. }
  494.  
  495. void WriteToPin(int _pin, int _value, Servo _servo){
  496. if (_value >= 1000 && _value < 2000)             // check if value should be used for Digital Write (HIGH/LOW)
  497. {      
  498.   if (_servo.attached()) _servo.detach();        // detach servo is one is attached to pin
  499.   pinMode(_pin, OUTPUT);                         // sets the pin for output
  500.   _value -=1000;                                 // subtract 1000 from the value sent from Firefly
  501.   if (_value == 1) digitalWrite(_pin, HIGH);     // Digital Write to pin
  502.   else digitalWrite(_pin, LOW);  
  503. }  
  504. else if (_value >= 2000 && _value < 3000)        // check if value should be used for Analog Write (0-255)
  505. {
  506.   if (_servo.attached()) _servo.detach();        // detach servo is one is attached to pin
  507.   pinMode(_pin, OUTPUT);                         // sets the pin for output
  508.   _value -= 2000;                                // subtract 2000 from the value sent from Firefly
  509.   analogWrite(_pin, _value);                     // Analog Write to pin
  510. }
  511. else if (_value >= 3000 && _value < 4000)        // check if value should be used for Servo Write (0-180)
  512. {
  513.   _value -= 3000;                                // subtract 3000 from the value sent from Firefly
  514.   if (!_servo.attached())_servo.attach(_pin);    // attaches a Servo to the PWM pin (180 degree standard servos)                                    
  515.   _servo.write(_value);                          // Servo Write to the pin
  516.   }
  517. }
  518.  
  519. uint32_t parsedecimal(char *str){
  520.   uint32_t d = 0;
  521.   while (str[0] != 0) {
  522.     if ((str[0] > '50') || (str[0] < '0'))
  523.       return d;
  524.     d *= 10;
  525.     d += str[0] - '0';
  526.     str++;
  527.   }
  528.   return d;
  529. }
Advertisement
Add Comment
Please, Sign In to add comment