Saturday, January 26, 2013

XBEEEEEEEE


  • So we started playing the with XBee!  Things we learned:
  • We can transmit stuffs!
  • Made an LED flash on the receiving end
  • We can replace the act of plugging and unplugging the XBee shield by toggling the shield switch from UART to DLINE.  In UART is transmission mode, DLINE is code upload mode.
  • We learned how to write to the serial monitor and transmit directly to STAMP plot
  • Receiving XBee:

//const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into
// int cr = 13;
void setup() {
 // initialize serial communication:
 Serial.begin(9600);
 // initialize the LED pin as an output:
 //pinMode(ledPin, OUTPUT);
}

void loop() {
 // see if there's incoming serial data:
 if (Serial.available() > 0) {
   // read the oldest byte in the serial buffer:
   incomingByte = Serial.read();
   // if it's a capital H (ASCII 72), turn on the LED:
   Serial.print(incomingByte);
   //Serial.write(cr);
    Serial.print("\n");
   //if (incomingByte == 'H') {
     //Serial.print('H');
     //digitalWrite(ledPin, HIGH);
   //} 
   // if it's an L (ASCII 76) turn off the LED:
   //if (incomingByte == 'L') {
     //Serial.print('L');
     //digitalWrite(ledPin, LOW);
   //}
 }
}

  • Transmitting XBee:

//RAAAMP Code
int i = 0;

void setup() {
 Serial.begin(9600);
}

void loop() {
 if (i < 500) {
   i+=10; 
 } 
 else {
   i = 0;
 }

 Serial.print(i);

 delay(100);
}


  • Flawed because Serial.print() writes individual ASCII characters, e.g. 100 -> "1" "0" "0"
  • Possible fix? Serial.write (binary) transmitting, Serial.print receiving --> printing "as is"
  • Other problem: Serial.write() can only send one byte at a time (0-255), so 260 -> 4
  • One solution: still send ASCII characters using Serial.print() and receive as 8-bit integers, then convert into the corresponding inputNumber:

int inputChar;
int inputNumber=0;

void setup()
{
 Serial.begin(9600);
}
void loop()
{
 if (Serial.available()>0){
   delay(5);
   while(Serial.available()>0){
     inputChar=Serial.read();
     inputNumber=inputNumber*10+(inputChar - '0');
   }
  
       Serial.println(inputNumber);
   inputNumber=0;
   inputChar=0;
 }
}

  • This method is more data-intensive than necessary. For example, if we want to send the number 1020, we would have to send four 8-bit integers representing each character, for a total of 32 bits. However since 1020 can be represented by 10-bits (0-1023), we really only need to send 2 8-bit packets of data. We can achieve this by using the following send code:

  upper = i >> 8;
  lower = i & 255;
  
  Serial.write(upper);
  Serial.write(lower);

  • Receiving end loop:

// make sure both upper and lower bits have been received  
if (Serial.available() >= 2) {
    // read the oldest byte in the serial buffer:
    upper = Serial.read();
    lower = Serial.read();
    
    incomingByte = (upper << 8) | lower;
    Serial.println(incomingByte);
}

Wednesday, January 23, 2013

Jan 22 2013 Progress + How to fix Arduino port problem without restarting your computer!

The progress report looks good. Rachel and I edited it yesterday. We also managed to replicate the result we had on Monday several times, so we're now sure the blinking light is due to transmission and not some coincidence. However it didn't work every single time, and we're having trouble having the receiving Arduino output data to the serial monitor at the same time. There has to be some better way than installing the Xbee shield every time, after uploading our code.

Also the way to fix 'COM# already in use' without restarting is to close the Arduino program and plug the Arduino into a different USB port. It seems that USB ports are reusable, just as long as you change ports each time this occurs.

Saturday, January 19, 2013

Using the A to D converter!

We had to simultaneously figure out how to use the SPI interface to talk to the A to D converter, and how to connect the Arduino and signal generator to the A to D converter.

Key for SPI Interface:
VCC and VDD stand for positive voltage
VSS stands for ground

First, here is the code we came up with:

// inslude the SPI library:
#include <SPI.h>

// set pin 10 as the slave select for the digital pot:
const int CS = 10;
const int out = 11;
const int in = 12;
const int clock = 13;
int fst = 0;
int snd = 0;

