When I try to use sound on my WAV trigger, regardless of how simple the code is, it just creates a drone sound of unimaginable irritation. It seems like it's continually starting a new track every clock interval instead of following through with the one it's already playing. Apparently, checking if the track is(n't) playing isn't good enough to stop it. I've tried on an arduino nano and a mega, and both times it's the same thing. funnily enough, when I hit the reset button the track plays fine, so it has to be a code issue, although looking through the wav trigger arduino library examples yields nothing useful information-wise.
Now it has an opposite effect now that I isolated the code relating specifically to the sound functions. Now, when I turn the power (digital power switch) on, it stops making sound completely and beeps for a split second when I turn it off. The WAV trigger LED lights up when the power is on, but no sound.
#include <wavTrigger.h>
#include <AltSoftSerial.h>
wavTrigger wTrig;
int pwr = LOW;
int pwrSw = 4;
AltSoftSerial sound;
void setup() {
sound.begin(57600);
wTrig.setAmpPwr(true);
wTrig.masterGain(10);
wTrig.setReporting(true);
pinMode(pwrSw, INPUT_PULLUP);
wTrig.start();
wTrig.stopAllTracks();
wTrig.samplerateOffset(0);
wTrig.update();
delay(500);
}
void loop() {
pwr = digitalRead(pwrSw);
if (pwr == LOW)
{
wTrig.trackPlayPoly(1);
}
else if (pwr == HIGH)
{
wTrig.stopAllTracks();
}
wTrig.update();
}
I don't believe so. Before, on my full program, it sounded like it was starting over every cycle (which leads me to believe that it doesn't), but now it's just not doing anything. I'm perplexed.
That's the thing, I get the same result even when I detect state changes, when I detect if the board is playing, no matter what I'm always left with the same result. I've read the documentation as much as I possibly could understand but I'm obviously missing something.
Here's a sample of the code where I'm trying to detect when the sound is playing as well as if the sound has played, I'm pretty sure this is where my code is messed up.
if ((!wTrig.isTrackPlaying(1)) && (didStart == false))
{
wTrig.trackPlayPoly(1);
didStart = true;
}
else if ((!wTrig.isTrackPlaying(1)) && (didStart == true))
{
wTrig.trackPlayPoly(2);
wTrig.trackLoop(2, true);
}
using (wTrig... == false) doesn't seem to work in lieu of the exclamation mark either.
Alright, after realizing that keeping the main part of my code secret is a little useless and counterproductive, here's the full thing. I worked out most of the bugs on my own but between the power on loop and power off sounds there's some weird buzzing noise still coming through. Any thoughts as to why?
#include <Adafruit_NeoPixel.h>
#include <wavTrigger.h>
// Start definitions for Cyclotron and Powercell neopixels
#define CTPIN 2
#define PCPIN 3
#define CTCNT 40
#define PCCNT 15
Adafruit_NeoPixel cyclotron = Adafruit_NeoPixel(CTCNT, CTPIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel powercell = Adafruit_NeoPixel(PCCNT, PCPIN, NEO_GRB + NEO_KHZ800);
unsigned long CTAstartMillis;
const unsigned long CTAperiod = 3;
unsigned long CTstartMillis;
const unsigned long CTperiod = 850;
unsigned long PCstartMillis;
const unsigned long PCperiod = 50;
unsigned long currentMillis;
unsigned long CTfade;
const unsigned long CTFperiod = 1;
int pci = 0;
int cti = 0;
int CTReds[] = { 255, 204, 153, 102, 51, 0 };
int GB1LEDS[] = { 0, 12, 22, 31 };
int GB1LEDR[] = { 31, 22, 12, 0 };
// End definitions for Neopixels
wavTrigger wTrig;
bool isPlayingAmbient = false;
bool isPlayingWand = false;
bool isPlayingFire = false;
bool packPwr = false;
bool newData;
const byte numChars = 32;
char receivedChars[numChars];
#define pwrSw 4
int pwrSwD;
int stButD;
int packState = 3;
int cti1 = 0;
int ctb = 255;
int dataNumber = 0;
bool reverse = false;
bool isDone = false;
char output[128];
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial3.begin(57600);
wTrig.setAmpPwr(true);
wTrig.masterGain(10);
wTrig.setReporting(true);
pinMode(pwrSw, INPUT_PULLUP);
CTstartMillis = millis();
PCstartMillis = millis();
cyclotron.begin();
powercell.begin();
delay(1000);
wTrig.start();
wTrig.stopAllTracks();
wTrig.samplerateOffset(0);
wTrig.update();
delay(500);
}
void loop() {
currentMillis = millis();
recSerial();
wandInput();
checkCyclotron();
checkPowercell();
checkAmbient();
checkPower();
}
void checkPower() {
pwrSwD = digitalRead(pwrSw);
if ((dataNumber == 0) && (pwrSwD == HIGH)) {
packPwr = false;
} else {
packPwr = true;
}
}
void checkPowercell() {
if (packPwr == true) {
if (currentMillis - PCstartMillis >= PCperiod) {
if (pci <= PCCNT) {
pci++;
PCstartMillis = currentMillis;
} else {
powercell.clear();
pci = 0;
}
}
powercell.setPixelColor(pci, powercell.Color(0, 0, 255));
powercell.show();
} else {
if (currentMillis - PCstartMillis >= PCperiod) {
if (pci > -1) {
pci--;
} else {
powercell.clear();
pci = 0;
}
PCstartMillis = currentMillis;
}
powercell.setPixelColor(pci + 1, powercell.Color(0, 0, 0));
powercell.show();
}
}
void checkCyclotron() {
if (packPwr == true) {
ctb = 255;
if (packState == 3) {
cyclotron.clear();
if (currentMillis - CTAstartMillis >= CTAperiod) {
cti--;
CTAstartMillis = currentMillis;
}
if (cti < 0) {
cti = 39;
}
for (int i = 0; i <= 5; i++) {
int q = cti + i;
if (q > 39) {
q = q - 40;
}
int v = CTReds[i];
cyclotron.setPixelColor(q, cyclotron.Color(v, 0, 0));
}
cyclotron.show();
} else if (packState == 2) {
if (currentMillis - CTstartMillis >= CTperiod) {
CTstartMillis = currentMillis;
int ctit = cti1 - 1;
if (ctit < 0) {
ctit = 3;
}
if (reverse) {
cyclotron.setPixelColor(GB1LEDR[ctit], cyclotron.Color(0, 0, 0));
cyclotron.setPixelColor(GB1LEDR[cti1], cyclotron.Color(ctb, 0, 0));
} else {
cyclotron.setPixelColor(GB1LEDS[ctit], cyclotron.Color(0, 0, 0));
cyclotron.setPixelColor(GB1LEDS[cti1], cyclotron.Color(ctb, 0, 0));
}
if (cti1++ > 2) {
cti1 = 0;
}
}
cyclotron.show();
}
} else {
if (currentMillis - CTfade >= CTFperiod) {
CTfade = currentMillis;
if (ctb > 0) {
ctb--;
} else if (ctb <= 0) {
ctb = 0;
}
cyclotron.fill(cyclotron.Color(ctb, 0, 0), 0, CTCNT);
}
cyclotron.show();
}
}
void recSerial() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial1.available() > 0) {
rc = Serial1.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void wandInput() {
if (newData == true) {
dataNumber = 0;
dataNumber = atoi(receivedChars);
Serial.println(receivedChars);
Serial.println(dataNumber);
newData = false;
}
}
void checkAmbient() {
wTrig.update();
if (packPwr == true) {
if (!wTrig.isTrackPlaying(1)) {
wTrig.update();
wTrig.trackPlaySolo(1);
wTrig.trackLoop(2, true);
wTrig.trackPlayPoly(2);
wTrig.update();
wTrig.flush(); //code will not work without this... idk why
}
}
if (packPwr == false) {
wTrig.trackLoop(2, false);
if (!wTrig.isTrackPlaying(10)) {
wTrig.trackPlaySolo(10);
wTrig.update();
wTrig.flush(); //ditto from line 219
}
}
}
I'm not sure I understand. It's supposed to play a short noise when I turn the power off and then stop, but the buzzing happens immediately after turning off the power, lasts a second at most, and then plays the power down sound. I toyed around with your idea for about an hour and giving it state variables only made the buzzing worse somehow? I wish I kept that code...