I am new to arduino and am working with the Due simple waveform generator example. I was able to get the potentiometer to work properly, but now I am having trouble with the push buttons. From the explanations in the example, it seems that the push buttons are supposed work independently and switch the waveform on its respective output. However, only one push button is functioning (push button associated with the DAC1 output) and switches the waveform on both outputs rather than just its respective output.
Is there a way to get both push buttons working properly and independently?
If there is another forum clarifying this issue then I apologize for reposting, I have not been able to find such a forum that addresses this issue.
I have double, triple, and quadruple checked the wiring. It is wired correctly.
This is the code I am currently running. I have made minor changes but have indicated where the changes are made
/*
Simple Waveform generator with Arduino Due
* connect two push buttons to the digital pins 2 and 3
with a 10 kilohm pulldown resistor to choose the waveform
to send to the DAC0 and DAC1 channels
* connect a 10 kilohm potentiometer to A0 to control the
signal frequency
*/
#include "Waveforms.h"
#define oneHzSample 1000000/maxSamplesNum // sample for the 1Hz signal expressed in microseconds
const int button0 = 2;
const int button1 = 3;
volatile int wave0 = 0;
volatile int wave1 = 0;
int i = 0;
int sample;
//int t_sample; //useless, without this line the potentiometer works - TD
void setup() {
// pinMode(2, INPUT); //Defining pin modes as input or output - TD
// pinMode(3, INPUT); //Does not help
// pinMode(DAC0, OUTPUT);
// pinMode(DAC1, OUTPUT);
analogWriteResolution(12); // set the analog output resolution to 12 bit (4096 levels)
analogReadResolution(12); // set the analog input resolution to 12 bit
attachInterrupt(button0, wave0Select, RISING); // Interrupt attached to the button connected to pin 2
attachInterrupt(button1, wave1Select, RISING); // Interrupt attached to the button connected to pin 3
}
void loop() {
// Read the potentiometer and map the value between the maximum and the minimum sample available
// 1 Hz is the minimum freq for the complete wave
// 170 Hz is the maximum freq for the complete wave. Measured considering the loop and the analogRead() time
sample = map(analogRead(A0), 0, 4095, 0, oneHzSample);
sample = constrain(sample, 0, oneHzSample); //changes t_sample to sample - TD
analogWrite(DAC0, waveformsTable[wave0][i]); // write the selected waveform on DAC0
analogWrite(DAC1, waveformsTable[wave1][i]); // write the selected waveform on DAC1
i++;{
if(i == maxSamplesNum) // Reset the counter to repeat the wave
i = 0;
}
delayMicroseconds(sample); // Hold the sample value for the sample time
}
// function hooked to the interrupt on digital pin 2
void wave0Select() {
wave0++;
if(wave0 == 4)
wave0 = 0;
}
// function hooked to the interrupt on digital pin 3
void wave1Select() {
wave1++;
if(wave1 == 4)
wave1 = 0;
}
I have commented out the pin modes because they did not make a difference. I tried adding them in hopes that by defining the pins the push button associated with the DAC0 output would work, but no such luck.
I have not tried printing the wave0 and wave 1 values. Is this as simple as serial.print(wave0), or is it more in-depth?
teden:
I have commented out the pin modes because they did not make a difference. I tried adding them in hopes that by defining the pins the push button associated with the DAC0 output would work, but no such luck.
They default to inputs, so you need to set them as outputs.
I have not tried printing the wave0 and wave 1 values. Is this as simple as serial.print(wave0), or is it more in-depth?
Yes, except you are inside interrupts when you change a wave value, so you need to set a flag and call Serial.print() from the main loop.
just have a
byte waveValueChanged = 0;
then when you modify a wave value, set it to 1.
In the main loop, check for 1, and if so, print the wave values then set the waveValueChanged back to 0.
I am very new to arduino so I have a general idea of what you (arduinodlb) are explaining but do not have enough experience with arduino to fully understand. I don't have the best understanding of how to actually code this but I would like to attempt it on my own first. However, I would greatly appreciate it if you could just give more details on where to place these lines of code.
More specific questions:
What is a flag and how can a flag be set?
Is "byte waveValueChanges=0;" the actual line of code needed or is it written without "byte"?
Does the location of these lines of codes in the main loop matter?