Difference between revisions of "CSC231 Lab 6"

From dftwiki3
Jump to: navigation, search
(Assembly Part)
(CSC 231 Lab # 6)
Line 12: Line 12:
 
==Getting the Messenger Library for the Arduino==
 
==Getting the Messenger Library for the Arduino==
  
You need a new library for the Arduino called '''[http://www.arduino.cc/playground/Code/Messenger Messenger]'''  
+
You need a new library for the Arduino called '''[http://www.arduino.cc/playground/Code/SimpleMessageSystem 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
 
* Boot the Ubuntu CD-Rom
Line 22: Line 22:
 
* download the library file from the Arduino Playground:
 
* download the library file from the Arduino Playground:
  
   wget http://www.arduino.cc/playground/uploads/Code/Messenger.zip
+
   wget http://www.arduino.cc/playground/uploads/Code/SimpleMessageSystem.zip
  
 
* unzip the file
 
* unzip the file
  
   unzip Messenger.zip
+
   unzip SimpleMessageSystem.zip
  
 
* Remove the archive file
 
* Remove the archive file
  
   rm Messenger.zip
+
   rm SimpleMessageSystem.zip
  
 
* Set the ownership of the new library the same ownership as that of the arduino software
 
* Set the ownership of the new library the same ownership as that of the arduino software
  
   chown -R 1000:1000 Messenger
+
   chown -R 1000:1000 SimpleMessageSystem
  
 
* Open the arduino GUI and create this new sketch
 
* Open the arduino GUI and create this new sketch
 
<code><pre>
 
<code><pre>
// This example sets all the values of the digital pins with a list through a callback function
+
/*
 +
---- 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 <Messenger.h>
+
// Include de SimpleMessageSystem library
// Instantiate Messenger object with the default separator (the space character)
+
// REMOVE THE FOLLOWING LINE IF USING WIRING
Messenger message = Messenger();
+
#include <SimpleMessageSystem.h>  
  
// Create the callback function
+
void setup() {
void messageReady() {
+
  // The following command initiates the serial port at 9600 baud. Please note this is VERY SLOW!!!!!!
    int pin = 0;
+
  // I suggest you use higher speeds in your own code. You can go up to 115200 with the USB version, that's 12x faster
      // Loop through all the available elements of the message
+
  Serial.begin(9600); //Baud set at 9600 for compatibility, CHANGE!
      while ( message.available() ) {
 
// Set the pin as determined by the message
 
        digitalWrite( pin, message.readInt() );
 
        pin=pin+1;
 
      }
 
 
}
 
}
  
 +
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 setup() {
+
void readpins(){ // Read pins (analog or digital)
   // Initiate Serial Communication
+
   switch (messageGetChar()) { // Gets the next word as a character
  Serial.begin(115200);  
+
    case 'd': // READ digital pins
  // Attach the callback function to the Messenger
+
      messageSendChar('d');  // Echo what is being read
   message.attach(messageReady);
+
      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;
  
void loop() {
+
  switch (messageGetChar()) { // Gets the next word as a character
   // The following line is the most effective way of using Serial and Messenger's callback
+
   case 'a' : // WRITE an analog pin
  while ( Serial.available() )  message.process(Serial.read () );
+
    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)
 +
  }
 
}
 
}
 +
  
 
</pre></code>
 
</pre></code>

Revision as of 16:28, 29 October 2008

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
  • 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