void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(38400); 
 
 // set the slaveSelectPin as an output:
 pinMode (CS, OUTPUT);
 pinMode (out, OUTPUT);
 pinMode (in, INPUT);
 pinMode (clock, OUTPUT);
 
 // initialize SPI:
 SPI.begin();
 SPI.setBitOrder(MSBFIRST);
 SPI.setClockDivider(SPI_CLOCK_DIV32);
 digitalWrite(CS, HIGH);
}

void loop() {
 digitalWrite(CS, LOW);
 fst = SPI.transfer(120);
 snd = SPI.transfer(0);
 Serial.println(((fst & 7) << 8) | snd);
 digitalWrite(CS, HIGH);
 
 delayMicroseconds(100);
}

We spent time trying to figure out how to connect the A to D converter to the Arduino. Here's the circuit (to match the code) we came up with.



Replacing the Serial Monitor

The Arduino development environment is nice in some ways, but the serial monitor sucks. Seriously. It opens by default to 9600 baud with autoscroll, and if the serial port is communicating at any other data transmission rate, the serial monitor spews out nonsense and slows down your computer to the point that it is hard to even move the mouse to the dropdown menu to change the rate to the actual rate being used (e.g. 115,200 baud). Furthermore, it just displays text.

Ideally, we want to be able to graph our data in real time. Can we do this? Yes, there are several options available for alternative displays. One option is an Arduino graphing library known a MegunoLink developed by a third party called Blue Leaf Software (1).

Another option is demonstrated on the Arduino website in a graphing tutorial (2). As shown in this example, Processing  can be used to access data from the serial port and graph it on a computer. Processing is a popular programming language and development environment (3). In fact, the Arduino development environment was based on Processing, according to the Arduino home page (4). It turns out that the Arduino project also has a Processing library that uses Firmata to communicate with the Arduino using Processing code directly (5). Firmata is a generic protocol for communicating with microcontrollers from a computer (6).

There is at least one entire discussion thread on the Arduino forums dedicated solely to replacing the serial monitor (7). One post there mentions the use of Firmata and Silverlight together, although the exact purpose of using this combination of these softwares is not clear.

We are going to explore these various alternatives until we find one that works.

1. http://www.blueleafsoftware.com/Resources/EmbeddedSand/Plotting_Arduino_Data
2. http://arduino.cc/en/Tutorial/Graph
3. http://processing.org/
4. http://www.arduino.cc/
5. http://playground.arduino.cc/interfacing/processing
6. http://www.firmata.org/wiki/Main_Page
7. http://arduino.cc/forum/index.php?topic=122167.0

Saturday, January 12, 2013

Proof of Concept: multiple signal input processing


First, we did single signal input processing. You know, to get the hang of it.
  • connect electronic test cables from computer's signal out put to Arduino (see image 1)
    • two cables to Arduino, one in A0 and one in ground
  • set up signal on easy scope 

  • connect Arduino to computer and set up sampling rate
      •  remember to install the drivers!
    • Put in the following code
  • Start the signal generator on easy scope
      • the signal looked weird. It only looked weird when we plugged it into the Arduino. The solution was to plug the ground into the breadboard. EDIT don't do that.
    • 300, 14400, 28800 didn't work.EDIT  these still don't work after later edit/solution.  The rest worked until anything past 19200 froze the computer:( 
      • we think this is because the serial monitor couldn't handle those speeds, so we researched alternatives to the serial monitor (standard with the Arduino program.)
      • We discovered this in addition to an Arduino graphing library. Why aren't we using this (the graphing library) already? I guess we'll find out! 

        • The graphing library would allow us to either graph from serial (not helpful) or 
        • EDIT the solution is to print on new lines, the serial monitor doesn't handle horizontal scrolling well 
  • The highest sampling speed (115200) works! We copied a bunch of the data and graphed it in MatLab but it looked like a wandering sine wave (sad).
    • Sad wandering sine wave




      • Upon re evaluating what we did before we realized they hall had to be in the same ground.

      • Additionally, we noticed that our signals would get cut off at 700 , so, we changed our peak to peak voltage output from 5 to 2 and it all fit so we had a full signal! This would be disturbing (in case we needed a larger peak to peak voltage output) but since it worked from a lower ppv, we attributed it to the arduino's Ato D converter, and since we will be using a different one, we let it go...for now...
      • Then we changed the sampling delay from 2ms to .2ms to give us a cleaner signal
      • Finally, we raised the frequency to 54 Hz (which is what we needed to be clinically applicable) and it worked! check it out :D