Hello all, I'm very new to Arduino coding, and I'm building a switch panel for a multimedia setup. I have everything working other than my volume knob. I can't understand how to merge working code properly. Here's my code so far=
/* Buttons to USB Keyboard Example - Special Media Player Keys
You must select Keyboard from the "Tools > USB Type" menu
This example code is in the public domain.
*/
#include <Bounce.h>
// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 ms debounce time is appropriate
Bounce button2 = Bounce(2, 10); // for most mechanical pushbuttons
Bounce button3 = Bounce(3, 10);
Bounce button4 = Bounce(4, 10); // if a button is too "sensitive"
Bounce button5 = Bounce(5, 10); // you can increase this time.
Bounce button6 = Bounce(6, 10);
int led = 12; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
void setup() {
// declare pin 12 to be an output:
pinMode(led, OUTPUT);
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip.
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
}
void loop() {
// set the brightness of pin 12:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(10);
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
// Check each button for "falling" edge.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button0.fallingEdge()) {
Keyboard.press(KEY_MEDIA_PREV_TRACK);
Keyboard.release(KEY_MEDIA_PREV_TRACK);
}
if (button1.fallingEdge()) {
Keyboard.press(KEY_MEDIA_PLAY_PAUSE);
Keyboard.release(KEY_MEDIA_PLAY_PAUSE);
}
if (button2.fallingEdge()) {
Keyboard.press(KEY_MEDIA_NEXT_TRACK);
Keyboard.release(KEY_MEDIA_NEXT_TRACK);
}
if (button3.fallingEdge()) {
Keyboard.press(KEY_MEDIA_VOLUME_DEC);
Keyboard.release(KEY_MEDIA_VOLUME_DEC);
}
if (button4.fallingEdge()) {
Keyboard.press(KEY_MEDIA_VOLUME_INC);
Keyboard.release(KEY_MEDIA_VOLUME_INC);
}
if (button5.fallingEdge()) {
Keyboard.press(KEY_MEDIA_MUTE);
Keyboard.release(KEY_MEDIA_MUTE);
}
if (button6.fallingEdge()) {
Keyboard.press(KEY_MEDIA_EJECT);
delay(300); // Mac OS-X will not recognize a very short eject press
Keyboard.release(KEY_MEDIA_EJECT);
}
}
(Use the code tags button, </> on the menu please. Thanks. Moderator)