# DUE BrainBot Intro

The BrainBot is equipped with several sensors and LEDs. We’ll be covering everything in later lessons. This intro lesson is meant to to get the system/software ready for the next lessons.

## Prerequisite

This assumes you’ve already assembled your BrainBot and are ready to set up the programming environment.

## BrainBot Driver

Software driver (library) is provided to start controlling BrainBot with minimal needed knowledge. Future robotic lessons cover details on controlling robots.

The driver provided below needs to be copied to to the bottom of your BrainBot projects.

``````
#####################################
### YOU CODE GOES ABOVE THIS LINE ###
#####################################
Exit
### Distance Sensor ###
# Return distance in d
@Dist
d = Distance(16,15)
Return

### Ground Sensor ###
# Returns ground line detection in g variable
# 1: Right, 2: Left, 3: Both
@Sense
g=0
Return

### Enable Sound ###
# f - The sound frequency
@Sound
Freq(f,0,500)
Return

### Enable Sound ###
# f - The sound frequency
@Sound
Freq(f,0,500)
Return

### Stop the Sound ###
@Quiet
Freq(0,0,500)
Return

### Make a Beep! ###
@Beep
Freq(2000,100,500)
Return

### Set Headlight ###
# c - headlight color
# uses a[]
@HLit
Dim a[4]
a[0]= 0x01
a[1]= (c>>16) & 0xff
a[2]= (c>>8 ) & 0xff
a[3]= (c>>0 ) & 0xff
I2cBytes(0x01, a, 5, b, 0)
Return

### Set Taillight ###
# x - right tailight color
# y - left tailight color
# uses a[]
@TLit
NeoSet(0, (x>>16) & 0xff, (x>>8) & 0xff, (x>>0) & 0xff)
NeoSet(1, (y>>16) & 0xff, (y>>8) & 0xff, (y>>0) & 0xff)
NeoShow(12,2)
Return

### Stop Motors ###
# uses l and r
@Stop
r=0:l=0:Move()
Return

### Set Motor Speed ###
# l - left motor speed
# r - right motor speed
# uses a[]
@Move
Dim a[5]
If (l > 100) || (r > 100):Return:End
a[0]= 0x02
If(l>0)
a[1]=l * 2.5:a[2]=0
Else
a[1]=0:a[2]=l * 2.5 * -1
End
If(r>0)
a[3]=l * 2.5:a[4]=0
Else
a[3]=0:a[4]=r * 2.5 * -1
End
I2cBytes(0x01, a, 5, b, 0)
Return
``````

Controlling the robot is now done easily, Just use use Move(). More on that in the next lessons.

``````L = 50 # Left motor forward speed
R = -50 # Right motor is rotating backward
Move()``````

The immediate window can also be used to control the robot. Immediate commands must be a single line but can have multiple instructions. The following code is exactly the same as earlier code, but on a single line.

``L=50:R=-50:Move()``

## BrainBot API

These are the functions available from the provided driver library.

## What’s Next?

Now that everything is set up, we can move to the BrainBot Dance lesson.

## BrainStorm

How safe are robots? What if we have made them smarter and they decided they no longer need us? Believe it or not, this is not a joke! We will soon be riding in cars with full self control. How would a car decide to stop in an emergency? Thankfully, Isaac Asimov has already thought of this and came up with the three laws of robotics:

• A robot may not injure a human being or, through inaction, allow a human being to come to harm;
• A robot must obey orders given it by human beings except where such orders would conflict with the First Law;
• A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws.