Hi guys, I have this code I’ve been working on and it’s working perfectly fine. I’ve been trying to modify it in order to save the voltage values in an array, but it looks like I’m doing something wrong.
I was trying to do two things:
-
at the same time, I save the voltage values in the SD and in an array;
-
after I save the voltage values in the SD (the code already does that), I read it outside of the loop and save it in an array.
I also have to use a simple if to determine if the I’m getting the first values (data000.txt) to be my reference, or anything after that (data001.txt, data002…), which I will use to compare with my reference.
After I compare them, I’ll do something like if comparison < 2, green LED turns on, else, red LED turns on.
And by comparison, I mean sum(([data001]-[data000])ˆ2).
What do you guys suggest?
Thanks.
#define W_CLK 5 // Pin 8 - connect to AD9850 module word load clock pin (CLK)
#define FQ_UD 9 // Pin 9 - connect to freq update pin (FQ)
#define DATA 6 // Pin 10 - connect to serial data load pin (DATA)
#define RESET 7 // Pin 11 - connect to reset pin (RST).
char filename[20];
#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
int n=300;
int sensorValue[300];
const int chipSelect = 10;
#include<SD.h>
// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data)
{
for (int i=0; i<8; i++, data>>=1) {
digitalWrite(DATA, data & 0x01);
pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high
}
}
// frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
void sendFrequency(double frequency) {
int32_t freq = frequency * 4294967295/125000000; // note 125 MHz clock on 9850
for (int b=0; b<4; b++, freq>>=8) {
tfr_byte(freq & 0xFF);
}
tfr_byte(0x000); // Final control byte, all 0 for 9850 chip
pulseHigh(FQ_UD); // Done! Should see output
}
void setup() {
// configure arduino data pins for output
pinMode(FQ_UD, OUTPUT);
pinMode(W_CLK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(RESET, OUTPUT);
pulseHigh(RESET);
pulseHigh(W_CLK);
pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10
Serial.begin(9600);
Serial.print("Initializing SD Card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
int l=0;
snprintf(filename, sizeof(filename), "data%03d.txt", l); // includes a three-digit sequence number in the file name
while(SD.exists(filename)) {
l++;
snprintf(filename, sizeof(filename), "data%03d.txt", l);
}
File dataFile = SD.open(filename,FILE_READ);
dataFile.close();
Serial.println(l);
Serial.println(filename);
}
void loop() {
for (unsigned long fq = 20000; fq<= 40000; fq+=10) {
sendFrequency(fq); // freq
//while(1);
int x=0;
int sensorPeakSum = 0;
for (int i = 2; i<= n; ++i) {
sensorValue[i] = analogRead(A0);
if (sensorValue[i-2] < sensorValue[i-1] & sensorValue[i-1]>sensorValue[i]) {
sensorPeakSum+= sensorValue[i-1];
x=x+1;
}
else {
}
}
float voltage = (sensorPeakSum/x)* (10.0/1023.0);
File dataFile = SD.open(filename, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(voltage);
dataFile.print(",");
dataFile.print(fq);
dataFile.println();
dataFile.close();
// print to the serial port too:
Serial.print(voltage);
Serial.print(",");
Serial.print(fq);
Serial.println();
}
// if the file isn't open, pop up an error:
else {
//Serial.println("error opening file");
}
delay(1);
}
while (1) {}
}