Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <Servo.h>
  2. #include <compat/deprecated.h>
  3. #include <FlexiTimer2.h>
  4.  
  5. #define SAMPFREQ 256 // ADC sampling rate 256
  6. #define TIMER2VAL (1024/(SAMPFREQ)) // Set 256Hz sampling frequency
  7. volatile unsigned char CurrentCh=0; //Current channel being sampled.
  8. volatile unsigned int ADC_Value = 0; //ADC current value
  9. volatile unsigned int ADC_Value1 = 0; //ADC current value
  10. Servo myServo1;
  11. Servo myServo2;
  12. Servo myServo3;
  13. Servo myServo4;
  14. Servo myServo5;
  15. Servo myServo6;int pos = 0; // variable to store the servo position
  16. int modo = 0;
  17. int oscMsg[12]; // buffer for incoming OSc packet
  18. int inByte = 0; // incomming serial byte
  19. int inbyteIndex=0; // incomming bytes counter
  20. float msgVal=0; // resulting message value
  21.  
  22. void setup()
  23. {
  24. Serial.begin(57600); // debug port
  25. myServo1.attach(13); // pulso
  26. myServo2.attach(8); //dedo mindinho
  27. myServo3.attach(9); //dedo2
  28. myServo4.attach(10); //dedo3
  29. myServo5.attach(11, 576, 2384); //indicador
  30. myServo6.attach(12); //poegar
  31. }
  32.  
  33. void loop(){
  34. int value99 = 0; //contador de numero de vezes
  35. int value98 = 0; //contador de ups
  36. while (value99 &lt; 500) { //Here finds the muscle pattern.
  37. if (Serial.available()) { // if we have new byte from LAN
  38. if (getOscMsg()==1){ // add it to message while is is not ready
  39. if (byte(oscMsg[1]) != 32){ //para evitar o envio de mensagem vazia Serial.print(oscMsg[1]); // or print the OSC message name Serial.print(" / "); Serial.println(msgVal); // and vaule int value = msgVal; switch(oscMsg[1]){ case'a': if(value &gt; 0 &amp;&amp; value &lt;= 180){ modo = 1; //variavel para determinar o movimento Serial.print("MODO "); Serial.println(modo); value = 0; break; } case'b': if(value &gt; 0 &amp;&amp; value &lt;= 180){ modo = 2; //variavel para determinar o movimento Serial.print("MODO "); Serial.println(modo); value = 0; break; } case'c': if(value &gt; 0 &amp;&amp; value &lt;= 180){ modo = 3; //variavel para determinar o movimento Serial.print("MODO "); Serial.println(modo); value = 0; break; } case'd': if(value &gt; 0 &amp;&amp; value &lt;= 180){ modo = 4; //variavel para determinar o movimento Serial.print("MODO "); Serial.println(modo); value = 0; break; } case'e': if(value &gt; 0 &amp;&amp; value &lt;= 180){ modo = 5; //variavel para determinar o movimento Serial.print("MODO "); Serial.println(modo); value = 0; break; } case'f': if(value &gt; 0 &amp;&amp; value &lt;= 180){ modo = 6; //variavel para determinar o movimento Serial.print("MODO "); Serial.println(modo); value = 0; break; } } } } } delay(1); ADC_Value = analogRead(CurrentCh); value99 = value99+1; if(ADC_Value &gt; 400){
  40. value98 = value98 + 1; //caso tenha o valor alto, adiciona um no contador
  41. }
  42. if(ADC_Value &lt; 250){
  43. value98 = value98 + 1; //caso tenha o valor alto, adiciona um no contador
  44. }
  45. }
  46. if (value98 &lt; 25){ //off Serial.print("off"); //this is packet header for my application Serial.println(ADC_Value); myServo1.write(90); myServo2.write(10); myServo3.write(10); myServo4.write(10); myServo5.write(10); myServo6.write(10); delay(1); } else if (value98 &gt;= 45){ //on
  47. Serial.print("on"); //this is packet header for my application
  48. Serial.println(ADC_Value);
  49. Serial.println(value98);
  50. if(modo &lt;= 1){
  51. Serial.print("MODO ");
  52. Serial.println(modo);
  53. myServo1.write(90);
  54. myServo2.write(150); // fechado
  55. myServo3.write(150);
  56. myServo4.write(150);
  57. myServo5.write(150);
  58. myServo6.write(150);
  59. delay(1);
  60. }
  61. if(modo ==2){
  62. Serial.print("MODO ");
  63. Serial.println(modo);
  64. myServo1.write(90);
  65. myServo2.write(10); // pinca
  66. myServo3.write(10);
  67. myServo4.write(10);
  68. myServo5.write(140);
  69. myServo6.write(150);
  70. delay(1);
  71. }
  72. if(modo ==3){
  73. Serial.print("MODO ");
  74. Serial.println(modo);
  75. myServo1.write(90);
  76. myServo2.write(150); // aponta
  77. myServo3.write(150);
  78. myServo4.write(150);
  79. myServo5.write(15);
  80. myServo6.write(20);
  81. delay(1);
  82. }
  83. if(modo ==4){
  84. Serial.print("MODO ");
  85. Serial.println(modo);
  86. myServo1.write(90);
  87. myServo2.write(15); // Spider
  88. myServo3.write(150);
  89. myServo4.write(150);
  90. myServo5.write(15);
  91. myServo6.write(20);
  92. delay(1);
  93. }
  94. if(modo ==5){
  95. Serial.print("MODO ");
  96. Serial.println(modo);
  97. myServo1.write(90);
  98. myServo2.write(150); // Joia
  99. myServo3.write(150);
  100. myServo4.write(150);
  101. myServo5.write(150);
  102. myServo6.write(20);
  103. delay(1);
  104. }
  105. if(modo ==6){
  106. Serial.print("MODO ");
  107. Serial.println(modo);
  108. myServo1.write(90);
  109. myServo2.write(15); // hang
  110. myServo3.write(150);
  111. myServo4.write(150);
  112. myServo5.write(150);
  113. myServo6.write(20);
  114. delay(1);
  115. }
  116. }
  117. }
  118.  
  119. // function to process simple OSC message sent by TouchOSC for iphone / ipad
  120. float getOscMsg(){
  121. inByte=Serial.read(); // read next serial byte
  122.  
  123. if (inByte == 47){ // if byte = slash it's message start
  124. inbyteIndex=0; // and we set array pointer to 0
  125. }
  126.  
  127. if ((inbyteIndex &lt;= 11) &amp;&amp; (inbyteIndex &gt;= 0)){ // is it time to finish or can we start?
  128. oscMsg[inbyteIndex]=inByte; // we add the byte to the array
  129. inbyteIndex++; // increase array counter
  130. }
  131.  
  132. if (inbyteIndex == 11){ // end of the message
  133. inbyteIndex=-1; // set the pointer to -1 so we stop processing
  134.  
  135. union u_tag { // this is array to float conversation routine
  136. byte bytes[4]; // I copied from Arduino.cc forum
  137. float buffer;
  138. }
  139.  
  140. u;
  141. u.bytes[0]=oscMsg[11]; // to decode we have to supply bytes inr everse order
  142. u.bytes[1]=oscMsg[10];
  143. u.bytes[2]=oscMsg[9];
  144. u.bytes[3]=oscMsg[8];
  145. msgVal = u.buffer; // byte array to float
  146. return 1; // signal we are ready to display value
  147. }
  148. return 0; // in this case the message is not ready yet
  149. }