trarbab
Posts: 1
Joined: Wed Sep 12, 2018 7:58 pm

Auto Running Scratch Script

Wed Sep 12, 2018 8:06 pm

Hello,

I'm trying to automatically run a Scratch 1.4 program that uses GPIO pins to control switches and LEDs. I don't need a screen for the Pi to do its job, so I would like to have it turn the program on when I power to Pi. I've tried a couple methods that I found online, but they both seem to be for older OSes; I have the newest Raspian OS, but am using Scratch 1.4 because my Model B can't handle Scratch 2.

If anyone know how to make this work, I would really appreciate some help

pfletch101
Posts: 629
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Auto Running Scratch Script

Thu Sep 13, 2018 12:00 am

One common way of starting (what would otherwise be) user jobs on boot is to use crontab to add a line to the cron table:

Code: Select all

@reboot /home/pi/my_folder/myprogram
There are a few caveats - you sometimes need to put in a pause (typically 10-20 seconds is enough) at the start of the program to allow the system to complete its boot process and stabilize, and since cron runs in a rather 'cut down' environment, you need to make sure that you use full path specifications to reference any auxiliary files or other resources you want to use. You would replace "/home/pi/my_folder/myprogram" with the command to run your Scratch program.

hippy
Posts: 8239
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Auto Running Scratch Script

Thu Sep 13, 2018 4:49 pm

trarbab wrote:
Wed Sep 12, 2018 8:06 pm
I'm trying to automatically run a Scratch 1.4 program that uses GPIO pins to control switches and LEDs.
I am not sure that is possible.

You can probably automatically launch the Scratch 1.4 graphical IDE environment, perhaps even get it to load a specific Scratch 1.4 program when it starts, but I'm not sure you can get Scratch 1.4 to automatically run that.

There is no reason the IDE couldn't automatically 'click the green flag', start a program running once it's loaded it, but I didn't think there was any capability for actually doing that. You might be able to convince the developer to add that capability if it doesn't exist.

I would see if you can get it to do that before moving on to getting it do that when booting.

Unfortunately Scratch is a visual programming environment where a block's action is interpretively decided when the Scratch environment runs it. There is not often a compiler which can take those blocks and convert them to native executable code.

You can find some utilities to convert Scratch projects into things which can run natively, for example https://phosphorus.github.io, but I don't know if that supports 1.4 or whatever blocks you are using which are Pi specific.

You might be better off manually translating your Scratch program to Python or something which can be more easily executed from the command line, when booting. I suspect that would be your best course of action.

If you can point us to what you have found on line there may be additional help which could be given regarding that.

pfletch101
Posts: 629
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Auto Running Scratch Script

Thu Sep 13, 2018 5:20 pm

hippy wrote:
Thu Sep 13, 2018 4:49 pm
trarbab wrote:
Wed Sep 12, 2018 8:06 pm
I'm trying to automatically run a Scratch 1.4 program that uses GPIO pins to control switches and LEDs.
I am not sure that is possible.

You can probably automatically launch the Scratch 1.4 graphical IDE environment, perhaps even get it to load a specific Scratch 1.4 program when it starts, but I'm not sure you can get Scratch 1.4 to automatically run that.

There is no reason the IDE couldn't automatically 'click the green flag', start a program running once it's loaded it, but I didn't think there was any capability for actually doing that. You might be able to convince the developer to add that capability if it doesn't exist.

I would see if you can get it to do that before moving on to getting it do that when booting.

Unfortunately Scratch is a visual programming environment where a block's action is interpretively decided when the Scratch environment runs it. There is not often a compiler which can take those blocks and convert them to native executable code.
I haven't used Scratch, and did not realize that it was a purely graphical environment - the OP also said that: "I don't need the screen for the Pi to do its job". Cron certainly won't help if the Scratch environment requires user input to start a program.

hippy
Posts: 8239
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Auto Running Scratch Script

Thu Sep 13, 2018 6:51 pm

pfletch101 wrote:
Thu Sep 13, 2018 5:20 pm
I haven't used Scratch, and did not realize that it was a purely graphical environment - the OP also said that: "I don't need the screen for the Pi to do its job". Cron certainly won't help if the Scratch environment requires user input to start a program.
No problem. Scratch might require convincing that there's a frame buffer or other display, but even if used headless it should just work, much like any other program.

The issue is whether what we have allows what needs to be done having not been specifically designed with the intent of working that way. I haven't used Scratch 1.4 so not sure but have never seen anything which suggest it is possible.

With access to source code it might be possible for someone to hack it to work. Even possible to hack it to load a program file and auto-execute that with no screen rendering at all -

https://en.scratch-wiki.info/wiki/Scrat ... ource_Code

It is even possible to craft a compiler or converter to turn a saved program to something which is runnable, doesn't actually need to use Scratch after that at all. But I expect that's all beyond the OP's experience level. And probably a lot of work which might prove hard to find anyone to take on.

Once done though; you can then run it as suggested !

Return to “Troubleshooting”