CSC231 Lab 6

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

Back to CSC231 Weekly Schedule


CSC 231 Lab # 6

ArduinoStamp.jpg

© 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 Messenger Library for the Arduino

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
/* 
 ---- 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)
  }
}


  • Compile and download it to your Arduino
  • You may get warnings, but make sure you get a compile confirmation:
 Binary sketch size: 3402 bytes (of a 14336 byte maximum)
  • I M P O R T A N T
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.

Assembly Part

  • 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