Paso 5: codificación
Código: - para el transmisor
int sen0 = A0; eje del acelerómetro x
int sen1 = A1; eje y del acelerómetro
int sen2 = A2; Flex sensor
void setup()
{
Serial.Begin(9600);
}
void loop()
{
val0 int = map(analogRead(sen0), 450, 100, 260, 210); asignar los valores entrante en requried gama Serial.print(val0); enviar datos a través de Xbee
int val1 = map(analogRead(sen1), 450, 100, 260, 210);
Serial.Print(val1);
int val2 = map(analogRead(sen2), 450, 620, 100, 210);
Serial.Print(val2);
Delay(50); retardo de 50 ms
}
.
.
.
.
Código - receptor
#include < Servo.h >
int servo0Pin = 6;
int servo1Pin = 7;
int servo2Pin = 8;
Servo myservo0;
Servo myservo1;
Servo myservo2;
val0 int = 2;
int val1 = 3;
int val2 = 4;
int val3 = 5;
int buttonState0 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int out0 = 9;
out1 int = 10;
out2 int = 11;
int out3 = 12;
int d, i, n;
int u, v, w;
int DÚltimo (DLast), nlast, ilast;
void setup()
{
Serial.Begin(9600);
pinMode (out0, salida);
pinMode (out1, salida);
pinMode (out2, salida);
pinMode (out3, salida);
pinMode (val0, entrada);
pinMode (val1, entrada);
pinMode (val2, entrada);
pinMode (val3, entrada);
myservo0.Attach(servo0Pin);
myservo1.Attach(servo1Pin);
myservo2.Attach(servo2Pin);
}
void loop()
{
while(serial.Available() == 0);
int datos = Serial.read() - '0';
byte a = Serial.read();
byte b = Serial.read();
c bytes = Serial.read();
f bytes = Serial.read();
g bytes = Serial.read();
h byte = Serial.read();
k bytes = Serial.read();
l byte = Serial.read();
bytes m = Serial.read();
d = (c-48) + (b-48) * 10 + (a-48) * 100;
d = mapa (d, 100, 210, 20, 130);
d = restringir (d, 20, 130);
i = (h-48) + (g-48) * 10 + (f-48) * 100;
i = mapa (i, 100, 210, 20, 130);
i = restringir (i, 20, 130);
n = (m-48) + (l-48) * 10 + (k-48) * 100;
n = mapa (n, 210, 20, 100, 130);
n = restringir (n, 20, 130);
Si (d < 130 & & d > 20)
{
Si (DÚltimo (DLast)-d < = 20)
{
Si (u
{
u = u + 2;
}
Si (u == d)
{
u = u;
}
Si (u > d)
{
u = u-2;
}
myservo0.Write(u);
Serial.Print(u);
Serial.Print ("\t");
}
}
Si (i < 130 & & i > 20)
{
Si (ilast-i < = 20)
{
Si (v
{
v = v + 2;
}
Si (v == i)
{
v = v;
}
Si (v > i)
{
v = v-2;
}
myservo1.Write(v);
Serial.Print(v);
Serial.Print ("\t");
}
}
Si (n < 130 & & n > 20)
{
Si (nlast-n < = 20)
{
Si (w
{
w = w + 2;
}
if(w==n)
{
w = w;
}
Si (w > n)
{
w = w-2;
}
myservo2.Write(w);
Serial.Print(w);
Serial.Print ("\t");
}
}
Serial.println();
Serial.Flush();
int d = (int);
int e = (int) b;
int f = (int) c;
DÚltimo (DLast) = d;
ilast = i;
nlast = n;
buttonState0 = digitalRead(val0);
buttonState1 = digitalRead(val1);
buttonState2 = digitalRead(val2);
buttonState3 = digitalRead(val3);
Si (buttonState3 == baja & & buttonState2 == baja & & buttonState1 == alto & & buttonState0 == LOW) {}
digitalWrite (out0, HIGH);
digitalWrite (out1, LOW);
digitalWrite (out2, LOW);
digitalWrite (out3, HIGH);
}
Si (buttonState3 == baja & & buttonState2 == alto & & buttonState1 == baja & & buttonState0 == LOW) {}
digitalWrite (out0, HIGH);
digitalWrite (out1, LOW);
digitalWrite (out2, LOW);
digitalWrite (out3, LOW);
}
Si (buttonState3 == baja & & buttonState2 == alta & & buttonState1 == alto & & buttonState0 == LOW) {}
digitalWrite (out0, LOW);
digitalWrite (out1, LOW);
digitalWrite (out2, LOW);
digitalWrite (out3, HIGH);
}
Si (buttonState3 == alto & & buttonState2 == baja & & buttonState1 == baja & & buttonState0 == LOW) {}
digitalWrite (out0, LOW);
digitalWrite (out1, HIGH);
digitalWrite (out2, HIGH);
digitalWrite (out3, LOW);
}
Si (buttonState3 == baja & & buttonState2 == alto & & buttonState1 == baja & & buttonState0 == HIGH) {}
digitalWrite (out0, LOW);
digitalWrite (out1, LOW);
digitalWrite (out2, LOW);
digitalWrite (out3, LOW);
}
Delay(50);
}