Firmware Tutorial: Dance - Part II
On to Part II! In this part we will add code to the loop.
Let’s get started!
8. LEDs
When starting Robot Butterfly, the internal library code for the IMU waits a few moments for the IMU to settle, then it takes a few seconds to calibrate the IMU. During this time, the IMU is not in an active state.
It would be good for the robot to let us know when it is ready. We will use the two LEDs to do that!
Add this snippet inside the loop function, after robotbutterfly.update();.
if(getIMUState() != IMU_ACTIVE) {
digitalWrite(LED_HEARTBEAT_PIN, LOW);
digitalWrite(LED_COMMS_PIN, LOW);
} else {
digitalWrite(LED_HEARTBEAT_PIN, HIGH);
digitalWrite(LED_COMMS_PIN, HIGH);
}

Here is what is happening:
- Checking the IMU state by calling getIMUState()
- Referencing the pin numbers in the Robot Butterfly library for the LEDs
- Turning on the LEDs when the IMU is active
9. Rest
We'll want the robot to move passively while it is resting. Why? Because it would be boring if it's not doing anything!
First, let's go through the if statements as there's lots to discuss.
Add this snippet of code after the above snippet, still inside the loop function.
if(getIMUPose() == IMU_Pose_Home && getIMUState() == IMU_ACTIVE) {
if(millis()-last_movement_rest >= 20000 && millis() > 10000) {
}
}

Here is what is happening:
- The first if-statement is checking the IMU position, and checking it is the home position. As well, it is checking that the IMU state is active. These are leveraging the Robot Butterfly library.
- The second if-statement is checking the duration of time between the current time, and the last movement time.
- As well, the second if-statement is checking that the current time is greater than 10000 milliseconds (aka 10 seconds), to make sure that it doesn't immediately activate upon startup.
10. Flutter
Inside of those two if-statements, we'll add a servo animation. Plus, we will set last_movement_rest to the current time in milliseconds with the millis() function.
Add this in between the if-statements, right before the two closing parenthesis } }, still within loop.
setServoAnim(&servo_animation_alert, SERVO_ANIM_FLUTTER, SERVO_ANIM_ALERT);
setServoAnimFlutterWings(&servo_animation_alert, SERVO_ANIM_FLUTTER_WINGS_BOTH_UP);
setServoAnimDuration(&servo_animation_alert, 3000);
startServoAnim(&servo_animation_alert);
last_movement_rest = millis();

Here is what is happening:
- Running a servo animation alert, which overrides the home animation
- The servo animation is SERVO_ANIM_FLUTTER and has various parameters that are being set
- The servo animation will run for 3000 milliseconds (aka 3 seconds)
- Start running the servo animation right away!
11. Compile
Before venturing further, we should test compile the code to make sure there's no new issues! Set the board to the correct values, and press the verify checkmark button.
The result should be the same as before: only an error about undefined reference to `imuPoseChangeCallback(unsigned char)'.
If there are more errors, see if you can debug the error, and ask Erin for assistance!
