Skip to content

Commit da50217

Browse files
committed
add switch mode button example
1 parent 3b2a022 commit da50217

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Using a button to switch between different modes and control the animation.
3+
4+
In this example we are using the OneButton library to handle both short and
5+
long button presses. Note that the animation will be in default mode first,
6+
meaning that the servo will not move until the button is pressed shortly to
7+
start the animation. See the comments below to understand what happens when
8+
pressing the button short or long according to the current mode.
9+
*/
10+
11+
#include "simple.h"
12+
#include <BlenderServoAnimation.h>
13+
#include <OneButton.h>
14+
#include <Servo.h>
15+
16+
// Frames per second - see original Blender animation / simple.h
17+
#define FPS 30
18+
19+
// Total animation frames - see original Blender animation / simple.h
20+
#define FRAMES 100
21+
22+
// The button instance for switching animation modes
23+
OneButton modeButton(2, false, false);
24+
25+
// Servo object to send positions
26+
Servo myServo;
27+
28+
// Callback function which is called whenever a servo needs to be moved
29+
void move(byte servoID, int angle) {
30+
// Ignore the servoID (there is only one servo) and write the current angle
31+
myServo.write(angle);
32+
}
33+
34+
// Animation object to represent the original Blender animation
35+
BlenderServoAnimation::Animation animation(FPS, FRAMES);
36+
37+
// Servo object to manage the positions
38+
BlenderServoAnimation::Servo myBlenderServo(0, Bone, move);
39+
40+
// Callback to be triggered on a short button press
41+
void onPressed() {
42+
// Get the current mode, act accordingly and trigger another mode
43+
switch (animation.getMode()) {
44+
// On short press in default mode, we want to start the animation
45+
case BlenderServoAnimation::Animation::MODE_DEFAULT:
46+
// Hint: here you could also trigger some audio
47+
animation.play();
48+
break;
49+
// On short press in play mode, we want to pause the animation
50+
case BlenderServoAnimation::Animation::MODE_PLAY:
51+
animation.pause();
52+
// Hint: here you could also pause some audio
53+
break;
54+
// On short press in pause mode, we want to resume the animation
55+
case BlenderServoAnimation::Animation::MODE_PAUSE:
56+
// Hint: here you could also resume some audio
57+
animation.play();
58+
break;
59+
}
60+
}
61+
62+
// Callback to be triggered on a long button press
63+
void onLongPressed() {
64+
// Get the current mode, act accordingly and trigger another mode
65+
switch (animation.getMode()) {
66+
// On long press in default mode, we want to trigger the live mode
67+
case BlenderServoAnimation::Animation::MODE_DEFAULT:
68+
animation.live(Serial);
69+
break;
70+
// On long press in any other mode, we want to stop the animation
71+
case BlenderServoAnimation::Animation::MODE_PLAY:
72+
case BlenderServoAnimation::Animation::MODE_PAUSE:
73+
case BlenderServoAnimation::Animation::MODE_LIVE:
74+
animation.stop();
75+
break;
76+
}
77+
}
78+
79+
void setup() {
80+
// Attach the servo to pin 9
81+
myServo.attach(9);
82+
83+
// Attach the callbacks to the mode button
84+
modeButton.attachClick(onPressed);
85+
modeButton.attachLongPressStart(onLongPressed);
86+
87+
// Add the Blender servo object to the animation
88+
animation.addServo(myBlenderServo);
89+
}
90+
91+
void loop() {
92+
// Update the animation state on each loop
93+
animation.run();
94+
}

examples/SwitchModeButton/simple.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Blender Servo Animation Positions
3+
4+
FPS: 30
5+
Frames: 100
6+
Seconds: 3
7+
Bones: 1
8+
Armature: Armature
9+
File: simple.blend
10+
*/
11+
12+
#include <Arduino.h>
13+
14+
// Servo ID: 0
15+
const int Bone[100] PROGMEM = {
16+
90, 90, 89, 89, 88, 87, 86, 84, 83, 81, 80, 78, 76, 74, 72, 70, 68, 65, 63, 61, 59, 57, 55, 54, 52, 51, 49, 48, 47, 46, 46, 45, 45, 45, 46, 47, 49, 51, 53, 55, 58, 61, 65, 68, 72, 76, 80, 84, 88, 92,
17+
96, 100, 104, 108, 112, 115, 119, 122, 125, 127, 129, 131, 133, 134, 135, 135, 135, 135, 134, 133, 132, 131, 130, 129, 127, 126, 124, 122, 120, 118, 116, 114, 112, 111, 109, 107, 105, 103, 101, 99, 98, 96, 95, 94, 93, 92, 91, 90, 90, 90,
18+
};

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ lib_deps =
2626
arduino-libraries/Servo@^1.1.8
2727
adafruit/Adafruit PWM Servo Driver Library@^2.4.1
2828
Wire
29+
mathertel/OneButton@^2.0.3

0 commit comments

Comments
 (0)