Pages: [1]   Go Down
Author Topic: Communicating PC to Arduino Mega 2560 through serial  (Read 535 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am having difficulty sending serial data from LabVIEW to my mega from my PC through USB.

A little bit of background to what I am trying to achieve: I would like to send a ASCII character (w, x, y, z) from the PC through serial to the mega, to move a servo.

I am able to send serial data through my terminal program (Putty) and interface with the mega. Everything works fine when I do this. When I try to do this in LabVIEW the RX Led blinks (So I know the mega is receiving data) but my servo does not move, Why?

Please may some one shed some light on this issue

Kind Regards

Kev

attached and below is my arduino sketchCode
--------------------------------------------------

#include <Servo.h>

Servo Pan;
Servo Tilt;

//int inByte = 0;
int panPos = 90;
int tiltPos = 90;
byte inByte = 0;
void setup(){
  //initialize Serial
 Serial.begin(115200);
delay(2000);
//Servo pins
Pan.attach(13);
Tilt.attach(12);

//Center Servos
Pan.write(panPos);
Tilt.write(tiltPos);
}

void loop(){
  Serial.flush();
 if (Serial.available() > 0){
 inByte = Serial.read();
 
 switch (inByte){
 
   case 'w':
   if (panPos >= 180){
     Pan.write(180);
     }
   else{
     panPos= panPos + 15;  //move servo Left
     Pan.write(panPos);
     }
     
 break;
 
 case 'x':
   if (panPos <= 0){
     Pan.write(0);
     }
   else{
     panPos= panPos - 15; //Move servo right
     Pan.write(panPos);
     }
     
 break;
 
 case 'y':
   if(tiltPos >= 180){
     Tilt.write(180);
     }
   else{
     tiltPos = tiltPos + 15;
     Tilt.write(tiltPos);
     }
     
 break;
 
 case 'z':
   if(tiltPos <= 0){
     Tilt.write(0);
     }
   else {
     tiltPos = tiltPos - 15;
     Tilt.write(tiltPos);
     }
     
 break;
 
 default:
 Tilt.write(tiltPos);
 Pan.write(panPos);
 
 }
 }
}



* Pan_and_Tilt.ino (1.05 KB - downloaded 5 times.)
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33271
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If this code works with putty or the serial monitor then your error is in the LabView bit which you have not posted.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unless you have a damned good reason for using Serial.flush(), stop it. If you do, you'd be the first Arduino user EVER that did, and I'd like to hear it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,


Thanks for such swift reply.

Serial.flush() I put in just to try things out hoping it would help (Saw it on a website)

Are you familiar with NI (National Instruments) products?

I have used VISA test panel, in Measurement and Automation explorer (MAX) to send a character, that is in my switch case w, x, y, z and everything works fine.

However when I do the same in LabVIEW it does not, (Ps. I am not a novice to LabVIEW)

Please see attached Block Diagram and Front Panel


Kind Regards

Kev



* Serial BD.png (13.29 KB, 521x516 - viewed 33 times.)

* Serial FP.png (15.2 KB, 333x509 - viewed 23 times.)
Logged

Pages: [1]   Go Up
Jump to: