Sketch delete automatically

can we make any code to delete sketch automatically after a time period using rtc.
i found some code using chat Gpt.

#include <RTClib.h>
int led=4;

RTC_DS3231 rtc;

unsigned long lastCheckTime = 0;  // Variable to track the last time check
const unsigned long checkInterval = 60000;  // Check interval in milliseconds (1 minute)
bool sketchDeleted = false;  // Flag to indicate if the sketch is deleted

void setup() {
  Serial.begin(115200);
  pinMode(4,OUTPUT);
  // Initialize RTC
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  // Check if RTC is running

    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
}

void loop() {
  // Get current time
  DateTime now = rtc.now();
 
  // Check if it's time to delete the sketch
  if (!sketchDeleted && now.unixtime() >= getTimeToDelete()) {
    deleteSketch();
  }
}

unsigned long getTimeToDelete() {
  // Return the time (in Unix timestamp) when the sketch should be deleted
  // In this example, we delete the sketch 1 minute after the sketch starts
  return rtc.now().unixtime() + 60; // 60 seconds (1 minute)
}

void deleteSketch() {
 digitalWrite(4,HIGH);
 delay(500);
 digitalWrite(4,LOW);
 delay(500);
 Serial.println("led working");
  Serial.println("Deleting sketch...");
  
  // Set the flag to indicate that the sketch is deleted
  sketchDeleted = true;
}

i have use an led with for debugging.

  // Code to delete or overwrite the sketch in flash memory
  // This operation may be system-dependent and may require additional libraries or tools
  // Exercise caution and ensure proper error handling

You need to ask your friend ChatGPT what to do here

2 Likes

Never modify a post after it has been replied to. Never.

If you have updated information, post it in a new reply.

sorry :smiley:, i have edit code but it`s not working still

1 Like

So I see

What do you expect the revised sketch to do ?

Your LED should be one of those explosive types.
Glue it onto the Atmega chip. :boom:

1 Like

Which arduino board? There is likely plenty of room to modify the bootloader to add code that could be invoked to wipe the section of flash memory that the sketch is stored in.

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