I want to know if my code is well written

I am doing a project to graph the frequency response of an audio filter, for this I have to perform a frequency sweep (in my case from 0hz to 20khz) and I use an ad9833 module,

Im facing some problems, because of that im seeking for help...

The microcontroller that I am using is an esp32 and for the user interface I am using a program made in visual studio that takes the data from the micro and graphs it.

The program performs a sweep, and for each increase in frequency it reads the GPIO port 34 of the ESP, saves it in a JSON format vector of 200 samples and sends it through the serial port

The project has several parts, but I want to know if the code is well written to rule out the problem of programming the microcontroller, and continue with other problems.

The code is the following:

#include <AD9833.h>
#include <ArduinoJson.h>
#define FNC_PIN 13
#define StartFrequency 0
#define EndFrequency 20000
#define vectorLength 200
#define Lectura 34
AD9833 gen(FNC_PIN);
int miVector[vectorLength];
int frequencyStep = EndFrequency / vectorLength;


void setup() {
  Serial.begin(115200);
  gen.Begin();
  gen.EnableOutput(false);
}


void adquiereADC() {
  DynamicJsonDocument doc(1024);
  JsonArray jsonArray = doc.to<JsonArray>();

  for (int i = 0; i < vectorLength; i++) {
    int freq = StartFrequency + i * frequencyStep;
    gen.ApplySignal(SQUARE_WAVE, REG0, freq);
    gen.EnableOutput(true);
    delay(10);
    miVector[i] = analogRead(Lectura);
    jsonArray.add(miVector[i]);
    gen.EnableOutput(false);
  }
  String jsonString;
  serializeJson(doc, jsonString);
  Serial.println(jsonString);
}


void loop() {
  if (Serial.available() > 0) {
    String request = Serial.readStringUntil('\n');
    if (request == "GetData") {
      adquiereADC();
    }
  }
}

not a comment about the style of the code

not sure using a square wave is a good approached for determining the responses at that frequency because a square wave is actually made up of many frequency harmonics (see spectrum of square wave)

another question is why is there only a 10 ms delay between generating the signal and measuring it. 10 msec is just 10 cylcles at 1 kHz, just a single cycle at 100 Hz

and why not structure the code to make the measurement and then format those values into some JSON message

1 Like

How can we know what you consider to be well written? As a professional, I think any program that works is well written. If it works and you muck about with it you will never be satisfied.

If you think something might be done differently. the start over with a totally fresh program and get it to work the new way.

What does your testing show? Does it work or not?

1 Like

What do you think the result of a single analogue read will be? I suspect not what you want.

A schematic would help: I'm making assumptions about how you have this connected up, and assumptions have a habit of being wrong.

What sort of problems?

I'm not a programmer, so almost all the code was written by chatgpt, so I couldn't tell you why it's only 10ms, maybe I should increase the delay time?

I'm using square waves temporarily, because sine waves are very distorted, but I'm working on it anyway.

And yes, you are right, it would be better to separate the part of the code that takes the data and another part of formatting in JSON

That immediately tells me you do not want to learn how to program because it might take too long. So good luck.

2 Likes

By well written I mean that if the code makes sense or is well structured by a professional... that is, 1- frequency sweep, 2- data collection, 3- formatting in JSON, etc...

I have problems when graphing the response that the esp32 reads in visual studio, instead of having a bode type function, I have a series of pulses that come from the ad9833, as if they were ignoring the filter that is being tested

I have problems when graphing the response that the esp32 reads in visual studio, instead of having a bode type function, I have a series of pulses that come from the ad9833, as if they were ignoring the filter that is being tested

That explains the lack of sensible and useful comments.

instead of having a bode type function, I have a series of pulses that come from the ad9833

That makes no sense, but given that you didn't write the code, that should not be a surprise.

thanks?

I didn't write the code... but I understand each line, but I would like to know if they are well structured due to the problems I am facing

The code is wrong. What more do you need to know?

can you tell me whats wrong?

Start here:

Write code that does what you want it to do.

thank you for your help...it was very helpful

Does it make sense to start/stop your generator each reading?

What filter???

You have to understand that there is a real distaste for people who don't know how to code that go and get ChatGPT code and then want us to fix it for them. ChatGPT has been a bit of an issue in these circles and you are spot on indicative of the exact type of user that is the real annoyance with it. I don't mean to be rude, you probably didn't know that when you got here. But that's the reason you're being received the way you are.

We like helping people learn to code. We like helping other coders fond problems. We don't like proofreading for the robot.

If you want to learn to code and write code for yourself then this is the place to come. If you just want the code written for you and ChatGPT isn't good enough then pay someone. If you want ChatGPT code then go ask the robot to fix it.

Please understand that it's nothing personal. If you have specific questions about the code or how it works then certainly feel free to ask them. But you're probably not going to get someone to just fix the ChatGPT code for you. Go back to ChatGPT and keep asking if that's all you want.

4 Likes

It is not personal. Indeed, but I'm new to this, no one helps me and I have to start somewhere, I've already looked for a lot of information in many places, I don't know people in person with the same hobby as me and I haven't had good results...
That's why I decided to come to this forum looking for someone who wants to help a newbie, but now I'm going to continue on my own...

It's obvious that the people who make up this platform are very good at what they do, so it shouldn't have been difficult to read a code written by a computer or anyone else, your answer could have helped me, a shame.

I came to this forum with good intentions to clear up a doubt... and I leave as I came... I didn't know that people in ArduinoForums are so toxic.

It's not strange that a lot of people here has a reputation for being like that... I didn't believe it, but now I do.

you won't see me here anymore, im sorry
now i feel like crap... thanks