CSC231 Lab 6

From dftwiki3
Revision as of 08:14, 31 October 2008 by Thiebaut (talk | contribs) (Getting the SimpleMessageSystem Library for the Arduino)
Jump to: navigation, search

Back to CSC231 Weekly Schedule


CSC 231 Lab # 6

© D. Thiebaut, 2008

In this lab we are going to make the Arduino keep on listening to commands it gets from the assembly language on the PC, and execute each command as it receives them.

Getting the SimpleMessageSystem Library for the Arduino

ArduinoCGI.jpg

You need a new library for the Arduino called SimpleMessageSystem (Note: there is a newer version of it, Messenger, but I haven't been able to play with it enough to make it the version to use for the project.)

  • Boot the Ubuntu CD-Rom
  • Open a Terminal window and type
  sudo su
  cd 
  cd arduino-0010/hardware/libraries
  • download the library file from the Arduino Playground:
  wget http://www.arduino.cc/playground/uploads/Code/SimpleMessageSystem.zip  
  • unzip the file
  unzip SimpleMessageSystem.zip  
  • Remove the archive file
  rm SimpleMessageSystem.zip  
  • Set the ownership of the new library the same ownership as that of the arduino software
  chown -R 1000:1000 SimpleMessageSystem
  • Open the arduino GUI and create this new sketch
  cd ../..
  .arduino
/* 
 ---- SimpleMessageSystem Example 1 ----
 Control Arduino board functions with the following messages:
 
 r a -> read analog pins
 r d -> read digital pins
 w d [pin] [value] -> write digital pin
 w a [pin] [value] -> write analog pin 
 
 Base: Thomas Ouellet Fredericks 
 Additions: Alexandre Quessy  
 */

// Include de SimpleMessageSystem library
// REMOVE THE FOLLOWING LINE IF USING WIRING
#include <SimpleMessageSystem.h> 

void setup() {
  // The following command initiates the serial port at 9600 baud. Please note this is VERY SLOW!!!!!! 
  // I suggest you use higher speeds in your own code. You can go up to 115200 with the USB version, that's 12x faster
  Serial.begin(9600); //Baud set at 9600 for compatibility, CHANGE!
}

void loop() {
  if (messageBuild() > 0) { // Checks to see if the message is complete and erases any previous messages
    switch (messageGetChar()) { // Gets the first word as a character
    case 'r': // Read pins (analog or digital)
      readpins(); // Call the readpins function
      break; // Break from the switch
    case 'w': // Write pin
      writepin(); // Call the writepin function
    }
  }
}

void readpins(){ // Read pins (analog or digital)
  switch (messageGetChar()) { // Gets the next word as a character
    case 'd': // READ digital pins
      messageSendChar('d');  // Echo what is being read
      for (char i=2;i<14;i++) {
         messageSendInt(digitalRead(i)); // Read pins 2 to 13
      }
      messageEnd(); // Terminate the message being sent
      break; // Break from the switch
  case 'a': // READ analog pins
      messageSendChar('a');  // Echo what is being read
      for (char i=0;i<6;i++) {
        messageSendInt(analogRead(i)); // Read pins 0 to 5
      }
      messageEnd(); // Terminate the message being sent
   }
}

void writepin() { // Write pin
  int pin;
  int state;

  switch (messageGetChar()) { // Gets the next word as a character
  case 'a' : // WRITE an analog pin
    pin = messageGetInt(); // Gets the next word as an integer
    state = messageGetInt(); // Gets the next word as an integer
    pinMode(pin, OUTPUT); //Sets the state of the pin to an output
    analogWrite(pin, state); //Sets the PWM of the pin 
    break;  // Break from the switch
    
  case 'd' : // WRITE a digital pin
    pin = messageGetInt();  // Gets the next word as an integer
    state = messageGetInt();  // Gets the next word as an integer
    pinMode(pin,OUTPUT);  //Sets the state of the pin to an output
    digitalWrite(pin,state);  //Sets the state of the pin HIGH (1) or LOW (0)
  }
}


  • Save your sketch in the /root/sketches folder, and give it the name loop. I found that saving is not always straightforward, and it works best for me if I use the right arrow in the tab area of the Arduino GUI, click it, and use the rename option.
  • IMPORTANT
Note: When you boot your PC in Ubuntu mode from the live CD, you are working with a Linux computer that exists only on the CD and in the RAM. Whatever you create or copy to your ubuntu or root account exists only in RAM. As soon as you close the system, all your changes disappear. All the files you created disappear. For this reason, you will need a USB key to save all the files you create, including sketches and libraries, such as the Messenger library you created above.
Thus, at this point your /root directory should contain two folders: arduino-0010 and scketchbook, and sketchbook should have a new sketch called loop. You may want to drag the whole root folder to your USB memory stick and save all these new modifications.

File:CSC231 Ubuntu Root Folder.jpg

  • Use the Tools menu to set the board as the Diecimila, and the port as the USB port.
  • Compile. You may get warnings, but make sure you get a compile confirmation:
 Binary sketch size: 3402 bytes (of a 14336 byte maximum)
  • Upload to the Arduino. Check that the Receive and Transmit LED on the board light up to indicate data transfer.


UbuntuPinguin.png





Assembly Part on the PC

OUTPUTTING INFORMATION

LedOn

You are going to create a program called ledOn that turns on the LED connected to Pin 13.

  • Open a Terminal window under Ubuntu and type
  sudo su
  cd
  • Highlight the contents of the file driver.c and copy it to the clipboard
  • In the terminal window type
  cat > driver.c
and then click the middle button of your mouse to paste the code you had highlighted
  • Take a look at LedOn.asm. We'll look over the main features in class. It is important to note where the message containing the command is, and what its format is.
  • Assemble/compile the files:
 nasm -f elf -F stabs asm_io.asm
 nasm -f elf -F stabs ledOn.asm
 gcc -o ledOn driver.c ledOn.o asm_io.o
  • Run the program
  ./ledOn
Does the LED on Pin 13 turn ON? (If your Pin 13 is damaged from previous labs, please put a resistor plus LED connected to Pin 11).

LedOff

Ok, you figured it out. Make a copy of ledOn.asm under the name ledOff.asm, modify it, and compile it. Use it to turn OFF the LED.

LedOnOff

Same scenario, but now this time make your assembly program send an ON message, followed by an OFF message in an endless loop.

Measure the frequency of the LED turning ON and OFF. This will give us an idea of how fast we can activate devices attached to the Arduino.

For those interested creating sound, the human ear can hear sounds between about 300 and 20,000 Hz.

INPUTTING INFORMATION

This part is for the whole class to devise as a group, and implement in pairs.

  • Figure out a way to connect a switch to the Arduino. Please be very careful how you wire it up so as to not damage the Arduino or its input/output pins. Remember that input pins should be programmed as such
  • Then write an assembly language program that continuously reads the status of the switch and displays it on the screen as a long series of 0s or 1s.

Open Problems

Things your instructor doesn't know, but that you might be able to discover while playing with the Arduino, and in which case you are asked to share your discoveries!

  • How can the serial speed of communication be increased so that the PC and the Arduino talk faster? I have tried changing the speed in the sketch from 9600 to a higher frequency, such as 115200, but that doesn't seem to work. The Java machine supporting the arduino crashes...