Hi there, thanks for taking the time to check out this question!
What I’m trying to accomplish: Play sound via the [sound board depending on sensor distance plus the ability to change the volume via [rotary encoder.
The issue: The constant retrieving of the sensor values is impeding my rotary code from running as quick as I would like - making me have to turn volume knob more as if it’s skipping pulses. I know why the sensor is retrieving the values as often as it is, but is there a way to slow down the intervals?
#include <SimpleRotary.h>
#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"
#include <SparkFun_RFD77402_Arduino_Library.h>
RFD77402 myDistance;
//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);
int rotaryState = HIGH;
void setup()
{
Serial.begin(115200);
ss.begin(9600);
while (!Serial);
if (myDistance.begin(Wire, I2C_SPEED_FAST) == false)
{
Serial.println("Sensor failed to initialize. Check wiring.");
while (1); //Freeze!
}
Serial.println("Sensor online!");
if (!sfx.reset()) {
Serial.println("Not found");
while (1);
}
Serial.println("SFX board found");
}
void loop()
{
//SENSOR
long startTimer = millis();
byte errorCode = myDistance.takeMeasurement();
long timeDelta = millis() - startTimer;
if (errorCode == CODE_VALID_DATA)
{
unsigned int distance = myDistance.getDistance();
byte pixels = myDistance.getValidPixels();
unsigned int confidence = myDistance.getConfidenceValue();
//PLAY AUDIO
if(distance >= 1500 ){
if(rotaryState == HIGH){
sfx.playTrack((uint8_t)1);
}
}
}
// ROTARY
byte i;
byte p;
// 0 = not turning, 1 = CW, 2 = CCW
i = rotary.rotate();
// louder
if ( i == 1 ) {
Serial.println("CW");
uint16_t v;
// sfx.stop();
if (! (v = sfx.volUp()) ) {
Serial.println("Failed to adjust");
}
}
// quiter
if ( i == 2 ) {
Serial.println("CCW");
uint16_t v;
// sfx.stop();
if (! (v = sfx.volDown()) ) {
Serial.println("Failed to adjust");
}
}
//pushed
p = rotary.push();
if(p == 1){
// STOP AUDIO FROM PLAYING
if(rotaryState == HIGH){
if (! sfx.reset() ) Serial.println("Failed to stop");
rotaryState = LOW;
}else{
rotaryState = HIGH;
}
}
}
](Rotary Encoder + Extras : ID 377 : Adafruit Industries, Unique & fun DIY electronics and kits)](Adafruit Audio FX Sound Board - WAV/OGG Trigger with 2MB Flash : ID 2133 : Adafruit Industries, Unique & fun DIY electronics and kits)