01-13-2017 02:13 PM - edited 01-13-2017 02:18 PM
Hello, I am a beginner, and I am controlling four leds from four pushbuttons on hardware side with arduino and on labview in UI I made four leds, is there anyway when I turn ON a specific LED on hardware side it will also turn ON in LABVIEW and I am also serial printing 1,2,3 or 4 when turning ON any specifc LED
below is the code of arduino and in attachment labview form is present:
byte led1 = 10;
byte led2 = 11;
byte led3 = 12;
byte led4 = 13;
byte but1 = 2;
int val1 = 0;
byte but2 = 3;
int val2 = 0;
byte but3 = 4;
int val3 = 0;
byte but4 = 5;
int val4 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(but1,INPUT);
pinMode(but2,INPUT);
pinMode(but3,INPUT);
pinMode(but4,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
val1 = digitalRead(but1);
val2 = digitalRead(but2);
val3 = digitalRead(but3);
val4 = digitalRead(but4);
if(val1 == LOW)
{
digitalWrite(led1,HIGH);
Serial.print(1);
}
else
digitalWrite(led1,LOW);
if(val2 == LOW)
{
digitalWrite(led2,HIGH);
Serial.print(2);
}
else
digitalWrite(led2,LOW);
if(val3 == LOW)
{
digitalWrite(led3,HIGH);
Serial.print(3);
}
else
digitalWrite(led3,LOW);
if(val4 == LOW)
{
digitalWrite(led4,HIGH);
Serial.print(4);
}
else
digitalWrite(led4,LOW);
}
01-13-2017 05:38 PM
Yes, it is possible. Look at Labview serial port examples.
Read message, process, update leds value. What part is a problem? Post a code.
I would modify arduino code a little bit.
1) Do not spam serial port. Send every 100 ms if it is for user only. 10 ms if you need to respond (start generating or something).
2) send status of all leds in one message. Much simpler to process, always fixed number of bytes (1) to read, error handling. Now read timeout means all leds are off or com port disconnected?
3) are arrays so difficult to work with in arduino?
01-13-2017 08:35 PM
@Alexander_Sobolev wrote:
2) send status of all leds in one message. Much simpler to process, always fixed number of bytes (1) to read, error handling.
Fully agree here. What I would do in the Arduino is set the output value to 0 and then for each button value that is LOW add a value to it (val1 = 1, val2 = 2, val3 = 4, val4 = 8). Then after all of that, you send the value. So if no buttons are active, then you send a 0. If all of the buttons are active, you will send a 0x0F. So now you send a byte each iteration of your loop in the Arduino.
Now on the LabVIEW side, you configure the serial port to match the baud rate and make sure the Termination Character is turned OFF (boolean input on top of VISA Configure Serial Port). Then you just use a loop to constantly read a byte, convert to a U8 and then to an array of booleans (there are primitives for doing each of those steps). I would have the display be an array of LEDs to make things simple. Then when you hit the stop button, close the port.
01-14-2017 11:25 AM
I understood point3 but I didnt understand how to implemet point 1 and 2 can you please elobarate?
01-16-2017 05:52 PM - edited 01-16-2017 05:53 PM
Hello shoaibliger95,
It is my understanding that for point 1 Alexander_Sobo meant to add a wait function in your code. Second of all, to group the commands in one message I would suggest referencing this Arduino forum post.
I hope this helps!
01-17-2017 09:27 AM - edited 01-17-2017 09:56 AM
Here is an example of sending multipule button presses as one byte like crossrulz was saying.
I used a PS2 controller in this so it has a D-pad and four buttons.
Note: I was using a bluetooth serial interface so I only send data if a button is pressed.
void setup() { for (int pin = 2; pin < 10; pin++) { // configure pins 2 through 9 as input with pull-up pinMode(pin, INPUT_PULLUP); } pinMode(13, OUTPUT); // configure pin 13 as Tx indicator Serial.begin(9600); // start serial connection } void loop() { int forward = digitalRead(2); // read each pushbutton into a variable int back = digitalRead(3); int left = digitalRead(4); int right = digitalRead(5); int triangle = digitalRead(6); int circle = digitalRead(7); int x_button = digitalRead(8); int square = digitalRead(9); byte buttontot = 0; // Convert all buttons pressed to a single byte value (buttontot) if (forward == LOW) { // Each button is given a binary weight if pressed buttontot = buttontot + 1; } if (back == LOW) { buttontot = buttontot + 2; } if (left == LOW) { buttontot = buttontot + 4; } if (right == LOW) { buttontot = buttontot + 8; } if (triangle == LOW) { buttontot = buttontot + 16; } if (circle == LOW) { buttontot = buttontot + 32; } if (x_button == LOW) { buttontot = buttontot + 64; } if (square == LOW) { buttontot = buttontot + 128; } if ( buttontot > 0) { // If buttontot > 0 xmit buttontot char digitalWrite(13, HIGH); Serial.print(char(buttontot)); } else { digitalWrite(13, LOW); } // Turn off Tx LED delay(50); }