The Knob on the Y-Board
The knob is a device known as a rotary encoder. It can be turned left or right, and it can also be pressed down like a button. Every time the knob is turned, it sends a pulse to the board so that it can keep track of how far it has been turned. While the knob can be turned left and right as much as you want, there is a value for how far it has been turned, postive for right and negative for left.
Functions
There are a few functions that we can use to interact with the knob. To get the knob position, you can call Yboard.get_knob()
. If you want to reset the knob position to zero, you can call Yboard.reset_knob()
. If you want to check if the knob is pressed down, you can call Yboard.get_knob_button()
.
Examples
Yboard.set_led_brightness(0); // Set initial brightness to 0
Yboard.set_all_leds_color(255, 255, 255); // Set all LEDs to white
while(true) {
int knob_position = Yboard.get_knob();
Serial.printf("Knob position: %d\n", knob_position);
// Make sure the knob value is not too low or too high.
if (knob_position < 0) {
knob_position = 0;
} else if (knob_position > 100) {
knob_position = 100;
}
int brightness = 255 * knob_position / 100;
Yboard.set_led_brightness(brightness);
// Then use Yboard.set_led_color() as normal inside this loop.
}
More Details
You’ll notice that we’ve multiplied the value of Yboard.get_knob()
by 255 and divided it by 100. The reason is because Yboard.set_led_brightness()
needs a brightness between 0 and 255, but Yboard.get_knob()
gives us a value between 0 and 100. We can scale our value from Yboard.get_knob()
to a value that Yboard.set_led_brightness()
will understand by multiplying it by the maximum value of our brightness function and dividing by the maximum value of our knob function.
Notice also that we are monitoring the value of Yboard.get_knob()
continuously by placing it inside an infinite while
loop.
There are other things you can try to control with Yboard.get_knob()
(for example, you could use it to change the color of LEDs rather than brightness) but brightness is the easiest. Feel free to experiment with it!
Exploration
Remember to change main.cpp
before continuing…
📝 NOTE: You will need to go to
main.cpp
and change the comments to call the correct activity function:// conditionals_activity(); knob_activity();
- Have the knob control the brightness of the LEDs. You can use the code example above to get started.
Challenges
Remember to comment/uncomment the correct function calls…
Remember to comment out the knob_exploration();
call in the knob_activity
function and uncomment the correct challenge function:
knob_exploration();
// knob_challenge1();
// knob_challenge2();
Challenge 1: Light up the LEDs as you turn the knob. The LEDs should light up one by one as you turn the knob to the right, and turn off one by one as you turn the knob to the left.
Challenge 2: If switch 1 is on, have the knob control the red component of the LED color, if switch 2 is on, have the knob control the green component of the LED color, and if switch 3 is on, have the knob control the blue component of the LED color.