Background scripts

Hello all, I have a question that the internet hasn't been very helpful answering. I need to know how to make a background script. Specifically, I'm making a UI for a bore press, and I was told that the operator would like it if the LED were to blink under a specific set of circumstances. The most prominent specific example would be that when the interface boots up, he wants a screen that makes the machine home, and while it's waiting for the button to be pressed, the LED blinks. Putting in blinking scripts is problematic, because then button presses have to be timed in sync with the on/off cycle. If you need more information, feel free to ask. If you need my code, let me know and I'll share it.

Thank you for your help, Conan

Welcome to the Forum. Yes, we need to see your code. There are no "scripts" in Arduino programming. It's called "source code". Before posting, please read the two posts How to use this forum - please read. and Read this before posting a programming question ...

Please read the two posts at the top of this Forum on guidelines for posting here, especially the use of code tags which make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Many questions can be answered by simply reading the documentation which is provided with the IDE, available under the help tab, or online here.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like digitalWrite(3,0). But such a statement doesn't reveal anything about the purpose. digitalWrite(hornRelayPin, LOW) does. You can do that by declaring const byte hornRelayPin = 3; before setup() in your program. Many such names are already defined for you by the compiler and the Arduino IDE. Here are some:

#define HIGH 0x1
#define LOW  0x0
#define PI 3.1415926535897932384626433832795

Use them. There are many more. Use compiler math to compute values so you can see where they came from (or at least document them). For example, if you see the number 73, you would be hard put to explain the significance of it. But if you see "daysPerYear/5", it is obvious. One more thing. When you work on program continuously, you become familiar with it. So many things seem obvious even if they are not spelled out explicitly. But try looking at your own code six months later. It will be as if a stranger wrote it. So write for strangers, not yourself.

Sorry it’s taken me so long, I didn’t realize how messy my code was. It may still be messy, since as you’ve said, I’m familiar with this code and I may have overlooked something. I’ve attached the code below, both as a .ino and as a .txt for anyone who would rather view it as that. I haven’t gotten to the homing program yet, but I have tried to tinker with this section:

lcd.begin(20, 4);
  lcd.setCursor(1, 1);
  lcd.print("Estes Machinations");
  delay(1250);
  lcd.clear();
  lcd.setCursor(1, 2);
  lcd.print("Estes Machinations");
  delay(650);
  lcd.clear();
  lcd.setCursor(1, 3);
  lcd.print("Estes Machinations");
  digitalWrite(potPower, HIGH);
  digitalWrite(ledPower, HIGH);
  delay(650);
  lcd.clear();

to make the LED flash “Estes” in morse code. It was just a test, which failed and caused weird things to happen, so I removed it. However, the premise was kind of flawed in that I tried to take the total amount of delay and divide that up to blink morse code and to coincide with rewrites of the screen. I don’t believe that would work in the homing application, as there needs to be a constant blinking that can be interrupted by pressing a button, opposed to me pressing a button at the right time for the program to recognize that I pressed a button.

Thank you for your help,
Conan

MotorProject.ino (9.82 KB)

MotorProject.txt (10.2 KB)

Read the post at the top of the forum on how to do many things at the same time. You will need to scrap the rest of your code.

Mark

If you want code to be responsive to user input and to appear to do multiple things you cannot use the delay() function.

Have a look at how millis() is used to manage timing in Several Things at a Time.

...R