Skip to content

Instantly share code, notes, and snippets.

@thomasstoeckert
Created August 2, 2018 16:58
Show Gist options
  • Save thomasstoeckert/f320184bb077844a9bad244a765d7d3d to your computer and use it in GitHub Desktop.
Save thomasstoeckert/f320184bb077844a9bad244a765d7d3d to your computer and use it in GitHub Desktop.
The Arduino code for the MagicRadio's input
// Adjust this to fit your smoothing desires. I've found 5 is the best balance of delay/smoothing
// Increasing this makes it a bit more sluggish to respond to quick changes
const int numReadings = 5;
// Set up pin definitions
int volumePot = A0;
int tuningPot = A5;
// Set up the variables for smoothing/reading with volume and tuning pots
int volumeReadings[numReadings];
int volTotal = 0;
int volAverage = 0;
int tuningReadings[numReadings];
int tunTotal = 0;
int tunAverage = 0;
int readIndex = 0;
// Set up variables for the volume switch
int volSwitch = 8;
int volReading;
void setup() {
// Setting up tuning/volume dials
pinMode(volumePot, INPUT);
pinMode(tuningPot, INPUT);
// Setting up the volume Switch
pinMode(volSwitch, INPUT);
// Begin serial comms
Serial.begin(9600);
// Fill readings arrays
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
volumeReadings[thisReading] = 0;
tuningReadings[thisReading] = 0;
}
}
void loop() {
// Prepare potentiometer readings
volTotal = volTotal - volumeReadings[readIndex];
tunTotal = tunTotal - tuningReadings[readIndex];
// Read potentiometers, add them to the list
volumeReadings[readIndex] = analogRead(volumePot);
tuningReadings[readIndex] = analogRead(tuningPot);
// Add back to totals
volTotal = volTotal + volumeReadings[readIndex];
tunTotal = tunTotal + tuningReadings[readIndex];
// Move index
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
// Calculate Averages
volAverage = volTotal / numReadings;
tunAverage = tunTotal / numReadings;
// Print pattern is "tunAverage,volAverage,volSwitch"
Serial.print(tunAverage);
Serial.print(",");
Serial.print(volAverage);
// Read and prepare switches
volReading = digitalRead(volSwitch);
Serial.print(",");
Serial.println(volReading);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment