Sei sulla pagina 1di 13

#include <Servo.

h>
#include <compat/deprecated.h>
#include <FlexiTimer2.h>
#define SAMPFREQ 256

// ADC sampling rate 256

#define TIMER2VAL (1024/(SAMPFREQ))


volatile unsigned char CurrentCh=0;
volatile unsigned int ADC_Value = 0;

// Set 256Hz sampling frequency

//Current channel being sampled.


//ADC current value

volatile unsigned int ADC_Value1 = 0;

//ADC current value

Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;
Servo myServo5;
Servo myServo6;

int pos = 0;

// variable to store the servo position

int modo = 0;
int oscMsg[12];
int inByte = 0;

// buffer for incoming OSc packet


// incomming serial byte

int inbyteIndex=0; // incomming bytes counter


float msgVal=0;

// resulting message value

void setup()
{

Serial.begin(57600); // debug port

myServo1.attach(13); // pulso
myServo2.attach(8); //dedo mindinho
myServo3.attach(9); //dedo2
myServo4.attach(10); //dedo3
myServo5.attach(11, 576, 2384); //indicador
myServo6.attach(12); //poegar

}
void loop(){

int value99 = 0; //contador de numero de vezes


int value98 = 0;

//contador de ups

while (value99 < 500) { //Here finds the muscle pattern.

if (Serial.available()) {

if (getOscMsg()==1){

// if we have new byte from LAN

// add it to message while is is not ready

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 > 0 && value <= 180){


modo = 1;

//variavel para determinar o movimento

Serial.print("MODO ");
Serial.println(modo);
value = 0;
break;

case'b':

if(value > 0 && value <= 180){


modo = 2;

//variavel para determinar o movimento

Serial.print("MODO ");
Serial.println(modo);
value = 0;
break;
}

case'c':

if(value > 0 && value <= 180){


modo = 3;

//variavel para determinar o movimento

Serial.print("MODO ");
Serial.println(modo);
value = 0;
break;
}
case'd':

if(value > 0 && value <= 180){


modo = 4;

//variavel para determinar o movimento

Serial.print("MODO ");
Serial.println(modo);
value = 0;
break;
}
case'e':

if(value > 0 && value <= 180){


modo = 5;

//variavel para determinar o movimento

Serial.print("MODO ");
Serial.println(modo);
value = 0;
break;
}

case'f':

if(value > 0 && value <= 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 > 400){
value98 = value98 + 1; //caso tenha o valor alto, adiciona um no contador
}
if(ADC_Value < 250){

value98 = value98 + 1; //caso tenha o valor alto, adiciona um no contador


}
}

if (value98 < 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 >= 45){ //on
Serial.print("on");

//this is packet header for my application

Serial.println(ADC_Value);
Serial.println(value98);

if(modo <= 1){


Serial.print("MODO ");
Serial.println(modo);
myServo1.write(90);
myServo2.write(150); // fechado
myServo3.write(150);
myServo4.write(150);
myServo5.write(150);
myServo6.write(150);
delay(1);
}
if(modo ==2){
Serial.print("MODO ");
Serial.println(modo);
myServo1.write(90);
myServo2.write(10); // pinca
myServo3.write(10);
myServo4.write(10);
myServo5.write(140);

myServo6.write(150);
delay(1);
}
if(modo ==3){
Serial.print("MODO ");
Serial.println(modo);
myServo1.write(90);
myServo2.write(150); // aponta
myServo3.write(150);
myServo4.write(150);
myServo5.write(15);
myServo6.write(20);
delay(1);
}
if(modo ==4){
Serial.print("MODO ");
Serial.println(modo);
myServo1.write(90);
myServo2.write(15); // Spider
myServo3.write(150);
myServo4.write(150);
myServo5.write(15);

myServo6.write(20);
delay(1);
}

if(modo ==5){
Serial.print("MODO ");
Serial.println(modo);
myServo1.write(90);
myServo2.write(150); // Joia
myServo3.write(150);
myServo4.write(150);
myServo5.write(150);
myServo6.write(20);
delay(1);
}
if(modo ==6){
Serial.print("MODO ");
Serial.println(modo);
myServo1.write(90);
myServo2.write(15); // hang
myServo3.write(150);
myServo4.write(150);

myServo5.write(150);
myServo6.write(20);
delay(1);
}

// function to process simple OSC message sent by TouchOSC for iphone / ipad
float getOscMsg(){
inByte=Serial.read();
if (inByte == 47){
inbyteIndex=0;

// read next serial byte


// if byte = slash it's message start
// and we set array pointer to 0

}
if ((inbyteIndex <= 11) && (inbyteIndex >= 0)){ // is it time to finish or can we start?
oscMsg[inbyteIndex]=inByte;
inbyteIndex++;

// we add the byte to the array

// increase array counter

}
if (inbyteIndex == 11){
inbyteIndex=-1;

// end of the message


// set the pointer to -1 so we stop processing

union u_tag {

// this is array to float conversation routine

byte bytes[4];

// I copied from Arduino.cc forum

float buffer;
}
u;
u.bytes[0]=oscMsg[11];

// to decode we have to supply bytes inr everse order

u.bytes[1]=oscMsg[10];
u.bytes[2]=oscMsg[9];
u.bytes[3]=oscMsg[8];
msgVal = u.buffer;
return 1;

// byte array to float


// signal we are ready to display value

}
return 0;
}

// in this case the message is not ready yet

Potrebbero piacerti anche