I've written a sketch that is quite extensive and I want to make it useable for a not-so tech-savvy audience (e.g. my colleagues). So I thought, wouldn't it be great if I could write a program that lets users input their variables (e.g. pin numbers, control thresholds, names, etc.) and that then assembles the sketch accordingly. Something very simple like a Python (tkinter) or Processing program that creates a popup with input fields for variables which are then pasted in the sketch. The output should be a .ino file with all the important variables in all the right places.
I'm thinking that something like this should already exist but I couldn't find anything about this (except for Pauls brilliant audio system design tool which I don't know how to program: Audio System Design Tool for Teensy Audio Library).
If anybody can give me a shove in the right direction, I'd be very thankful. Otherwise, I'll try to start from scratch.
Do you want the PC program to take values from a user then actually write the sketch using those values ? If so, then how will it get onto the Arduino and get compiled and uploaded ?
Or more likely, there is a sketch running on the Arduino that reads user input from the PC application then applies user values to the sketch that is already running. This would be like prompting the user for hour, minute and second then setting the time of an RTC and running the already loaded clock sketch
Hi, those are two valid options. I'd currently prefer to pre-create the complete sketch with the program and then compile and upload it through the Arduino IDE. This should be doable, even for beginners.
The reason why I would not prefer to have the arduino sketch read user input is that the system is running more or less autonomously (measuring, controlling, logging data to SD card) and might need to be restarted without losing all the variables.
It's the option that you mention first: the PC program takes values from a user and creates the sketch. The user can then compile and upload the sketch with the Arduino IDE without having to write code themselves.
However, creating a "configuration" file on the SD card might be way more simple. That's a great idea, I'll check out this option
edit I'd still be interested in ways to create a sketch with a graphical interface
There are several possibilities:
You could just use an rs232 terminal to send simple commands to the Arduino to change settings (which are then stored in EEPROM). This would require the Arduino rs232 drivers to be installed on the computer which could get difficult if you want to use it on other computers.
You could use an ESP32 and have a web interface they can interact with.
You could have as suggested an sdcard where the settings are stored in a text file (json even)
Have a display and rotary encoder with a menu system they can use to change settings
If it is just very simple settings they need to be able to alter you could use a row of dip switches.
That, of course, is not an Arduino specific question. What you are asking for is a program running on a PC that prompts a user for some parameters then writes out a text file that includes the answers to the questions.
As I say, not an Arduino question as the program will be running on the PC
Thanks for the helpful answers, I will first see if I can get there with the settings file on the SD card.
I still believe that a program running on a PC that creates Arduino sketches in a modular way (e.g. add specific chunks of code based on the application) might have certain benefits, as it creates sketches that are fitted to the purpose, which might be more memory-efficient and versatile.
This might be OT but I thought this subforum was specifically for interfacing with non-Arduino software.
Have you written a Python program that can read and do something with the Arduino code file you have written? The Python program would have to look for and replace a word in the Arduino code with the word the user wanted. Can/have you done something like that just to prove your proposal?
I have not - which is why I was asking whether somebody could provide information on this. However, I was inspired by the tool that Paul Stoffregen created for the Audio library, which does something like what I imagined:
That will not work. The Python or Processing sketch must transfer the information into to Arduino during run time as well as the Arduino answering back again in run time.
So you need to communicate between the two using the serial interface.
So look for examples of serial communication between the two.
Two points
Not very relevant
Way way too complex, learn to crawl before you try to fly.
Why not maintain the Python line of thinking. Create a Python PC app that programs a microcontroller that has Micropython firmware. Micropython has a built in interface called REPL (Read Eval Print Loop) that allows you to type and enter valid Micropython commands from a PC terminal or GUI.
This is not c/c++ but if c is what you want then the best course of action is to use the existing Arduino IDE in my opinion. On the other hand if you are teaching the basic principals of microcontroller programming to a not so "tech-savvy" audience then Python and the REPL are ideal.
If you enable verbose output during compilation and verbose output during upload in file -> preferences in the IDE, you can see what the IDE does under the hood when you do an upload.
Your PC program can do the same things. I would in that case advise that all user defined "settings" will be in a separate file thay will be compiled together with the sketch.
You will have to harden the PC application so the compile process can not fail due to invalid user input.
Be aware that flash memory can only be written 10,000 times (worse case scenario); every upload counts as one. Therefore it might be better to use the option where the sketch allows users to enter the "settings" and the sketch stores them in intetnal or external EEPROM or in FRAM. Your sketch can then read them from there. EEPROM can be written 100,000 times, FRAM basically unlimited.
An .ino is simply a text file with a .ino file extension, so all you need to do is to generate some text and put it into a file.
I often use a simple Processing program for inputting a .wav sound file and getting the syntax I need to generate a lookup table printed out. I then do a copy on the console output and then paste it into my Arduino sketch, or text editor (I ma on a Mac so I use TextEdit) and give it a .ino file extension. But I could, instead of printing it out, get Processing to write it out to a text file with an .ino file extension.
This is the Processing code to do this:-
/**
* Sound Sample
* by Mike Cook
* Converts 16 bit .wav file
* into 8 bit .h file for Arduino sample player
* for Processing 3.x
*/
PrintWriter output;
String waveName="yes";
String loadPath="blank";
String savePath="blank";
void loadFile(){ // load waveform definition from disc
selectInput("Choose wav file","doLoadFile"); // Opens file chooser
}
void doLoadFile(File selection){
if (selection == null) {
println("No file was selected...");
} else {
loadPath = selection.getAbsolutePath();
println(loadPath);
saveFile(); // place to save results
}
}
void saveFile(){ // load waveform definition from disc
selectOutput("Save converted file","doSaveFile"); // Opens file chooser
}
void doSaveFile(File selection){
if (selection == null) {
println("No file was selected...");
} else {
savePath = selection.getAbsolutePath();
waveName = savePath;
// nibble away at the path name until we just have the file name
while(waveName.indexOf('/') != -1) {
waveName = waveName.substring(1, waveName.length() );
}
println("wave name "+waveName);
convertWave();
}
}
void setup()
{
loadFile();
}
void convertWave(){
int lsb, msb, a;
int max=0, min =0;
float scale;
byte sample[] = loadBytes(loadPath);
if(sample[8] != 87 || sample[9] != 65 ||sample[10] != 86 ||sample[11] != 69){
println("Error - "+loadPath+" is not a .wav file");
exit();
}
// Print each value, from 0 to 255
for (int i = 44; i < sample.length; i +=2) {
// bytes are from -128 to 127, this converts to 0 to 255
lsb = sample[i] & 0xff;
msb = sample[i+1] & 0xff;
a = lsb | (msb << 8);
if( (a & 0x8000) != 0) a= a | 0xFFFF0000; // sign extend
if( a > max) max = a;
if( a < min) min = a;
}
println("max = " + max +" min = "+min);
scale = 255.0/((float)max - (float)min);
println("scale = "+ scale);
int dataLength = (sample.length -40) / 2;
println("data length = 0x"+ hex(dataLength)+" or "+dataLength+" bytes");
msb = (dataLength >> 8) & 0xff;
lsb = dataLength & 0xff;
// output the sample file
output = createWriter(savePath+".h");
output.print("const PROGMEM byte "+waveName+"[] = { 0x" + hex((byte)lsb) + ", 0x" + hex((byte)msb) + ", ");
for (int i = 44; i < sample.length; i +=2) {
// Every tenth number, start a new line
if (((i+8) % 20) == 0) {
output.println();
}
// bytes are from -128 to 127, this converts to 0 to 255
lsb = sample[i] & 0xff;
msb = sample[i+1] & 0xff;
a = (lsb | (msb << 8));
if( (a & 0x8000) != 0) a= a | 0xFFFF0000;
a = int((float)a * scale) + 128;
if(a>255) a = 255;
if(a<0) a = 0;
output.print("0x"+hex(a).substring(6,8));
if(i < sample.length - 2) output.print(", ");
}
// finish it off
output.print(" };");
output.flush(); // Write the remaining data
output.close(); // Finish the file
println("saved at "+savePath+".h");
exit();
}
void draw(){ }
The default path for the .wav file is a data folder inside the sketch folder, but could be anywhere.
I would suspect the concept behind the many drag & drop Arduino Block GUIs would be similar for what you are looking for: just add a bunch of templates with appropriate drop-down property controls for variables. Drag And Drop Programming Gets Kids Started Early | Hackaday
Sounds rather straightforward but lots of templates needed to drive the GUI.
Thank you, lots of useful information here for me!
That is exactly what I was thinking about, thank you for the code example!
This would be helpful for a more comprehensive solution (i.e. create sketch and upload with python program) -> maybe in the future (I guess there are more specialized threads/tutorials for this).
I completely forgot that there's always the option to store some data in EEPROM...
More options that I was unaware of...
For my application, the solution that I can realize the fastest is to create a configuration file on the SD card which is then parsed by the Arduino sketch. Ppl can just open it in the text editor and change it according to their needs.
In my free time, I will explore some of the other options that you raised here