Explanation and Uses of Arrays.

Step 4. Script Array.

So we have our ultrasonic sensor array set up on our robot, but we need a script to make the sensor array work. If we had one sensor fitted to our robot, we could have the following script running...

# Monitor 1 ultrasonic sensor for detection
# mode. When a value is less than 30,
# it will execute the "Detection" part of the script.

:Loop

Forward()

$ping1 = GetPing(d1, d2)

# This "IF" script command will be referred to as
# an object for an array.

if ($ping1 < 30)
  Goto(Detection)
endif

Sleep(1000)

Goto(Loop)

:Detection

Sleep(1000)

Say("Object detected.")

Left(70,2000)

sleep(2000)

Goto(Loop)

A simple script monitoring one sensor that takes action by saying that an object has been seen, turns left at a low speed for two seconds, then starts monitoring again. This is not really an array yet, as the script only has one "object" to detect one sensor.

We need to set up an array script, so we start with the following...

:Loop

Forward()

$ping1 = GetPing(d1, d2)
$ping2 = GetPing(d3, d4)
$ping3 = GetPing(d5, d6)
$ping4 = GetPing(d7, d8)
$ping5 = GetPing(d9, d10)

This is not an array yet, just the start of one. So now we need to add some "Objects" to make it an array...

if ($ping1 < 30)
  Goto(Detection)
endif

if ($ping2 < 30)
  Goto(Detection)
endif

if ($ping3 < 30)
  Goto(Detection)
endif

if ($ping4 < 30)
  Goto(Detection)
endif

if ($ping5 < 30)
  Goto(Detection)
endif

Sleep(1000)

Goto(Loop)

Now we have five objects that have the same characteristics (they all monitor sensors), but have different values (they have different names and one will take action when the others don't). We have now formed an array script. We just need to finish off be making the robot do something when an object is detected...

:Detection

Sleep(1000)

Say("Object detected.")

Left(70,2000)

sleep(2000)

Goto(Loop)

So, to put it all together, you will have a fully operational sensor and script array...

# Monitor the 5 ultrasonic sensors for detection
# mode. When a value is less than 30,
# it will execute the "Detected2 part of the script.

:Loop

Forward()

$ping1 = GetPing(d1, d2)
$ping2 = GetPing(d3, d4)
$ping3 = GetPing(d5, d6)
$ping4 = GetPing(d7, d8)
$ping5 = GetPing(d9, d10)

if ($ping1 < 30)
  Goto(Detection)
endif

if ($ping2 < 30)
  Goto(Detection)
endif

if ($ping3 < 30)
  Goto(Detection)
endif

if ($ping4 < 30)
  Goto(Detection)
endif

if ($ping5 < 30)
  Goto(Detection)
endif

Sleep(1000)

Goto(Loop)

:Detection

Sleep(1000)

Say("Object detected.")

Left(70,2000)

sleep(2000)

Goto(Loop)

In the next step, we will go over the basics of variable arrays.


ARC Pro

Upgrade to ARC Pro

ARC Pro is your gateway to a community of like-minded robot enthusiasts and professionals, all united by a passion for advanced robot programming.