I recently purchased an Arduino R4 Wifi (my second R4) and an R1 GIGA Wifi.
Ive been working on a personal project to use a DAC output (originally using my R4) to produce a sine wave output at a specific frequency, then delay, and increase the frequency by a specified increment.
I originally started with using my first R4, but i tried to measure ac voltage across a resistor using the analog input and I found out the hard way that they cannot accept a negative voltage input. So now i have my second R4 and my R1 GIGA.
Id like to use my R1 GIGA instead, purley because I want to, but that means i must use a different library and my code must change. I am having trouble modifying an example code i found for creating a sine-wave output, so that it will increase the frequency of the sine-wave by whatever increment i desire. Specifically, I want to include the same method of playing a frequency, waiting, then increasing it, like i did with my R4, but i am unable to get the integer i declared to work in the void loop. It only works if i place freq1= 100; in the void setup. My reason for wanting it to be in the void loop is so I can include the same method of increasing the value of freq1 as i did for my R4.
My teacher and I were able to create this code for my R4 and i previously made a different post about that. Here are both my code and link to my post, followed by the link for the example code for the GIGA and my modified version of that code:
Best way to generate sin waves with DAC R4 wifi
#include "analogWave.h"
analogWave wave(DAC);
int freq1;
void setup(){
Serial.begin(115200);
wave.sine(freq1);
}
void loop() {
freq1=15;
while (freq1<=200){
Serial.print(freq1);
Serial.println(" hz");
wave.freq(freq1);
delay(1000);
delay(1000);
freq1=freq1+5;
}
delay(1000);
}
GIGA Example code link: https://docs.arduino.cc/tutorials/giga-r1-wifi/giga-audio/
The specific section is Digital to Analog Converters(DAC), and the sub-section DAC Sine Wave.
Here is my modified version with comments:
#include <Arduino_AdvancedAnalog.h>
int freq1;
AdvancedDAC dac0(A12);
uint16_t lut[] = {
0x0800,0x08c8,0x098f,0x0a52,0x0b0f,0x0bc5,0x0c71,0x0d12,0x0da7,0x0e2e,0x0ea6,0x0f0d,0x0f63,0x0fa7,0x0fd8,0x0ff5,
0x0fff,0x0ff5,0x0fd8,0x0fa7,0x0f63,0x0f0d,0x0ea6,0x0e2e,0x0da7,0x0d12,0x0c71,0x0bc5,0x0b0f,0x0a52,0x098f,0x08c8,
0x0800,0x0737,0x0670,0x05ad,0x04f0,0x043a,0x038e,0x02ed,0x0258,0x01d1,0x0159,0x00f2,0x009c,0x0058,0x0027,0x000a,
0x0000,0x000a,0x0027,0x0058,0x009c,0x00f2,0x0159,0x01d1,0x0258,0x02ed,0x038e,0x043a,0x04f0,0x05ad,0x0670,0x0737
};
static size_t lut_size = sizeof(lut) / sizeof(lut[0]);
void setup() {
freq1= 100; //when placing freq1 equals 100 here instead of on line 35, a 100hz tone is generated
Serial.begin(9600);
if (!dac0.begin(AN_RESOLUTION_12, freq1 * lut_size, 64, 128)) {
Serial.println("Failed to start DAC1 !");
while (1);
}
}
void loop() {
freq1= 100; // When placing freq1 here instead of on line 20, the 100hz tone does not occur, and there is no output.
static size_t lut_offs = 0;
if (dac0.available()) {
// Get a free buffer for writing.
SampleBuffer buf = dac0.dequeue();
// Write data to buffer.
for (size_t i=0; i<buf.size(); i++, lut_offs++) {
buf[i] = lut[lut_offs % lut_size];
}
// Write the buffer to DAC.
dac0.write(buf);
}
}