My code stops running every time with most of My sketches. example included.

Status
Not open for further replies.
I'm trying to use the builtin DACs on a 3.5 and I found that the code stops running on almost every sketch I put on it.
I have 4 Leds on pins 6, 24, 25, 26 that I use to show the code is running. I'm just trying to take audio signal from analog pins and put them out the DAC pins. I can get sound out of the DAC pins if I just send numbers to the DACs but it seems that when I try to read the analog input pins it stops running the entire code. I've been at this for about 4 days or so.
Waiting for my audio in/out board should be here to day, but at the same time I can't find anything in my code that would make it stop running. I'm running arduino 1.8.10 and teensy loader 1.48.

I read somewhere that you should not interfere with the data stream when you use the
AudioConnection patchCords from the Audio System Design Tool. So don't have them in my code now,
but I've tried that too. It seems that a sketch will keep running as long as I don't try to use audio objects.

here is the one of the sketches that stops.

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthSimpleDrum     drum1;          //xy=201,199
AudioSynthWaveformSine   sine1;          //xy=238,106
AudioInputAnalogStereo   adcs1;          //xy=154,172
AudioOutputAnalogStereo  dacs2;          //xy=528,125
//AudioConnection          patchCord1(drum1,0,dacs2,1);
//AudioConnection          patchCord2(sine1,0,dacs2,0);
// GUItool: end automatically generated code

int R=0,L=0,Rt=0,Lt=0;
int dacR=66,dacL=67,adc1R=16,adc1L=17,adc2R=37,adc2L=38;
int apin[6]={16,17,37,38,66,67};
int loopCnt=0,lDisp=0,lcdCurPos=0;
float volR=0,volL=0;
int led1=26,led2=25,led3=24,led4=6;
int leda[4]={26,25,24,6};
int but1=32,but2=31,but3=30,but4=29;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
int but1was=1,but2was=1,but3was=1,but4was=1;
int ledn=0,vol=0,bal=0,tick=0;
int aR(int pin){int rd=analogRead(pin);return rd;}
int dR(int pin){int rd=digitalRead(pin);return rd;}
void dW(int pin,int d){digitalWrite(pin,d);}
void aW(int pin,int d){analogWrite(pin,d);}
void gnd(int pin){pinMode(pin,1);digitalWrite(pin,0);}
void vcc(int pin){pinMode(pin,1);digitalWrite(pin,1);}
void pu(int pin){pinMode(pin,0);digitalWrite(pin,1);}
void in(int pin){pinMode(pin,0);digitalWrite(pin,0);}
void out(int pin){pinMode(pin,1);}
void flash(int gap){digitalWrite(13,1);delay(20);digitalWrite(13,0);delay(gap*10);}
void togleLed(){if(ledn==1){ledn=0;dW(13,0);}else{ledn=1;dW(13,1);}}
void ledOff(){dW(led1,0);dW(led2,0);dW(led3,0);dW(led4,0);}
void ledOn(){dW(led1,1);dW(led2,1);dW(led3,1);dW(led4,1);}

time_t getTeensy3Time(){return Teensy3Clock.get();}
void lf(){Serial1.println();}
void clrl(){Serial1.write(12);delay(50);}//only clear
void clrs(){Serial.println();delay(50);}//only clear
void snd1(){Serial1.write(212);Serial1.write(220);} // sou8nd a tone FROM THE DISPLAY
void clearDisplay(){Serial1.write(12);}//only clear 
void lineF(){Serial1.write(13);}// line feed 
void lcdOnn(){Serial1.write(17);delay(5);}// lcd light on 
void lcdOf(){Serial1.write(18);}//steer light off
void pd(){Serial1.print(".");lcdCurPos++;if(lcdCurPos==15){clrl();}}
void ps(){Serial1.print(" ");}
void p(int i){Serial1.print(i);}

void numb(int dat){ledOff();if(dat&8)dW(led4,1);if(dat&4)dW(led3,1);if(dat&2)dW(led2,1);
    if(dat&1)dW(led1,1);delay(500);}
int rch=0,lch=0;
unsigned long time=0;
int b=0;

void setup(){pu(but1);pu(but2);pu(but3);pu(but4);gnd(led1);vcc(led2);vcc(led3);gnd(led4);
    Serial1.begin(9600);Serial.begin(9600);delay(1000);lcdOnn();gnd(led2);gnd(led3);gnd(13);
    clrs();clrl();delay(1000);Serial1.println("lcd running");Serial.println("monitor running");}

void loop(){if(time+500<millis()){time=millis();b++;if(b==16)b=0;numb(b);
    rch=aR(16);lch=aR(17);aW(66,rch);aW(67,lch);}}
 
Last edited by a moderator:
The audioInputAnalogStereo object "monopolizes" the 2 internal ADCs as does the audioOutputAnalogStereo object with the DACs. Using the generic analogRead() and analogWrite() functions in your code conflicts with the interrupt based and timing critical audio queue processing. That's why your code stops.

You'll have to decide: Either you let the audio library read the ADCs or you read them manually, and either you let the audio library write the DACs or you write them manually. You can't do both at the same time.
 
Thank you for explaining that I was getting very frustrated. I have since got things to work but I found that if I try to change things that they stop working quickly. I have only been able to make small changes in the example sketches and have them keep working. One of my problems was that I didn't wait long enough for it to start. but now I have written a sketch that takes data from the A2 and A3 pins and some circuitry and sends them out to the DAC pins and shows a sample of the data including min and max values and I see I need some voltage limiting parts on my input. But The way you worded your answer helps me understand what I was doing wrong. So I was right, when I tried to sample the data stream I put a wrench in the gears and stopped it. I guess there are objects I can use to sample the data like the peak detector. okay as long as I keep my fingers out of it there should be no problem.

Thank you very much.
 
Status
Not open for further replies.
Back
Top