0

Help with Code for Volume Knob (Rotary Encoder) + FX Mini So
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help with Code for Volume Knob (Rotary Encoder) + FX Mini So

by meggiep on Sun Sep 08, 2019 8:49 pm

Hi, thanks for checking out this question!

I am trying to control the fx mini sound board volume with a rotary encoder - both are connected to a metro 328. I have tried calling sfx.volUp() & sfx.VolDown() depending on direction rotate thinking that would automatically turn up/down the volume - this didn't work.

I am not sure how I can accomplish what I want. Can you please help me out?

meggiep
 
Posts: 11
Joined: Wed Jun 05, 2019 4:39 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by adafruit_support_mike on Mon Sep 09, 2019 2:43 am

Post the code you're using between CODE tags, plus a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.

adafruit_support_mike
 
Posts: 58898
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by meggiep on Mon Sep 09, 2019 1:04 pm

adafruit_support_mike wrote:Post the code you're using between CODE tags, plus a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.


That's what I get for being in a hurry. Here's the code, I'll get some pics up of connections in a bit. I'm using the class d amp (max98306), 3W 4Ohm speaker set, the fx mini sound board, basic rotary & metro 328.

If it helps, I can get sound to play absolutely no problem.

Code: Select all | TOGGLE FULL SIZE
 #include "FastLED.h"
 #include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
#include <SimpleRotary.h>


int inPin = 8;         // the number of the input pin
int inPinTwo = 9;
int inPinThree = 10;
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin

int reading;           // the current reading from the input pin
int readingTwo;
int readingThree;
int previous = LOW;    // the previous reading from the input pin
int previousTwo = LOW;
int previousThree = LOW;
volatile int change;
int btnPress;

int pixelNumber;

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

//NEOPIXEL
#define LED_PIN     7
#define LEDPIN 7
#define LED_TYPE     NEOPIXEL
#define NUM_LEDS    2
#define BRIGHTNESS 255
#define FRAMES_PER_SECOND 60
CRGB leds[NUM_LEDS];
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B

//AUDIO
#define SFX_RST 11
#define SFX_TX 5
#define SFX_RX 6
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);

//ROTARY
// Pin A, Pin B, Button Pin
SimpleRotary rotary(2,4,3);

void setup()
{
//BUTTON
  pinMode(inPin, INPUT);
  pinMode(inPinTwo,INPUT);
  pinMode(inPinThree,INPUT);
  pinMode(outPin, OUTPUT);

//  Serial.begin(9600);
Serial.begin(115200);
  ss.begin(9600);

  // set up LED strip info
FastLED.addLeds<LED_TYPE,LEDPIN>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
btnPress = 1;

//AUDIO
if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  Serial.println("SFX board found");

 
}

void loop()
{
 
//  ROTARY
  byte i;
  byte p;
  // 0 = not turning, 1 = CW, 2 = CCW
  i = rotary.rotate();
//    louder
  if ( i == 1 ) {
    Serial.println("CW");
sfx.volUp();
  }
//    quiter
  if ( i == 2 ) {
    Serial.println("CCW");
sfx.volDown();
  }
  //pushed
  p = rotary.push();
  if(p == 1){
    Serial.println("Pushed");
    }
  }

meggiep
 
Posts: 11
Joined: Wed Jun 05, 2019 4:39 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by meggiep on Mon Sep 09, 2019 3:55 pm

I don't have a good enough image of my connections but here is a fritz...

soundWRotary_bb.png
soundWRotary_bb.png (524.87 KiB) Viewed 99 times

meggiep
 
Posts: 11
Joined: Wed Jun 05, 2019 4:39 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by adafruit_support_mike on Tue Sep 10, 2019 12:17 am

The pin numbers in the code don't match the connections in the schematic. The code refers to 8, 9, and 10, while the drawing shows 2, 3, and 4.

adafruit_support_mike
 
Posts: 58898
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by meggiep on Tue Sep 10, 2019 1:37 am

adafruit_support_mike wrote:The pin numbers in the code don't match the connections in the schematic. The code refers to 8, 9, and 10, while the drawing shows 2, 3, and 4.


So pins 8, 9, 10 are actually referencing push buttons in a more complicated version of the code that I didn't think was necessary to post. The rotary pins are 2,4, 3 as referenced as "SimpleRotary rotary(2,4,3);". Here is the full version of the code - including the rotary.

Code: Select all | TOGGLE FULL SIZE
 #include "FastLED.h"
 #include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
#include <SimpleRotary.h>

//BUTTONS
int inPin = 8;         // the number of the input pin
int inPinTwo = 9;
int inPinThree = 10;
int outPin = 13;       // the number of the output pin
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int readingTwo;
int readingThree;
int previous = LOW;    // the previous reading from the input pin
int previousTwo = LOW;
int previousThree = LOW;
volatile int change;
int btnPress;
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

//NEOPIXEL
#define LED_PIN     7
#define LEDPIN 7
#define LED_TYPE     NEOPIXEL
#define NUM_LEDS    2
#define BRIGHTNESS 255
#define FRAMES_PER_SECOND 60
CRGB leds[NUM_LEDS];
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
int pixelNumber;

//AUDIO
#define SFX_RST 11
#define SFX_TX 5
#define SFX_RX 6
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);

//ROTARY
// Pin A, Pin B, Button Pin
SimpleRotary rotary(2,4,3);

void setup()
{
//BUTTON
  pinMode(inPin, INPUT);
  pinMode(inPinTwo,INPUT);
  pinMode(inPinThree,INPUT);
  pinMode(outPin, OUTPUT);

//  Serial.begin(9600);
Serial.begin(115200);
  ss.begin(9600);

  // set up LED strip info
FastLED.addLeds<LED_TYPE,LEDPIN>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
btnPress = 1;

//AUDIO
if (!sfx.reset()) {
    Serial.println("Not found");
    while (1);
  }
  Serial.println("SFX board found");
}

void loop()
{
 
//  ROTARY
  byte i;
  byte p;
  // 0 = not turning, 1 = CW, 2 = CCW
  i = rotary.rotate();
//    louder
  if ( i == 1 ) {
    Serial.println("CW");
   sfx.volUp();
  }
//    quiter
  if ( i == 2 ) {
    Serial.println("CCW");
    sfx.volDown();
  }
  //pushed
  p = rotary.push();
  if(p == 1){
    Serial.println("Pushed");
    }

 
//    BUTTON 1 - SOLID LIGHT
  reading = digitalRead(inPin);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH){
      state = LOW;
      btnPress = 1;
//      sfx.stop();     
    }
    else{
      state = HIGH;
      btnPress = 2;
      }

    time = millis();   

  }
    digitalWrite(outPin, state);
    previous = reading;

//  BUTTON 2 - GLITTER LIGHT
   readingTwo = digitalRead(inPinTwo);
  if (readingTwo == HIGH && previousTwo == LOW && millis() - time> debounce){
    if(state == HIGH){
      state = LOW;
      btnPress = 3;
//      sfx.stop();
      }
    else{
      state = HIGH;
      btnPress = 4;
    }
    time = millis();
    }
    digitalWrite(outPin,state);
    previousTwo = readingTwo;


//  BUTTON 3  - MOVEMENT SENSOR LIGHT
       readingThree = digitalRead(inPinThree);
  if (readingThree == HIGH && previousThree == LOW && millis() - time> debounce){
    if(state == HIGH){
      state = LOW;
       btnPress = 5;
      }
    else{
      state = HIGH;
      btnPress = 6;
    }

    time = millis();
    }
    digitalWrite(outPin,state);
    previousThree = readingThree;



    //BUTTON 1 - SOLID LIGHT
     if (btnPress == 1){
          fill_solid(leds,NUM_LEDS,CRGB::Black);
          FastLED.show();
//           sfx.stop();
//          Serial.println("pressed1");
      }

    if (btnPress == 2){
          fill_solid(leds,NUM_LEDS,0xFfffff);
          FastLED.show();
//          Serial.println("pressed2");

      }

    //  BUTTON 2 - GLITTER LIGHT
    if (btnPress == 3){
            fill_solid(leds,NUM_LEDS,CRGB::Black);
          FastLED.show();
//           sfx.stop();
//          Serial.println("pressed3");

      }
    if (btnPress == 4){
         fadeToBlackBy( leds, NUM_LEDS, 10);
          addGlitter(30);
          FastLED.show();
//          Serial.println("pressed4");
      }

    //  BUTTON 3  - MOVEMENT SENSOR LIGHT
    if (btnPress == 5){
            fill_solid(leds,NUM_LEDS,CRGB::Black);
          FastLED.show();
//           sfx.stop();
//          Serial.println("pressed5");
      }
    if (btnPress == 6){
        Serial.println("pressed6");
//        sfx.playTrack((uint8_t)1);

      }
//        sfx.playTrack((uint8_t)1);
  }


//glitter effect
void addGlitter( fract8 chanceOfGlitter) {
  if( random8() < chanceOfGlitter) {
    leds[ random16(NUM_LEDS) ] += CRGB::White;}
}


Thanks Mike!

meggiep
 
Posts: 11
Joined: Wed Jun 05, 2019 4:39 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by meggiep on Thu Sep 12, 2019 2:43 pm

Can I bump this to get some help please?!

meggiep
 
Posts: 11
Joined: Wed Jun 05, 2019 4:39 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by adafruit_support_mike on Fri Sep 13, 2019 12:50 am

Are you sure the SimpleRotary library configures its pins as inputs?

adafruit_support_mike
 
Posts: 58898
Joined: Thu Feb 11, 2010 2:51 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by meggiep on Fri Sep 13, 2019 3:34 pm

adafruit_support_mike wrote:Are you sure the SimpleRotary library configures its pins as inputs?


According to the SimpleRotary documentation:
Getting Values

SimpleRotary Library makes it easy to get input. Call any of the following within the loop.
Getting Up / Down Rotation

To get the up / down rotation, simply call rotate().

Code: Select all | TOGGLE FULL SIZE
void loop(){
   byte i;
   i = rotary.rotate();
   
   // CW
   if ( i == 1 ) {
      Serial.println("CW");
      // Do something cool
   }
   
   // CCW
   if ( i == 2 ) {
      Serial.println("CCW");
      // Do something cool
   }
}



Am I just misunderstanding the scope of being able to call volUp()/volDown()?

meggiep
 
Posts: 11
Joined: Wed Jun 05, 2019 4:39 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by meggiep on Fri Sep 13, 2019 4:35 pm

So I was able to get the volume knob to work, but only before/after the track plays. Is there a way to adjust the volume while the track is playing so the audio feedback is instant?

Code: Select all | TOGGLE FULL SIZE
//  ROTARY
  byte i;
  byte p;
  i = rotary.rotate();

//    louder
  if ( i == 1 ) {
    Serial.println("CW");
    uint16_t v;
     if (! (v = sfx.volUp()) ) {
            Serial.println("Failed to adjust");
           
          } else {
            Serial.print("Volume: ");
            Serial.println(v);
           
          }
  }
//    quiter
  if ( i == 2 ) {
    Serial.println("CCW");
     uint16_t v;
     if (! (v = sfx.volDown()) ) {
            Serial.println("Failed to adjust");
           
          } else {
            Serial.print("Volume: ");
Serial.println(v);
           
          }
  }

meggiep
 
Posts: 11
Joined: Wed Jun 05, 2019 4:39 pm

Re: Help with Code for Volume Knob (Rotary Encoder) + FX Min

by adafruit_support_mike on Sun Sep 15, 2019 7:10 pm

How are you playing the file?

adafruit_support_mike
 
Posts: 58898
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.