Hey everyone! I'm delighted to release the second edition of the StructureBot Arduino R4 SE model, designed specifically for Arduino UNO R4 boards. It's designed to be quick, custom and easily copy-pastable. ↓ Code ↓ #include <Arduino.h> #define MAX_TRAINING_SIZE 15000 #define MAX_WORDS 10 char trainingBuffer[MAX_TRAINING_SIZE]; int currentTrainingLength = 0; bool isTrained = false; void setup() { Serial.begin(115200); randomSeed(analogRead(A0)); memset(trainingBuffer, 0, MAX_TRAINING_SIZE); Serial.println("SBarduinoR4-se2"); Serial.println("Please provide training data to train SBarduinoR4-se2."); } void loop() { if (Serial.available()) { String input = Serial.readStringUntil('\n'); input.trim(); input.toLowerCase(); if (input.length() > 0) { if (!isTrained) { if (input.indexOf(' ') != -1) { addTrainingData(input); } else { Serial.println("Error: Training data requires multiple words (tokens)."); } } else { processMultipleSeeds(input); } } } } void addTrainingData(String data) { if (data.length() < MAX_TRAINING_SIZE) { strncpy(trainingBuffer, data.c_str(), MAX_TRAINING_SIZE - 1); trainingBuffer[MAX_TRAINING_SIZE - 1] = '\0'; currentTrainingLength = strlen(trainingBuffer); isTrained = true; Serial.println("--- TRAINING LOCKED ---"); Serial.print("Used: "); Serial.print(currentTrainingLength); Serial.println(" bytes. Send a prompt to generate an output with SBarduinoR4-se2."); } else { Serial.println("Error: Prompt too large."); } } void processMultipleSeeds(String input) { Serial.println("Generating..."); String finalOutput = ""; char inputBuf[128]; input.toCharArray(inputBuf, 128); char* seed = strtok(inputBuf, " "); while (seed != NULL) { finalOutput += generateSubSentence(String(seed)) + " "; seed = strtok(NULL, " "); } Serial.print("Output: "); Serial.println(finalOutput); Serial.println("------------------"); } String generateSubSentence(String seed) { String result = seed; String currentWord = seed; for (int i = 0; i < 3; i++) { String next = findNextWord(currentWord); if (next == "") break; result += " " + next; currentWord = next; } return result; } String findNextWord(String currentWord) { String nextWords[MAX_WORDS]; int nextWordCounts[MAX_WORDS] = {0}; int nextWordCount = 0; char* ptr = trainingBuffer; while ((ptr = strstr(ptr, currentWord.c_str())) != nullptr) { bool startOk = (ptr == trainingBuffer || *(ptr - 1) == ' '); char* endPtr = ptr + currentWord.length(); bool endOk = (*endPtr == ' ' || *endPtr == '\0'); if (startOk && endOk) { while (*endPtr == ' ') endPtr++; char nextWordBuf[32]; int idx = 0; while (*endPtr != ' ' && *endPtr != '\0' && idx < 31) { nextWordBuf[idx++] = *endPtr++; } nextWordBuf[idx] = '\0'; if (strlen(nextWordBuf) > 0) { String foundNext = String(nextWordBuf); bool exists = false; for (int j = 0; j < nextWordCount; j++) { if (nextWords[j] == foundNext) { nextWordCounts[j]++; exists = true; break; } } if (!exists && nextWordCount < MAX_WORDS) { nextWords[nextWordCount] = foundNext; nextWordCounts[nextWordCount] = 1; nextWordCount++; } } } ptr++; } if (nextWordCount > 0) { int total = 0; for (int j = 0; j < nextWordCount; j++) total += nextWordCounts[j]; int r = random(total); int sum = 0; for (int j = 0; j < nextWordCount; j++) { sum += nextWordCounts[j]; if (r < sum) return nextWords[j]; } } return ""; }
This is a simple copy-paste code for Arduino IDE, which works using C++. You no longer need to manually organize the training data into training data banks, the SBarduinoR4 SE-2 program automatically asks you for the training data through the serial monitor. Additionally, SBarduinoR4 SE-2 now supports multi-word prompts! No longer just a singular 'seed' word. SBarduinoR4 SE-2 is optimized to run on the following boards: - Arduino UNO R4 WiFi - Arduino UNO R4 Minima - Arduino NANO R4