This is a quick (REALLY quick) writeup on establishing a connection between a custom designed Arduino Board using Atmega8 and LabVIEW. I used a very simple setup where Arduino would receive data serially from LabVIEW. To demonstrate this I have used an example of RGB-colour gradient. This employs a simple 2-way communication, the Arduino would listen for a comma separated values of red, green and blue at the beginning of each iteration. If a command is heard, it would react by first parsing these values and then changing the color of the RGB LED, depending on the combination of red, green and blue color received. If nothing was heard, then it would continue on through the read cycle. The method is inefficient and barbaric, but my intent was proof-in-concept. The concept may be improved upon to increase reliability and efficiency.
Step 1: Quick Arduino Sketch
The first thing we need to do is write a sketch for the Arduino, to prepare it for taking values from the serial port and parse it to ensure correct intensities are set for red, green and blue pins of the RGB led..
This is accomplished with a simple sketch that accepts comma separated values of red, green and blue and constantly transmits a message over the Serial comm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* rgb values are sent to the arduino board and as a comma seperated list of decimal codes of 0-255 | |
* for example rgb could look like this 213,89,46. | |
* This program accepts these codes form serial comm port and seperates rgb values using (,) comma as the seperator | |
* and sends them to the leds | |
*/ | |
// global variable declarations | |
int incomingByte = 0; // to hold incoming serial data | |
// pin number to connect red green and blue leds | |
int red = 9; | |
int green = 10; | |
int blue = 11; | |
int brightness = 0; //default brightness level of leds | |
int comma; //to keep a track of which led to process | |
int pin[] = {red , green , blue}; //the list of pins used for red, green and blue | |
int i = 0; | |
int x,l,j; | |
int temp[3]; | |
void setup() { | |
Serial.begin(9600); // opens serial port, sets data rate to 9600 bits per second | |
pinMode(red,OUTPUT); | |
pinMode(blue,OUTPUT); | |
pinMode(green,OUTPUT); | |
} | |
void loop() | |
{ | |
//wait for transmission of output serial data to complete | |
Serial.flush(); | |
// send data only when you receive data: | |
if (Serial.available() > 0) | |
{ | |
//confirm that data received | |
Serial.println("processing"); | |
delay(100); | |
comma = 0; | |
do | |
{ | |
// read the incoming byte: | |
incomingByte = Serial.read(); | |
//the byte received would be processed as ascii code and so as to process it as integer subtract ascii code of 0 (48) from it | |
incomingByte -= '0'; //get the number as integer | |
//As all characters have been subtracted by the ascii code of 0 | |
// ,(comma) ie 44 becomes -4 | |
//loop to read the comma seperated incoming byte and add it to a temporary arrray | |
while(incomingByte != -4) //while incomingByte != comma | |
{ | |
if((incomingByte>=0)&&(incomingByte<=9))\ | |
{ | |
temp[i] = incomingByte; | |
i++; | |
} //end if | |
incomingByte = Serial.read(); | |
incomingByte -= '0'; | |
}//end while | |
j = i; | |
i = 0; | |
brightness = 0; | |
//only a number can be sent to a pin and not an array so temp array must be changed to one number | |
for( x = 0,l = j; x<j; x++,l--) //convert the array to a single number | |
{ | |
brightness = brightness + temp[x]*pow(10,l-1); | |
}//end for | |
brightness++; | |
analogWrite(pin[comma],brightness); | |
//use this to debug code | |
// Serial.print("brightness "); | |
// Serial.print(brightness); | |
// Serial.print(" pin "); | |
// Serial.println(pin[comma]); | |
comma++; | |
}while (comma < 3); | |
}//end if | |
}//end loop | |
Step 2: Let's do the Labview VI
The trick in this step is more about the right serial data than anything else. Below is a snapshot of the VI I threw together to connect to the Arduino.
The Labview Vi creates a comma separated values of the colors and sends it to Arduino, which simply parses that string and does it's job. With that all wired together, save it as a VI named Arduino or something along those lines. Hypothetically you could set the baud rate as a constant 9600, but I left it changeable in case I feel crazy one day and want to spice up that rate a bit.
As a side note, the VI is a handy thing to be able to build, and when you establish a successful connection with the Arduino with reliable data flow and communication, I recommend wrapping the whole thing up as a VI so all you need to do is set the Baud Rate and Resource Name on input nodes and the output node will flow the raw data for you to do with whatever you want.
Step 3: Now Look at the front-end to see if everything is perfect
Step 4: Plug in the Arduino and turn it on
Plug in the Arduino and turn it on. Go to the Front Panel and select the appropriate VI resource name (which is hidden under the settings panel on my front panel) and flip the “On” switch. If all goes well, on modifying the R, G and B values in the front-end above, the RGB Led would change color to the color shown in the front end.
From here it is just a matter of using your new found connectivity to do something revolutionary. Feel free to leave me any questions you might have.
You can get the code here.
You can get the code here.