Serial.h library question

Hello all,
I'm using an AI code generator to finish a project. I'm trying to send text from a PC running Blender through the serial port to launch a sketch on my Arduino Uno R3. Right now I'm just trying to get it to blink a few LEDs for a few seconds.

The Code generator wants me to include Serial.h but I can't find that library and what I'm seeing is that the Serial.h library is included in the Arduino firmware?

Thanks for any help I can get.

Here's the code:

// Include the Serial library
#include <Serial.h>

// Define the pins for the LEDs
#define LED1 11
#define LED2 12
#define LED3 13

// Define the interval for the LEDs to flash
#define INTERVAL 2000 // 2 seconds

// Define the duration for the LEDs to flash
#define DURATION 8000 // 8 seconds

// Initialize the serial communication
Serial.begin(9600);

// Wait for the 'start' command to be received
while(!Serial.available()){}

// Read the incoming data
String command = Serial.readString();

// Check if the command is 'start'
if(command == "start"){

  // Set the LED pins as output
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);

  // Loop for the duration of the flashing
  for(int i = 0; i < DURATION/INTERVAL; i++){

    // Turn on the LEDs
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);

    // Wait for the interval
    delay(INTERVAL);

    // Turn off the LEDs
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);

    // Wait for the interval
    delay(INTERVAL);
  }

  // Turn off the LEDs
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
}

// If the command is not 'start', print an error message
else{
  Serial.println("Invalid command. Please send 'start' to begin flashing LEDs.");
}

Include of serial.h is not needed, but that code is not a compilable Arduino sketch anyway.

Make sure you tell the AI to create for Arduino Uno (if that is the board you are using).

And here's the poster child of why using AI to do something you don't know how to do yourself is a total waste of time.

Your AI is lying to you. It made stuff up. It gave you a confident sounding answer that is utter crap. There is no Serial.h to include. Feel free to look through the Arduino AVR core. There's no such file.

Also, as @bobcousins already pointed out, that isn't a valid sketch in any way, shape or form anyhow. Anyone with the utterly basic skills to make an Arduino blink an LED should have recognized that immediately.

There is no royal road to learning.

2 Likes

I suggest that you read Robin's updated Serial Input Basics Tutorial to get some ideas.

Once you understand the ideas you can apply them to what you want to achieve.

Thank you, I'll check out the tutorial

Thank you Bob, I did tell it which Arduino I'm using, I'm going to rework my request and see if I can improve it.

Just curious, what AI and prompt did you use?

With a few tweaks the sketch will work better :

// Define the pins for the LEDs
#define LED1 11
#define LED2 12
#define LED3 13

// Define the interval for the LEDs to flash
#define INTERVAL 2000  // 2 seconds

// Define the duration for the LEDs to flash
#define DURATION 8000  // 8 seconds

void setup()
{
  // Initialize the serial communication
  Serial.begin(9600);

  // Wait for the 'start' command to be received
  while (!Serial.available()) {}
}

void loop()
{
  // Read the incoming data
  if (Serial.available())
  {
    String command = Serial.readString();

    // Check if the command is 'start'
    if (command.substring(0, 5) == "start")
    {
      // Set the LED pins as output
      pinMode(LED1, OUTPUT);
      pinMode(LED2, OUTPUT);
      pinMode(LED3, OUTPUT);

      // Loop for the duration of the flashing
      for (int i = 0; i < DURATION / INTERVAL; i++)
      {
        // Turn on the LEDs
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);

        // Wait for the interval
        delay(INTERVAL);

        // Turn off the LEDs
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);

        // Wait for the interval
        delay(INTERVAL);
      }

      // Turn off the LEDs
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
    }
    // If the command is not 'start', print an error message
    else
    {
      Serial.println("Invalid command. Please send 'start' to begin flashing LEDs.");
    }
  }
}

Does anyone else find it concerning that some of the the words added to common usage in the early 21st century are -

Drone
AI
Bromance
….

The site is "https://www.duinocodegenerator.com/"

The prompt was something like:
Arduino Uno R3
3 LEDs on pins 11, 12 and 13
Blink for 2 seconds and stop after 8 seconds when the command "start" is sent through the USB

Thank you very much, that made it work correctly!

Report them for false advertising and/or incompetence.

@sonofcy
On second thought, why? The entertainment value here is immense!

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.