// simple project using Arduino LEONARDO, potentiometer and 10 LEDs to control computer volume
// created by upir, 2022
// youtube channel: https://www.youtube.com/upir_upir
// FULL TUTORIAL: https://youtu.be/6cqvTHCuDto
// Arduino Leonardo - https://s.click.aliexpress.com/e/_DDhZNN9
// Arduino UNO - https://s.click.aliexpress.com/e/_AXDw1h
// Arduino breadboard prototyping shield - https://s.click.aliexpress.com/e/_ApbCwx
// Huge Aluminium Knob: https://s.click.aliexpress.com/e/_A4GlET
// Big Black knob: https://s.click.aliexpress.com/e/_Aq1wGF
// PCBWay - https://www.pcbway.com/setinvite.aspx?inviteid=572577
// Use the link for PCBway to get 10 PCBs created for free (0$). Only pay for shipping.
//byte led_pins[10] = {2,3,4,5,6,7,8,9,10,11}; // pins for 10 LEDs
byte led_pins[10] = {11,10,9,8,7,6,5,4,3,2}; // reversed order
//byte led_pins[10] = {11, 6, 7, 8, 9, 10, 5, 4, 3, 2}; // PCB order
#if defined(ARDUINO_AVR_LEONARDO)
#include <HID-Project.h> //include HID_Project library
#endif
int pot_value = 0; // potentiometer value
int old_volume = 0; // old volume value
void setup() {
for (int i=0; i<10; i++) {
pinMode(led_pins[i], OUTPUT); // initialize digital pins as outputs (in order to control LEDs)
}
pinMode(A0,INPUT); // set A0 as input for reading the potentiometer value
#if defined(ARDUINO_AVR_LEONARDO) // initialize USB connection when the board is Arduino Leonardo
Consumer.begin();
#endif
}
void loop() {
pot_value = analogRead(A0); // read the value of the potentiometer
pot_value = map(pot_value, 0, 1023, 0, 50); // map the value between 0-50, as this is the range used in Windows for volume
if (pot_value != old_volume) { // volume is different from the pontetiometer value
if (pot_value > old_volume) {
#if defined(ARDUINO_AVR_LEONARDO)
Consumer.write(MEDIA_VOLUME_UP); // increase volume
#endif
old_volume = pot_value;
}
else if (pot_value < old_volume) {
#if defined(ARDUINO_AVR_LEONARDO)
Consumer.write(MEDIA_VOLUME_DOWN); // decrease volume
#endif
old_volume = pot_value;
}
}
// light up the corresponding LEDs
for (int i=0; i<10; i++) { // set the LEDs based on the potentiometer value
if (pot_value <= i*5) {
digitalWrite(led_pins[i], LOW); // LED off
}
else {
digitalWrite(led_pins[i], HIGH); // LED on
}
}
}