View Single Post
  #7   Spotlight this post!  
Unread 12-12-2012, 20:47
apples000's Avatar
apples000 apples000 is offline
Registered User
no team
 
Join Date: Mar 2012
Rookie Year: 2012
Location: United States
Posts: 222
apples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant futureapples000 has a brilliant future
Re: Sending ASCII characters through cRio serial port

For the arduino code, we switched to using a demo project that worked a little better than what we wrote.
Code:
/*
  Reading a serial ASCII-encoded string.
 
 This sketch demonstrates the Serial parseInt() function.
 It looks for an ASCII string of comma-separated values.
 It parses them into ints, and uses those to fade an RGB LED.
 
 Circuit: Common-anode RGB LED wired like so:
 * Red cathode: digital pin 3
 * Green cathode: digital pin 5
 * blue cathode: digital pin 6
 * anode: +5V
 
 created 13 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
 */

// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(redPin, OUTPUT); 
  pinMode(greenPin, OUTPUT); 
  pinMode(bluePin, OUTPUT); 

}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt(); 
    // do it again:
    int green = Serial.parseInt(); 
    // do it again:
    int blue = Serial.parseInt(); 

    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {
      // constrain the values to 0 - 255 and invert
      // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
      red = 255 - constrain(red, 0, 255);
      green = 255 - constrain(green, 0, 255);
      blue = 255 - constrain(blue, 0, 255);

      // fade the red, green, and blue legs of the LED: 
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);

      // print the three numbers in one string as hexadecimal:
      Serial.print(red, HEX);
      Serial.print(green, HEX);
      Serial.println(blue, HEX);
    }
  }
}
As for the labview, I can't get to that right now, it was just a while loop with a 100ms wait in it. We opened a serial port with all the default settings outside the loop, then in the loop we used the serial write vi. For the buffer input, we used format into string vi. You can drag the vi so it gets bigger and you can have three inputs. These are your red/green/blue values. Leave the initial string blank and for the format string use

%g,%g,%g\n

Make sure that you put the commas and that the slash goes the right way. Then you can connect the resulting string into the buffer input of the serial write. I would recommend starting by wiring constants of 127 into the inputs of the format into string so that it doesn't matter if 0 or 255 is on or off. Also, we had some issues with having both the arduino's usb connection and the serial connection running at the same time, so if it doesn't work you may also have to disconnect the arduino from the computer. Also, I don't know how much you've already done, but it helps to make sure that your arduino board can actually control the lights. We used TIP-120 transistors with ours and it worked well.
I'll be at the computer with the LV code on friday, so I can post that.

Last edited by apples000 : 12-12-2012 at 20:49.
Reply With Quote