Lab 4Pre: Arduino embedded coding
(continued from successful LabVIEW DIO VI)

Here you will simulate control of a robot rover using an Atmel 328 microprocessor communicating with LabVIEW controls and indicators; the code is created in the Arduino environment. Read about Arduino at www.arduino.cc. Arduino software will "compile" a version of C as a *.ino file, not a *.c file. We are using Arduino 1.0.6.
Details about Arduino: in "Setup" code Arduino declares its variables. In "loop" Arduino runs an infinite while loop...inside of which Lab4Pre default code goes...to be followed later by your modifications.
Arduino does not have a single-step debug feature. You can debug your code with the "serial monitor" that will print out in a "serial window" values of variables you call for. In what you have as default code is a for loop that runs 200 times and has a 250msec delay in each loop, and a printout of variables at the end of the loop.

We have 5 dedicated DE-11-Arduino-LabVIEW setups labelled with computer names and intended to be connected to those computers only. The dedicated Arduino cards can then be instantly recognized by their respective computers, and come up with definite COM ports (instead of a computer "installing new hardware" after seeing an unfamiliar card. The cards should work perfectly well without you having to adjust any wires.

You'll see a LabVIEW green connector CB-68LP with wires running to a STD socket plugged onto a DE-11 card on which a white breadboard has a blue Arduino card mounted, and a 7406 TTL hex inverter chip. The Arduino card hosts one end of a USB A-B cable going into a USB port of the computer. The LabVIEW connector will have a 68-pin cable running to the 6024E card, as usual...

These dedicated setups have each been tested with current default code...

The dedicated computers are
SINK (095)
PHONE (095)
Corner (097)
Storm King(097)
BRONCOS (097)


The Arduino card shown does not have a screwshield (current versions do) which prevents wire breakage in the socket holes...

You may want to make sure you can trace the following connections from the LabVIEW 68LP green connector screw card to Arduino pins on its screw shield: (D stands for digital...)
D0 screw 52 to Arduino D pin 8 OUTPUT from Arduino: LEFT motor direc control
D1 screw 17 to Arduino D pin 9 OUTPUT from Arduino: RIGHT motor direc control
D2 screw 49 to Arduino D pin 6 OUTPUT from Arduino: LEFT motor inhibit (HI)
D3 screw 47 to Arduino D pin 7 OUTPUT from Arduino: RIGHT motor inhibit (HI)
D4 screw 19 to Arduino D pin 11 INPUT to Arduino: LEFT bumper switch (active LO)
D5 screw 51 to Arduino D pin 12 INPUT to Arduino: RIGHT bumper switch (active LO)
D6 screw 16 to Arduino D pin 10 INPUT to Arduino: V-Switch for START of "pp" cycle
D7 screw 48 to Arduino D pin 15 INPUT to Arduino: Backup switch
Don't forget to ground properly the Arduino board to pin 53 (GND) of the LabVIEW connector card.

Example: If the left bumper switch (D4) is flipped up then the right motor LED (D1) will go ON to represent the right wheel reversing direction of rotation...

In the current EN123 IP folder you will find folder Lab4Pre14 inside of which is Arduino "sketch" Lab4Pre14.ino, running on Arduino 1.0.6.
Start running the Arduino software rev 1.0.6 (desktop icon of infinity sign in teal).
Under File/Open bring up Lab4Pre14.ino (read-only) and immediately rename it your_4pre to be in your IP folder; remove the read-only restriction.
Under Tools select the Arduino card on your board: Duemilanove or Uno; next select the USB port number (you may have to check with Device Manager...)
Make sure the default code compiles: click on the left side arrow "verify" and wait for mssg "Done Compiling" with no red text... below are the first lines of the Arduino default code.

Comments in the code above tell you which Arduino pins are associated with what rover signals.
Misprint: DIR_LFT and DIR_RGHT are "forward" when LOW... after passing through 7406 inverter...
The next section of default code shows void setup( )

showing the way Arduino assigns input and output status to variables declared above.
On the Arduino DE-11 card is a hex inverter 7406 chip to be a buffer between Arduino output signals and the stepping motor drivers inputs... the 7406 presence means that DIREC and ON-OFF signals from Arduino are inverted. We normally work with drivers that are ON when ON-OFF input is LOW and wheels going FORWARD when DIREC inputs are LOW. Thus in the code a HIGH DIREC output appears as a LOW signal in your VI and at the actual driver in lab 4R
Schematic of Arduino-7406 card
The robot rovers which LabVIEW simulates the inputs and outputs of have the following conventions:
* V-switch is ON when HI and OFF when LO
* bumper switch is LO when it contacts a wall, and HI otherwise.
* When an INHIBIT on the blue STD connector pin 4 or 5 is LO the associated motor is ON (UNINHIBITED).
* DIRECtion Left and Right are to be LOW when the rover goes forward.
The conventions above mean that the when going forward all front panel LEDs will be OFF; when stopped the two right LEDs will be ON. When going backwards the two left LEDs will be ON.
Next screen shot below: loop( ) is an infinite while loop in Arduino, and inside the infinite loop() is a limited for-loop on pp=0 to pp_max:



Notes: loop begins with Serial.println debugging lines. When D6 is switched up the pp for-loop starts.
If D7 is flipped up the default code tests backing up and turning off. All LEDs are briefly lit. See use of While Loop.
After pp>12 the code tests for front panel D4(left) or D5(right) on, and if so transiently reverses the direction of the opposite wheel, lighting D0 or D1 front panel LEDs.

During the code running through the loop it shows the serial.print of pp and (anticipating Lab 4Lam) the phototransistor inputs.
After 200 pp increments the code "turns off" and starts looking for D6 up again...

In other words... The code will run an infinite loop that is continually waiting for virtual switch D6 to be flipped up to start the pp loop. In that WAIT FOR START state the 4 indicator lights on the left of the front panel are LOW.

The left-most lights, L0 and L1, represent the directions of the left and right motors respectively, with both OFF meaning forward, (if the L2 and L3 lights are also OFF, they represent that the motors themselves are ON).

If after 3 seconds ON the "left" switch (D4) is flicked ON and OFF you will see the second green light go on for a sec; if the "right" switch (D5) is flicked the first green light will go on for a second.

D6 can be flicked on and off to start the pp loop going...

Flipping up D7 any time will "test" backing up and stopping...
To repeat: D6 represents the V-switch on the back of the rover, and flipping it to its center position is the start signal. Let D4, D5 represent proximity switches for the front left and right sides of rover; Let L0 and L1 represent the directions of the wheels left and right. For now, let L2 and L3 ON represent signals to uninhibit the left and right motors respectively. (Inhibit means the motor stops and "relaxes": offers no resistance to turning).

With everything working in the default mode, you're ready your Lab4Pre challenge:

Modify your re-named version of the default code so that when a left or right wall is detected (after rover being on for 3 seconds) the rover
1. Stops for one second
2. Backs up for one second
3. Stops for another second
4. Turns to the center (CW for left, CCW for right wall) for one second
5. Stops again for one second
6. Goes Forward until it hits another wall or time is up or (see below) D7 in switched ON.
thus we will see 3 stops during the turn maneuver

Also, arrange that switch D7 turned ON causes the rover to
1. Stop for 2 seconds (motors inhibited)
2. Go CONTINUOUSLY backwards until the D7 is turned OFF. If D7 is OFF then the rover will go forward (unless it hits a wall).
D7 has been declared as variable BU_SWTCH near the top of the code. Use a "while" and an "if" test on D7, ..to achieve the backup function specs...pin 3 is the Arduino side...on the LabVIEW side it's D7...

Possible FTQ:
Explain what changes (if anything) after one line of your code is commented out.

Free Advice: re-compile ("verify") frequently so you don't have too many keystrokes to consider if an error pops up in compling...

See Quickie C file for understanding of some default code. Consider describing your problem as a series of STATE changes: Forward to Inhibit to Reverse to Inhibit to Turn to Inhibit to Forward. Study the default code and its comments for what and how the C instructions work. Attend to assignment statement format, and if statement structure.

The STD blue socket pinout:

Arduino Uno top view:

Note the Analog-in pins at the bottom right... for Lab 4Lam