Behaviour - Helper Functions
These are helper functions to help with some common RoboBrrd actions, like flapping its wings and opening & closing its beak. The functions can be added in the code after the loop() function. Same place as where the other helper functions were, from
Programming - Step 4.
Wings
// -- WINGS -- //
void leftWave(int repeat, int d) {
for(int i=0; i<repeat; i++) {
lwing.write(l_upper);
delay(d);
lwing.write(l_lower);
delay(d);
}
lwing.write(l_middle);
}
void rightWave(int repeat, int d) {
for(int i=0; i<repeat; i++) {
rwing.write(r_upper);
delay(d);
rwing.write(r_lower);
delay(d);
}
rwing.write(r_middle);
}
The above are two functions for the left and right wing that make the, 'wave'. Going from the top to the bottom for a given number of repeats, and returning to the bottom. The speed of the movement from top to bottom is controlled with the variable d. d is the given amount of time that the servo will have to complete its movement before going on to the next one.
If d is really small, it is possible that the servo won't have enough time to reach its position before having to move to the next one. This will give the appearance that the wings are flapping quite quickly. If d is really large, the servo will have enough time to reach its position, and possibly wait for a little while before moving on to the next one. I usually use 100ms for d, to give the wings a medium/normal flapping speed.
Both wings
void bothWave(int repeat, int d, boolean alt) {
for(int i=0; i<repeat; i++) {
rwing.write(r_upper);
if(alt) {
lwing.write(l_lower);
}
else {
lwing.write(l_upper);
}
delay(d);
rwing.write(r_lower);
if(alt) {
lwing.write(l_upper);
}
else {
lwing.write(l_lower);
}
delay(d);
}
rwing.write(r_middle);
lwing.write(l_middle);
}
Here is a helper function to flap both wings. Similar to the ones above, it is for a specified number of repeats, with a specified time for the wing movements between positions. There is also the option to specify if the wings should be alternate each other or not. This will just make the wings either go up and down in unison, or both sides would alternate between up and down.
Beak
// -- BEAK -- //
void beakOpen(int s) {
beak.write(b_open);
delay(s);
}
void beakClose(int s) {
beak.write(b_closed);
delay(s);
}
These ones are pretty straight forward. Since opening and closing the beak is pretty straight forward, we have this helper function so that we can specify a delay time for the beak to reach that position. A smaller value of s will make the beak open/close quickly, though possibly not all the way. Slower value of s will make the beak open/close slowly, and possibly wait at that position. I usually use 50ms for s, for a medium/normal opening/closing speed.
Eyes
// -- EYES -- //
void eyes(int rval, int gval, int bval) {
analogWrite(r, rval);
analogWrite(g, gval);
analogWrite(b, bval);
}
This will really depend on how you have your eyes wired. If both left and right sides are wired onto PWM pins, this function will be able to control the brightness of the red, green, and blue LEDs.
Eyes (alternate version)
// -- EYES -- //
void eyes(int rlval, int glval, int blval, int rrval, int grval, int brval) {
(rlval == 1) ? digitalWrite(rl, HIGH) : digitalWrite(rl, LOW);
(glval == 1) ? digitalWrite(gl, HIGH) : digitalWrite(gl, LOW);
(blval == 1) ? digitalWrite(bl, HIGH) : digitalWrite(bl, LOW);
(rrval == 1) ? digitalWrite(rr, HIGH) : digitalWrite(rr, LOW);
(grval == 1) ? digitalWrite(gr, HIGH) : digitalWrite(gr, LOW);
(brval == 1) ? digitalWrite(br, HIGH) : digitalWrite(br, LOW);
}
Here is an alternate example of an eyes helper function, if the RGB LEDs were connected to separate pins (making left and right independently controlled). By passing a 1 to any of the values, it will make that colour lit. By passing a 0 (or actually, anything other than a 1), will make them not lit. As a programming note, the reason why we check to see if it is a 1 or 0, instead of directly passing that to digitalWrite, is just in case HIGH and LOW change in some future version of Arduino. It's unlikely, but good programming practice.