another noob trying to debug some code ai threw up on me - Page 1
Forum section: electronics / general

author: drspastic
author_rank: Contributor
timestamp: December 30, 2024, 12:03:17 am
content: so i played with a little arduino code before and got some examples twisted to do my bidding but im over my head here. im using lolin 32 lite and want it to let me enter a value in a webpage and output a voltage from the dac. ultimately it will generate a 2.4ghz signal going to a mixer but at this stage i just want to test how stable (or not) the output will be.anyway i thought to try ai and it spewed this:#include <WiFi.h>#include <WebServer.h>// Access Point credentialsconst char* ssid = "vco";const char* password = ""; // Use a strong password (minimum 8 characters)// Web server running on port 80WebServer server(80);// HTML for the webpageconst char webpage[] PROGMEM = R"rawliteral(<!DOCTYPE html><html><head><title>DAC Control</title><style>body { font-family: Arial; text-align: center; margin-top: 50px; }input { width: 80px; height: 30px; font-size: 16px; }button { height: 35px; font-size: 16px; }</style></head><body><h1>ESP32 DAC Control</h1><p>Enter a value (-255) to set the DAC output:</p><form action="/set" method="get"><input type="number" name="value" min="" max="255" required><button type="submit">Set DAC</button></form></body></html>)rawliteral";// DAC output pinconst int dacPin = 25;void handleRoot() {server.send(200, "text/html", webpage);}void handleSetValue() {if (server.hasArg("value")) {int value = server.arg("value").toInt();Serial.print("Received value: "); // Debug printSerial.println(value); // Debug printif (value >= && value <= 255) { // Corrected comparisondacWrite(dacPin, value); // Write the value to the DAC pinserver.send(200, "text/plain", "DAC output set to " + String(value));} else {server.send(400, "text/plain", "Invalid value. Enter a number between and 255.");}} else {server.send(400, "text/plain", "Value not provided.");}}void setup() {// Initialize serial for debuggingSerial.begin(115200);// Set up DAC pinpinMode(dacPin, OUTPUT);// Initialize WiFi in Access Point modeWiFi.softAP(ssid, password);Serial.println("Access Point started.");Serial.print("AP IP address: ");Serial.println(WiFi.softAPIP());// Define web server routesserver.on("/", handleRoot);server.on("/set", handleSetValue);// Start the serverserver.begin();Serial.println("Web server started.");}void loop() {// Handle incoming client requestsserver.handleClient();}and it returned this:/home/ide/Desktop/vco2/vco2.ino: In function 'void handleSetValue()':vco2:46:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]46 | if (value >= && value <= 255) { // Corrected comparison| ~~~~~~^~~~~~~~~~~~vco2:46:22: error: label 'value' used but not defined46 | if (value >= && value <= 255) { // Corrected comparison| ^~~~~exit status 1ISO C++ forbids comparison between pointer and integer [-fpermissive]this was the best it could do. the webserver part actually works, but no peanut for the rest.so any ideas what i need to do? or just slate me for trying to let a computer do the work.im old btw, and used to learn quite well in the days gone by crashing games and playing with the code. if i see the code as it is supposed to be i can usually work out what bits to editedit btw i do have the code running by commenting out the offensive idiot check lines, which are not really needed, i just want to know how they SHOULD be formatted if they were to work.

author: Psi
author_rank: Super Contributor
timestamp: December 30, 2024, 12:06:56 am
content: AI should be able to fix that error if you feed it back in along with the error message.It's a very trivial mistake, the 0 is missing and the brackets should be better.The line should beif (value >= 0) && (value <= 255)I've not gone through the logic, but that is why it wont compile

author: Whales
author_rank: Super Contributor
timestamp: December 30, 2024, 12:20:36 am
content: Quote from: Psi on December 30, 2024, 12:06:56 amSpoken like a true AI, your suggestion is wrong and won't compileif (value >= && value <= 255) // Badif (value >= 0) && (value <= 255) // Badif (value >= 0 && value <= 255) // Worksdrspastic:You have two comparison operations. The second one makes sense "value <= 255", but the first one is incomplete "value >=". Every comparison compares two things.Psi:"If" statements can't have multiple sets of brackets after them in C or C++. This would work in python, so perhaps that's where your mind is.

author: drspastic
author_rank: Contributor
timestamp: December 30, 2024, 12:35:22 am
content: great, i will throw that up the flagpole tomorrow and see if anyone salutes.as for the suggestion to ask ai, well of course i tried that 4 or 5 times before asking.in the end i just commented the lines out to get it working because i know not to enter more than 255 into it!anyhow its working enough to test signal stability as is. its for a qo100 satellite uplink so i cant have the signal wandering all over the transponder. my backup option is to use mcp4725 dac for 12 bit resolution. if resolution is good enough i might be able to splice in code i adapted a few years ago to do rtty mode and feldhell but thats another story.btw, thanks guys for not reaming me out for using ai and asking for a fix

author: MarkF
author_rank: Super Contributor
timestamp: December 30, 2024, 12:43:33 am
content: Quote from: Whales on December 30, 2024, 12:20:36 amThis is not true. You can have multiple sets of brackets.What you can't have is multiple conditional statements for the 'if'.if (value >= 0 && value <= 255) // this is okaybut if you wanted to be more explicit:if ((value >= 0) && (value <= 255)) // this would make more sense for a more complex condition

author: drspastic
author_rank: Contributor
timestamp: December 30, 2024, 11:36:00 pm
content: well i tried both answers and it just threw errors in other places. so i stayed with the version where i deleted that whole sanity check <255 routine.then i failed the sanity check overall because in the test circuit (which generates 2.4ghz frequencies) as soon as i got the esp32 to output almost the right dac voltage to get the exact output frequency i needed, the output frequency jammed the esp32 wifi and kicked me from the web console.so im going to have to think of another plan not involving wifi.thanks anyway. it was fun

author: Psi
author_rank: Super Contributor
timestamp: January 01, 2025, 01:27:11 am
content: Quote from: Whales on December 30, 2024, 12:20:36 amops yeah, in my haste after seeing the missing zero I forgot the final brackets.if (value >= && value <= 255) // Badif (value >= 0) && (value <= 255) // Badif (value >= 0 && value <= 255) // Works but still hard to readif ( (value >= 0) && (value <= 255) ) // That's better.