|
1 |
| -#include <Servo.h> |
| 1 | +#include <Adafruit_PWMServoDriver.h> |
2 | 2 |
|
3 |
| -Servo myservo; |
4 |
| -int received {0}; |
| 3 | +#define SERVOMIN 110 |
| 4 | +#define SERVOMAX 512 |
5 | 5 |
|
6 |
| -void setup() { |
7 |
| - pinMode(2, INPUT_PULLUP); |
8 |
| - pinMode(3, INPUT_PULLUP); |
9 |
| - pinMode(4, INPUT_PULLUP); |
10 |
| - pinMode(5, INPUT_PULLUP); |
11 |
| - pinMode(6, INPUT_PULLUP); |
12 |
| - pinMode(7, INPUT_PULLUP); |
| 6 | +Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); |
| 7 | + |
| 8 | +int convert2pulse(int ang) { |
| 9 | + int pulse = map(ang, 0, 180, SERVOMIN, SERVOMAX); |
| 10 | + return pulse; |
| 11 | +} |
13 | 12 |
|
14 |
| - myservo.attach(2); |
15 |
| - // 500 - 2500 |
16 |
| - myservo.writeMicroseconds(1000); |
17 |
| - delayMicroseconds(1000); |
| 13 | +void setup() { |
18 | 14 |
|
19 | 15 | Serial.begin(9600);
|
20 |
| - Serial.print("Enter microceconds: "); |
| 16 | + Serial.println("Servo test! please input angle: "); |
| 17 | + |
| 18 | + pwm.begin(); |
| 19 | + pwm.setPWMFreq(50); |
| 20 | + delay(50); |
21 | 21 | }
|
22 | 22 |
|
23 | 23 | void loop() {
|
24 |
| - // put your main code here, to run repeatedly: |
25 |
| - if (Serial.available() > 0) { |
26 |
| - // read the incoming byte: |
27 |
| - received = Serial.parseInt(); |
28 |
| - if (received > 0) { |
29 |
| - // say what you got: |
30 |
| - Serial.println("I received: "); |
31 |
| - Serial.println(received); |
32 |
| - myservo.writeMicroseconds(received); |
33 |
| - delay(1000); |
| 24 | + String inString = ""; |
| 25 | + while (Serial.available() > 0) { |
| 26 | + char inChar = Serial.read(); |
| 27 | + if (inChar != "\n") { |
| 28 | + inString += (char)inChar; |
34 | 29 | }
|
| 30 | + delay(10); |
| 31 | + } |
| 32 | + if (inString != "") { |
| 33 | + Serial.print("I received: "); |
| 34 | + Serial.println(inString); |
| 35 | + pwm.setPWM(0, 0, convert2pulse(inString.toInt())); |
35 | 36 | }
|
36 | 37 | }
|
0 commit comments