I am new to Arduino and I am trying to use the signal generator shield MiniGen (https://www.sparkfun.com/products/11420, GitHub - sparkfun/MiniGen: Arduino Pro Mini shield using AD9837 to provide a small footprint signal generator.) to output AC signal. I am trying to control the board with a switch, so that when it is flipped the MiniGen will output a low frequency for 10 seconds and then a high frequency for 10 seconds.
So far, this works:
#include <SparkFun_MiniGen.h>
#include <SPI.h>
MiniGen gen;
int switchState1 = 0;
void setup() {
// put your setup code here, to run once:
gen = MiniGen(10);
gen.reset();
delay(2000);
gen.setMode(MiniGen::SINE);
gen.setFreqAdjustMode(MiniGen::FULL);
gen.selectPhaseReg(MiniGen::PHASE0);
pinMode(9, OUTPUT);
pinMode(2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
switchState1 = digitalRead(2);
if (switchState1 == 1){
static float frequency = 5.0;
unsigned long freqReg = gen.freqCalc(frequency);
gen.adjustFreq(MiniGen::FREQ0, freqReg);
}
}
However, this does not:
#include <SparkFun_MiniGen.h>
#include <SPI.h>
MiniGen gen;
int switchState1 = 0;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long previousMillis;
void setup() {
// put your setup code here, to run once:
gen = MiniGen(10);
gen.reset();
delay(2000);
gen.setMode(MiniGen::SINE);
gen.setFreqAdjustMode(MiniGen::FULL);
gen.selectPhaseReg(MiniGen::PHASE0);
pinMode(9, OUTPUT);
pinMode(2, INPUT);
previousMillis = millis();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
switchState1 = digitalRead(2);
currentMillis = millis();
lowFreq();
currentMillis = millis();
highFreq();
Serial.println(currentMillis);
}
void lowFreq() {
//
if (switchState1 == 1) {
if (currentMillis - previousMillis < (10000)) {
static float frequency = 5.0;
unsigned long freqReg = gen.freqCalc(frequency);
gen.adjustFreq(MiniGen::FREQ0, freqReg);
currentMillis = millis();
}
}
}
void highFreq() {
if (switchState1 == 1) {
if (currentMillis - previousMillis < (20000)) {
static float frequency = 15.0;
unsigned long freqReg = gen.freqCalc(frequency);
gen.adjustFreq(MiniGen::FREQ0, freqReg);
previousMillis += 20000;
}
}
}
I've gone through the examples using millis(), but I am having trouble implementing it here. Any help or advice would be greatly appreciated.