DUE Script Coding Intro

The DUE scripting language allows devices to run independent from any host. This very easy-to-learn scripting language is inspired by BASIC and Python, giving the user the simplicity and flexibility they need. DUE Script is part of the DUE Link platform.

Prerequisite

This lesson assumes you have completed the system setup as shown on the main DUE Script page.

Objective

This lesson is a quick introduction to the language. The goal at the end of this lesson is to write a program that counts 0 to 7. And then we’ll modify the program to print 0 to 7 but skip number 4.


Case Sensitive

DUE Script is not case-sensitive, but what does that mean?! This means the language does not care how things are spelled. For example, pRint() and Print() are exactly the same.


Comments

A comment is a group of words or sentences we can add to our code that don’t affect how it runs. The computer will ignore the comment and run the code only. We use comments to tell us what’s happening in the code we’ve written or how something is done so others can understand the code we’re creating.  DUE Script uses # to start a comment and everything after that # will be ignored.

To make comments easier to spot by our eyes, they are usually colored differently.

Comments can also be useful for testing different sections of code by commenting and uncommenting lines and viewing the results.

# This is a comment. 
# Comments are useful to remind us of what our code is doing. 
# The computer will ignore comments completely.
 
Log("Hello")
# Log("Goodbye") # This line of code won't run. 

Log() is a function that will “print” whatever it gets. In this case we gave it a string “Hello” and it printed that to the output.


LCD Output

Going forward, we will use Print() instead of Log() which prints the ouytput to the LCD instead of hte console output.

Print("Hello")

PrintLn() is same as Print() but it also add a new line. Note that written text does not wrap. Anything beyond the screen width will not show.


Variables

A variable is a spot in the computer memory to help the program in memorizing something. DUE Script does not allow for creating variables. Instead, it includes variables for each letter in the alphabets, A to Z.

x = 5
PrintLn(x)

Can you guess why Print() showed the value of x, which is 5, instead of printing the letter X? Remember that X and x are the same thing because DUE Script is not case sensitive.

x = 5
PrintLn(X)

Let’s make a program that takes two numbers and then adds them up. The two numbers will be stored in variables a and b.

a = 5
b = 4
PrintLn("The sum of a and b is:")
PrintLn(a+b)

More on variables in other lessons.


Loops

So far, all of our programs have executed a small program and then terminated. However, most programs will continue to run, handling specific tasks repeatedly. This happens in a loop that executes a specific block over and over.

DUE Scripts does not support code blocs. It implements loops in a simple way, similar to BASIC. The code will contain a label and that label can be use to shift code execution. For loops are also supported and are also similar to BASIC for loops.

For i=0 to 7
  PrintLn(i)
Next

The code did indent PrintLn() (add spaces before it) but this is not needed and ignored by DUE Script engine. We sometimes add those spaces because it is easier for us to understand the code.

More on loops in other lessons.


Operators

Operators are used for assigning values, like when we used = symbol, and for evaluating/comparing two values, like when we used the > symbol. There are other operators to help us mathematically, like the + symbol, and others to help with logic checks.

More on Operators in other lessons.


Flow Control

In some cases, we need to check for condition and take action if that action is true. How do we check “if” something is true? The answer is exactly what you may have guessed, an if statement!

This code will print “one” if x equals 1.

If x=1
  PrintLn("one")
End    

Let’s now combine the earlier for loop with the if statement, say we want to print numbers 0 to 7, but this time we want to skip 4! Meaning, we will PrintLn() the number if the variable i does not equal 4.

For i=0 to 7
  If i != 4
    PrintLn(i)
  End
Next

In this example, the for loop that counts 0 to 7. Inside of it is an if statement that prints the number if it is not 4. In most coding languages, = is equal and != is not equal.

The results? All numbers show except for 4.

More on flow control in other lessons.


Functions

Functions are softly implemented in DUE Script. The built-in functions work very similarly to full languages as they take arguments and return results. Print() is a function that takes an argument and prints it.

Print("Hello World!")

Go ahead and try this code. You can change it to print your name, and even add multiple lines.

PrintLn("Hello, my name is:")
PrintLn("BrainPad")

Print() and PrintLn() can also print numbers or the results of a mathematical calculation. The first line will print 55 and the second will print 10.

PrintLn(55)
PrintLn(5+5)

Developers can’t create functions but they can create subroutines. More on functions/methods in other lessons.



BrainStorm

Computers are not as smart as we may assume. They run instructions step-by-step. But if so, how does a computer play music while surfing the web and checking emails? They do so by running billions of instructions per second. It feels like things are happening simultaneously, but it’s just a very fast simple machine.

How do computers compare to our brains? Do you think we process one instruction at a time? How about feelings? Computers always have an answer, but how come we are sometimes unsure? How do we make decisions that we feel confident about, but then we regret those decisions later? Would a computer regret a decision? What is “regret” to a computer if all it knows is a yes or no?

Content Licensing
Newsletter

Twitter Feed
Hot off the press!
January 30, 2024
December 14, 2023
December 11, 2023
December 8, 2023
November 24, 2023