Pi Pico volume control?

Hello I only currently have a pi pico board at the moment. I'm trying to build a volume controller for my pc. I'm trying to use a encoder https://www.amazon.com/HiLetgo-Degrees-Rotary-Encoder-Arduino/dp/B07WZF1TXX Here. But everything I found is for push buttons. I don't have any push buttons. and the only one I found for a encoder code I did mange to find is https://blog.prusa3d.com/3d-print-an-oversized-media-control-volume-knob-arduino-basics_30184/ Here. When I try to load it I get this error.

In file included from C:\Users\jJc\Documents\Arduino\files\oversized_volume_knob\oversized_volume_knob\oversized_volume_knob.ino:3:0:
C:\Users\jJc\Documents\Arduino\libraries\encoder-volume/ClickEncoder.h:18:10: fatal error: avr/io.h: No such file or directory
 #include <avr/io.h>
          ^~~~~~~~~~
compilation terminated.
exit status 1
Error compiling for board Raspberry Pi Pico.

I'm not much of a coder and i really need help. Can someone please help me?

Joseph

A RPi Pico isn't an AVR.

Did you install the pico software in the Arduino IDE?

Have you selected the board type that matches your board?

Hello, You are correct. After reading up on the whole error it is for an avr. Yes I do have the arduino pi pico bootloader on it. I'm able to do a lot of things such as the oled and other things with no problem.

Joseph

Yes I have the arduino bootloader on it. I can do a lot of things I normally do in the IDE.

Joseph

hello, Just an update. I finally mange to find a arduino nano. I tried the code and got errors. I couldn't get it to work. The error stats that

In file included from C:\Users\jJc\AppData\Local\Temp\arduino_modified_sketch_837413\oversized_volume_knob.ino:5:0:
C:\Users\jJc\Documents\Arduino\libraries\HID\src/HID-Project.h:35:2: error: #error HID Project can only be used with an USB MCU.
 #error HID Project can only be used with an USB MCU.
  ^~~~~
exit status 1
Error compiling for board Arduino Nano.

It looks like it wants a arduino leonardo that has native USB on it. something I do not have. Here is the sketch.

// Read the full tutorial at prusaprinters.org

#include <ClickEncoder.h>
#include <TimerOne.h>
#include <HID-Project.h>

#define ENCODER_CLK 5 // Change A0 to, for example, A5 if you want to use analog pin 5 instead
#define ENCODER_DT 4
#define ENCODER_SW 3

ClickEncoder *encoder; // variable representing the rotary encoder
int16_t last, value; // variables for current and last rotation value

void timerIsr() {
  encoder->service();
}

void setup() {
  Serial.begin(9600); // Opens the serial connection used for communication with the PC. 
  Consumer.begin(); // Initializes the media keyboard
  encoder = new ClickEncoder(ENCODER_DT, ENCODER_CLK, ENCODER_SW); // Initializes the rotary encoder with the mentioned pins

  Timer1.initialize(1000); // Initializes the timer, which the rotary encoder uses to detect rotation
  Timer1.attachInterrupt(timerIsr); 
  last = -1;
} 

void loop() {  
  value += encoder->getValue();

  // This part of the code is responsible for the actions when you rotate the encoder
  if (value != last) { // New value is different than the last one, that means to encoder was rotated
    if(last<value) // Detecting the direction of rotation
      Consumer.write(MEDIA_VOLUME_UP); // Replace this line to have a different function when rotating counter-clockwise
      else
      Consumer.write(MEDIA_VOLUME_DOWN); // Replace this line to have a different function when rotating clockwise
    last = value; // Refreshing the "last" varible for the next loop with the current value
    Serial.print("Encoder Value: "); // Text output of the rotation value used manily for debugging (open Tools - Serial Monitor to see it)
    Serial.println(value);
  }

  // This next part handles the rotary encoder BUTTON
  ClickEncoder::Button b = encoder->getButton(); // Asking the button for it's current state
  if (b != ClickEncoder::Open) { // If the button is unpressed, we'll skip to the end of this if block
    //Serial.print("Button: "); 
    //#define VERBOSECASE(label) case label: Serial.println(#label); break;
    switch (b) {
      case ClickEncoder::Clicked: // Button was clicked once
        Consumer.write(MEDIA_PLAY_PAUSE); // Replace this line to have a different function when clicking button once
      break;      
      
      case ClickEncoder::DoubleClicked: // Button was double clicked
         Consumer.write(MEDIA_NEXT); // Replace this line to have a different function when double-clicking
      break;      
    }
  }

  delay(10); // Wait 10 milliseconds, we definitely don't need to detect the rotary encoder any faster than that
  // The end of the loop() function, it will start again from the beggining (the begginging of the loop function, not the whole document)
}


/*
    This is just a long comment
    Here are some fun functions you can use to replace the default behaviour 
    Consumer.write(CONSUMER_BRIGHTNESS_UP);
    Consumer.write(CONSUMER_BRIGHTNESS_DOWN);
    Consumer.write(CONSUMER_BROWSER_HOME);
    Consumer.write(CONSUMER_SCREENSAVER);
    Consumer.write(HID_CONSUMER_AL_CALCULATOR); //launch calculator :)
    Consumer.write(HID_CONSUMER_AC_ZOOM_IN);
    Consumer.write(HID_CONSUMER_AC_SCROLL_UP);
    CONSUMER_SLEEP = 0x32,

    FULL LIST CAN BE FOUND HERE:
    https://github.com/NicoHood/HID/blob/master/src/HID-APIs/ConsumerAPI.h
*/
        

I'm unsure what to do now. I can not find anything that use a rotary encoder for a volume control on a uno or nano board. Does anyone know where I can find something like that?

Joseph

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.