|
| 1 | +// This sketch demonstrates how to command 2 servos through the USB of the Digispark. |
| 2 | +// It uses: |
| 3 | +// - <SoftRcPulseOut> library to easily generates the RC pulses for the servos. |
| 4 | +// - <DigiUSB> library to communicate with the PC |
| 5 | +// By RC Navy (http://p.loussouarn.free.fr) |
| 6 | + |
| 7 | +// The command (issued in the DigiUSB Monitor or the digiterm) is: |
| 8 | +// S=P with: |
| 9 | +// S=A for ServoA and S=B for ServoB |
| 10 | +// P=Position number x 20° (Possible positions are from 0 to 9 which correspond to from 0° to 180°) |
| 11 | +// Ex: |
| 12 | +// A=7 sets Servo1 at 7 x 20 =140° |
| 13 | +// B=3 sets Servo2 at 3 x 20 =60° |
| 14 | +// Once the servo selected, just type the value between 0 and 9 |
| 15 | +// Please, note this sketch is derived from the SerialServo example of <SoftwareServo> library. |
| 16 | + |
| 17 | +#include <DigiUSB.h> |
| 18 | +#include <SoftRcPulseOut.h> |
| 19 | + |
| 20 | +#define LED_PIN 1 /* Builtin Led on Rev2 ModelA Digispark */ |
| 21 | +#define SERVO_A_PIN 2 |
| 22 | +/* /!\ Do not use Pin 3 (used by USB) /!\ */ |
| 23 | +/* /!\ Do not use Pin 4 (used by USB) /!\ */ |
| 24 | +#define SERVO_B_PIN 5 |
| 25 | + |
| 26 | +SoftRcPulseOut ServoA; |
| 27 | +SoftRcPulseOut ServoB; |
| 28 | + |
| 29 | + |
| 30 | +void setup() |
| 31 | +{ |
| 32 | + pinMode(LED_PIN,OUTPUT); |
| 33 | + ServoA.attach(SERVO_A_PIN); |
| 34 | + ServoB.attach(SERVO_B_PIN); |
| 35 | + DigiUSB.begin(); |
| 36 | + DigiUSB.println(" Ready"); |
| 37 | +} |
| 38 | + |
| 39 | +void loop() |
| 40 | +{ |
| 41 | + static int value = 0; |
| 42 | + static char CurrentServo = 0; |
| 43 | + |
| 44 | + if ( DigiUSB.available()) { |
| 45 | + char ch = DigiUSB.read(); |
| 46 | + switch(ch) { |
| 47 | + case 'A': |
| 48 | + CurrentServo='A'; |
| 49 | + digitalWrite(LED_PIN,LOW); |
| 50 | + break; |
| 51 | + case 'B': |
| 52 | + CurrentServo='B'; |
| 53 | + digitalWrite(LED_PIN,HIGH); |
| 54 | + break; |
| 55 | + case '0' ... '9': |
| 56 | + value=(ch-'0')*20; |
| 57 | + if (CurrentServo=='A') |
| 58 | + { |
| 59 | + ServoA.write(value); |
| 60 | + } |
| 61 | + else if (CurrentServo=='B') |
| 62 | + { |
| 63 | + ServoB.write(value); |
| 64 | + } |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + DigiUSB.refresh(); |
| 69 | + SoftRcPulseOut::refresh(); |
| 70 | + /* |
| 71 | + Put here your non-blocking code |
| 72 | + */ |
| 73 | +} |
0 commit comments