movable_sd_speaker: SDspeaker_ProMini.ino

File SDspeaker_ProMini.ino, 1.6 KB (added by soumura, 7 years ago)
Line 
1#include <Servo.h>
2Servo myservo[2];
3char rcvData = 0;
4int servo_angle[2];
5void setup()
6{
7 myservo[0].attach(3);
8 myservo[1].attach(7);
9 Serial.begin(9600);
10 myservo[0].write(90);
11 myservo[1].write(90);
12}
13
14void loop()
15{
16 servo_angle[0] = 0;
17 servo_angle[1] = 0;
18 if(Serial.available() > 0){
19 rcvData = Serial.read();
20 if(rcvData == 'x'){
21 while(!Serial.available()){}
22 rcvData = Serial.read() - '0';
23 servo_angle[0] = rcvData * 100;
24 while(!Serial.available()){}
25 rcvData = Serial.read() - '0';
26 servo_angle[0] += rcvData * 10;
27 while(!Serial.available()){}
28 rcvData = Serial.read() - '0';
29 servo_angle[0] += rcvData;
30
31 if(servo_angle[0] >= 0 && servo_angle[0] <= 180){
32 myservo[0].write(servo_angle[0]);
33 Serial.print("SERVO X ");
34 Serial.print(servo_angle[0]);
35 }
36 }
37 else if(rcvData == 'y'){
38 while(!Serial.available()){}
39 rcvData = Serial.read() - '0';
40 servo_angle[1] = rcvData * 100;
41 while(!Serial.available()){}
42 rcvData = Serial.read() - '0';
43 servo_angle[1] += rcvData * 10;
44 while(!Serial.available()){}
45 rcvData = Serial.read() - '0';
46 servo_angle[1] += rcvData;
47
48 if(servo_angle[1] >= 0 && servo_angle[1] <= 180){
49 myservo[1].write(servo_angle[1]);
50 Serial.print("SERVO Y ");
51 Serial.println(servo_angle[1]);
52 }
53 }
54 }
55}