This code is for an LED/Sound layout using common-cathode RGB LEDs on a MEGA 2560 and 6 10k Potentiometers for adjustment.
When I use the code absent the volume library, it works perfectly, but when I incorporate the volume library, the results just become unpredictable and effed-up.
For example, Potentiometer 2 (potPins[1]), aka pin A6, corresponding to the Bval variable seems to slow down the execution of the code, i.e. has something to do with timer0 (I'm assuming), which is utilized by the volume library NO MATTER WHERE I PLUG IT (amongst the available analog pins, which are A8-A12, or A6).
This is really frustrating me beyond my ability to produce words to convey that emotion.
The obvious answer is "don't use the volume library, but it's pretty integral to the intended function of the project, and also, I need to know what this timer problem is". An answer for me could be as simple as "such and such pins shouldn't be used because they correspond to some interrupt or timer, but that answer doesn't seem available anywhere I've looked). And I obviously don't wholly grasp the idea of timers and interrupts.
Here is the problem code:
#include <Volume.h>
Volume vol; // Plug your speaker into the default pin for your board type: //Note to self: this declares an instance of "Volume", is necessary.
// https://github.com/connornishijima/arduino-volume#supported-pins
int cathode[15] = {32, 34, 33, 35, 31, 49, 51, 52, 53, 50, A2, A1, A0, A3, 8};
int anode[9] = {2,3,5,6,9,10,7,11,12};
int period = 55;
int Rval = 50;
int Gval = 0;
int Bval = 50;
int potPins[6] = {A15, A14, A13, A5, A6, A7};
int potVals[6] = {0, 0, 0, 0, 0, 0};
void setPinModes();
void setColors();
void clearall();
void sequenceAdd();
void sequenceClear();
void getPotVals();
void setup() {
vol.begin(); // After calling this, vol.delay() and vol.delayMicroseconds will no longer work
// correctly! Instead, use vol.delay() and vol.delayMicroseconds() for
// the correct timing
vol.setMasterVolume(1.00); // Self-explanatory enough, right? Try lowering this value if the speaker is too loud! (0.00 - 1.00)
vol.delay(500);
vol.alternatePin(false);
Serial.begin(9600);
setPinModes();
setColors();
}
void loop() {
clearall();
setColors();
getPotVals();
//sequenceAdd();
sequenceClear();
}
void setPinModes() {
for (int i = 0; i < 15; i++) {
pinMode(cathode[i], OUTPUT);
}
for (int i = 0; i < 8; i++) {
pinMode(anode[i], OUTPUT);
}
for (int i = 0; i < 5; i++) {
pinMode(potPins[i], INPUT);
}
}
void setColors() {
getPotVals(); //This is sort of sloppy coding, so clean this up now.
analogWrite(anode[0], Rval);
analogWrite(anode[3], Rval);
analogWrite(anode[6], Rval);
analogWrite(anode[1], Gval);
analogWrite(anode[4], Gval);
analogWrite(anode[7], Gval);
analogWrite(anode[2], Bval);
analogWrite(anode[5], Bval);
analogWrite(anode[8], Bval);
}
void clearall() {
for (int i = 0; i<15; i++) {
digitalWrite(cathode[i], HIGH);
}
setColors();
}
void sequenceAdd(){
for (int i = 0; i<15; i++) {
digitalWrite(cathode[i], LOW);
vol.delay(100);
}
clearall();
for (int i = 14; i>0; i--) {
digitalWrite(cathode[i], LOW);
vol.delay(100);
}
clearall();
}
void sequenceClear() {
for (int i = 0; i <12; i++) {
digitalWrite(cathode[i], LOW);
vol.delay(period);
clearall();
}
vol.delay(period*0.20);
digitalWrite(cathode[12], LOW);
vol.delay(period*1.40);
clearall();
digitalWrite(cathode[13], LOW);
vol.delay(period*1.70);
clearall();
digitalWrite(cathode[14], LOW);
vol.delay(period*2.00);
clearall();
for (int i = 14; i > 2; i--) {
digitalWrite(cathode[i], LOW);
vol.delay(period);
clearall();
}
vol.delay(period*0.20);
digitalWrite(cathode[2], LOW);
vol.delay(period*1.40);
clearall();
digitalWrite(cathode[1], LOW);
vol.delay(period*1.70);
clearall();
digitalWrite(cathode[0], LOW);
vol.delay(period*2.00);
clearall();
}
void getPotVals() {
for (int i = 0; i < 5 ; i++) {
potVals[i] = analogRead(potPins[i]);
Serial.print(potVals[i]);
Serial.print(" ");
}
Serial.println(); // carriage return after potVals written serially.
Rval = map(potVals[3], 0, 1023, 0, 255);
Gval = map(potVals[4], 0, 1023, 0, 255);
Bval = map(potVals[5], 0, 1023, 0, 255);
}
I'll be researching the issue and hoping someone will help me figure it out.
Thanks a lot.