1

I have a string:

string s="digitalWrite(8,LOW);"

Is there any way to run it as a code?

askedMar 14, 2020 at 9:57
rktech's user avatar
4
  • Not an easy way, since you basically would need to write a C/C++ interpreter library for this. I don't know of such a library. Why do you need to do this? What are you trying to achieve?CommentedMar 14, 2020 at 10:30
  • To run commands that will be saved in external eepromCommentedMar 14, 2020 at 10:38
  • Do you really have such complex programs to save there, that you need C syntax text there? Instead you could build your own command structure, which can be way easier to interpret. What commands do you need to save in the EEPROM?CommentedMar 14, 2020 at 10:52
  • @chrisl thank you, I forgot that I can do it like thisCommentedMar 14, 2020 at 11:34

1 Answer1

2

This is actually pretty simple:

if (s == "digitalWrite(8,LOW);") {    digitalWrite(8,LOW);}

Obviously, it will not work iss contains any other string... If youwant something more general, that is able to interpret a wide range ofpossible commands, you will have to define a language, and write aninterpreter for that language. From the example you give, it seems youwould like your language to look like C++. This is most likely a baddesign choice. If you want an interpreter that understands the whole C++language: forget it. You will never fit something this big into anArduino Uno.

Here is, for inspiration,a very simple interpreter Iwrote that understand the following commands:

mode <pin> <mode>: pinMode()read <pin>: digitalRead()aread <pin>: analogRead()write <pin> <value>: digitalWrite()awrite <pin> <value>: analogWrite()echo <value>: set echo off (0) or on (1)

You can use it a basis for writing your custom interpreter. Otherwiseyou can do a Web search for “Arduino interpreter”: you should be able tofind interpreters implementing a wide variety of languages, includingcompact binary languages (Firmata),Forth (another one),Lisp,Basic, and evena C-like language.

answeredMar 14, 2020 at 11:37
Edgar Bonet's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.