Skip to content

Commit 4793a50

Browse files
committed
make more friendly with ESP8266 & Zero
1 parent 8846078 commit 4793a50

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

Adafruit_PWMServoDriver.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
#include <Adafruit_PWMServoDriver.h>
1919
#include <Wire.h>
20-
#if defined(__AVR__)
21-
#define WIRE Wire
22-
#elif defined(CORE_TEENSY) // Teensy boards
23-
#define WIRE Wire
24-
#else // Arduino Due
20+
#if defined(ARDUINO_SAM_DUE)
2521
#define WIRE Wire1
22+
#else
23+
#define WIRE Wire
2624
#endif
2725

26+
2827
// Set to true to print some debug messages, or false to disable them.
2928
#define ENABLE_DEBUG_OUTPUT false
3029

examples/pwmtest/pwmtest.pde

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,31 @@ Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
2424
// you can also call it with a different address you want
2525
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
2626

27+
#if defined(ARDUINO_ARCH_SAMD)
28+
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
29+
#define Serial SerialUSB
30+
#endif
31+
2732
void setup() {
33+
#ifdef ESP8266
34+
Wire.pins(2, 14); // ESP8266 can use any two pins, such as SDA to #2 and SCL to #14
35+
#endif
36+
2837
Serial.begin(9600);
2938
Serial.println("16 channel PWM test!");
3039

40+
pwm.begin();
41+
pwm.setPWMFreq(1600); // This is the maximum PWM frequency
42+
3143
// if you want to really speed stuff up, you can go into 'fast 400khz I2C' mode
3244
// some i2c devices dont like this so much so if you're sharing the bus, watch
3345
// out for this!
34-
35-
pwm.begin();
36-
pwm.setPWMFreq(1600); // This is the maximum PWM frequency
37-
46+
#ifdef TWBR
3847
// save I2C bitrate
3948
uint8_t twbrbackup = TWBR;
4049
// must be changed after calling Wire.begin() (inside pwm.begin())
4150
TWBR = 12; // upgrade to 400KHz!
42-
51+
#endif
4352
}
4453

4554
void loop() {
@@ -49,4 +58,4 @@ void loop() {
4958
pwm.setPWM(pwmnum, 0, (i + (4096/16)*pwmnum) % 4096 );
5059
}
5160
}
52-
}
61+
}

examples/servo/servo.pde

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ void setup() {
3838
Serial.begin(9600);
3939
Serial.println("16 channel Servo test!");
4040

41+
#ifdef ESP8266
42+
Wire.pins(2, 14); // ESP8266 can use any two pins, such as SDA to #2 and SCL to #14
43+
#endif
44+
4145
pwm.begin();
4246

4347
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
48+
49+
yield();
4450
}
4551

4652
// you can use this function if you'd like to set the pulse length in seconds
@@ -65,12 +71,14 @@ void loop() {
6571
for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
6672
pwm.setPWM(servonum, 0, pulselen);
6773
}
74+
6875
delay(500);
6976
for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
7077
pwm.setPWM(servonum, 0, pulselen);
7178
}
79+
7280
delay(500);
7381

7482
servonum ++;
75-
if (servonum > 15) servonum = 0;
83+
if (servonum > 7) servonum = 0;
7684
}

0 commit comments

Comments
 (0)