A Working Due
So after the faulty Due, the second attempt seems to work. The new Due isn't missing a transistor, boots, and even cycles properly. I spent some time reading about ARM behaviour and specific functions before coding a sketch. I decided to try and see how much I could abuse the new found speed of the Due so I wrote all of my wave generators to calculate values on the fly. Here is the new script:
/* Synth.ino
----------------------------------------------------------
Description:
A sketch to generate sound waves for audio processing
with an arduino Due.
State:
"in early developement" (AKA not functioning yet).
Something is slowing down the cycles to the point
where it just sounds like a speaker-pop. All
the coret values are beings processed, just not
quickly enough.
----------------------------------------------------------
References:
http://makezine.com/projects/make-35/advanced-arduino-sound-synthesis/
https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
----------------------------------------------------------
Contact: ldavis@marlboro.edu
https://github.com/SafeCamp
https://safecamp.github.io/
----------------------------------------------------------
Logan Davis | 3/5/16 | Arduino 1.6.7
----------------------------------------------------------
*/
#include <avr/pgmspace.h>
//-----WAVE FORM NAMES-----//
#define TRIANGLE 0
#define SAW 1
#define SQUARE 2
#define WHITENOISE 3
#define SINE 4
//-----PIN NAMES------//
#define WAVESELECT A0
#define FREQ A1
#define AUDIO_PORT DAC1
int declining_triangle[2048] = {0};
int i = 0;
void setup(){
/* Set PWM pin to OUTPUT */
pinMode(AUDIO_PORT, OUTPUT);
analogWriteResolution(12);
gen_downward_slope(declining_triangle);
}
void loop() {
/*check to see if the user wants a different wave
* if the selection is different, change waveforms.
* After that check, output waveform value and
* increase index.
*/
int output = 0;
byte choice = map(analogRead(WAVESELECT), 0, 4096, 0, 4);
switch(choice){
case TRIANGLE:
output = gen_triangle(i);
break;
case SAW:
output = gen_saw(i);
break;
case SQUARE:
output = gen_square(i);
break;
case WHITENOISE:
output = gen_noise();
break;
case SINE:
output = gen_sine(i);
break;
}
analogWrite(output,AUDIO_PORT);
i++;
delayMicroseconds(analogRead(FREQ));
}
int gen_sine(int i){
return int(2048 + (2047 * sin((6.283185 / 4096) * (i % 4096))));
}
int gen_noise(){
return random(4096);
}
int gen_square(int i){
if(i < 2048){
return 0;
} else {
return 4095;
}
}
int gen_saw(int i){
return i % 4096;
}
int gen_triangle(int i){
if(i < 2048){
return i;
} else {
return declining_triangle[i-2048];
}
}
void gen_downward_slope(int slope[]){
int i = 0;
int value = 4095;
for(i = 0;i < 2048;i++){
slope[i] = value;
value -= 2;
}
}
The only problem is that when I run it I get this message over the Serial line:
assertion "duty <= pPwm->PWM_CH_NUM[ul_channel].PWM_CPRD" failed: file
"../source/pwmc.c", line 272, function: PWMC_SetDutyCycle
Exiting with status 1.
I found out (after some debugging) that it is the analogWrite line. I will look into it further.
I did comment out the line and serial printed the values passed to it. The generators seem to be working correctly, but I won't know until I can get an actual output from the DAC pins.