Go Down

Topic: Serial communication querry (Read 2599 times) previous topic - next topic

dattasaurabh82

Is it possible to send data from Arduino and at the same time send data back to it through serial communication at the same time?
I want to display some analog sensor data into processing & also want to control a servo attached to the same Arduino, from the same GUI's buttons
Does the baud rate effects any ways?

James C4S

So you want to attach the Servo to the Serial lines?

Or is your question because you didn't understand that Serial is a bi-directional protocol?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

dattasaurabh82

Your 2nd guess's right.
So you are saying that writing "port.write();" in processing & Serial.println();" in arduino will not interfere with each other? That's great news. :)

James C4S

Why would there be interference?

port.write() in processing will send data to the Arduino which is received with Serial.read().
Serial.println() in Arduino will send data to the PC, which you can use (among other things) processing to receive.

Isn't that the point of serial communication?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

dattasaurabh82

Thanks for that piece of information.

dattasaurabh82

#5
Feb 11, 2013, 06:39 pm Last Edit: Feb 11, 2013, 06:52 pm by Coding Badly Reason: 1
umm :~ facing problem here!!

I've been playing with an X-Band radar & I've copy pasted a code from the forum only .

http://arduino.cc/forum/index.php?topic=91793.new;topicseen#new

My aim is to get the serial data (the change in frequency data) to the processing app through serial communication where I'll visualize it in cool graphics. Along with it I would implant a webcam(To snap the pics of those passing by). Now I want to control the tilting of the webcam through a slider from the app. The whole communication goes at 115200 bps(Can't change it for timer concerns).

-----------------Arduino code------------------------
Code: [Select]
#include <Servo.h>

Servo myservo;

const uint16_t TICK_CNT = 3; // 255-(16MHz/1024/62Hz)  
static uint16_t freq = 0;
double sped = 0; //"speed" seems to be a reserved term


void setup()
{
 myservo.attach(9);
 pinMode(13, OUTPUT);
 digitalWrite(13, HIGH);
 
 Serial.begin(115200);

 noInterrupts();                     // disable all interrupts while we configure  
 // init Timer1 - 16-bit timer/counter
 TCNT1   = 0;                                  // start count at zero.        
 TCCR1B  |= _BV(CS12) | _BV(CS11) | _BV(CS10); // Increment T1 input on each positive edge
                                               // using an external source. Table 16-5, pg 139.
 
 // init Timer2 - 8-bit timer/counter
 TCNT2   = TICK_CNT;                 // preload Timer2 to interrupt every 250 msec
 TIMSK2  = _BV(TOIE2);               // enable the Timer2 overflow interrupt
 TCCR2B  |= _BV(CS22) |_BV(CS21) | _BV(CS20);   // init clock prescaler to 1024. Table 18-9, page 164.
 interrupts();                       // enable all interrupts
 
 //Serial.println("Ready...");
}

ISR(TIMER1_OVF_vect) {
 // do nothing. this is just a dummy ISR in case it actually overflows.
 //Serial.println("Inside Timer1 Overflow Interrupt.");
}

ISR(TIMER2_OVF_vect) {
 //Serial.print("TCNT1: ");
 //Serial.println(TCNT1);
 freq = TCNT1;
 //Serial.println(freq);
 TCNT1 = 0;
 TCNT2 = TICK_CNT;
}

void loop()
{
 while(Serial.available() == 0);
 {
 int data = Serial.read() - '0';
 int pos = map(data, 0, 180, 0, 180);
 pos = constrain(pos, 0, 180);
 myservo.write(pos);
 Serial.flush();
 }
 if (freq != 0)
 {
     freq = freq * 62;      // multiple the frequency * 4 (using leftshift 2 places). 250ms*4 = 1 sec.
     //sped = freq * .03225;  // multiplying freq * 0.03225 will give speed in mph. 31Hz == 1 mph.
                            // see: http://www.microwave-solutions.com/contents/en-uk/d13_System_Design.html
    // Serial.print("Freq: ");
     Serial.println(freq, DEC);
     //Serial.print(" Hz, Speed: ");
     //Serial.print(sped, 0);
     //Serial.println(" mph");
     freq = 0;
 }
}


------------------processing code----------------------------
Code: [Select]
import processing.serial.*;
import codeanticode.gsvideo.*;
import controlP5.*;

PFont fontB;

GSCapture cam;
Serial port;
ControlP5 cp5;

int id = 1;
int freq = 0;
float velocity = 0.00;
int tilt = 0;

void setup()
{
size(680, 420);

port = new Serial(this, "COM14", 115200);
port.bufferUntil('\n');

String[] cameras = GSCapture.list();

fontB = loadFont("ArialMT-48.vlw");
textFont(fontB, 48);

if (cameras.length == 0)
 {
   println("There are no cameras available for capture.");
   exit();
 }
 else
 {
   println("Available cameras:");
   
   for (int i = 0; i < cameras.length; i++)
   {
     println(cameras[i]);
   }
   
   cam = new GSCapture(this, 320, 240, cameras[0]);
   cam.start();  
   }
 
cp5 = new ControlP5(this);

cp5.addSlider("tilt")
    .setPosition(320, 0)
    .setSize(20,240)
    .setRange(0,180)
    .setValue(90)
    ;
}

void draw()
 {
   if (cam.available() == true)
   {
   background(0);
   
   cam.read();
   image(cam, 0, 0);
   
   fill(255);
   text(velocity, 350, 350);
   text("mph", 500, 350);
   println(tilt);
   port.write(tilt);
   
   }
 }

void serialEvent (Serial port)
{
 String inString = port.readStringUntil('\n');//reading from serial data until new line
 
  if (inString != null)
{
 //conversion of the string into integer
 inString = trim(inString);
 freq = int(inString);
 velocity = (freq * .03225)-1.9994999;
 
 if(velocity == 1.9994999)
   {
     velocity = 0.00;
   }
 }
}


help!!! =(


Moderator edit: [code] [/code] tags added.

PaulS

Code: [Select]
int pos = map(data, 0, 180, 0, 180);
Maybe you need a call to pow(pos, 1) after this useless call.

Code: [Select]
  Serial.flush();
If you can't explain EXACTLY what this is doing, you don't need to be calling it. So, don't.

Quote
help!!!

Sure. You need to tell us what the problem is.

dattasaurabh82

i'm sorry for that.
Servo's not responding. even though when I ran an exemplary  "sweep" code just to check whether the servo's ok or not, it worked just fine.

dattasaurabh82

surely I even tried to do it by simply servo.write(the serial value).
it didn't work.

PaulS

Quote
surely I even tried to do it by simply servo.write(the serial value).
it didn't work.

Nor surprising that servo.write('1') doesn't do what you think it does.

If you read and ignore the serial data, and just servo.write() some random value, does the servo move? Have you looked into which timer the Servo library uses? Your diddling with timer 2 may be what is causing the problem.

dattasaurabh82

this is the code:
----------------new sketch-----------------

#include <Servo.h>

Servo myservo;

const uint16_t TICK_CNT = 3; // 255-(16MHz/1024/62Hz) 
static uint16_t freq = 0;
double sped = 0; //"speed" seems to be a reserved term


void setup()
{
  myservo.attach(9);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
 
  Serial.begin(115200);

  noInterrupts();                     // disable all interrupts while we configure 
  // init Timer1 - 16-bit timer/counter
  TCNT1   = 0;                                  // start count at zero.       
  TCCR1B  |= _BV(CS12) | _BV(CS11) | _BV(CS10); // Increment T1 input on each positive edge
                                                // using an external source. Table 16-5, pg 139.
 
  // init Timer2 - 8-bit timer/counter
  TCNT2   = TICK_CNT;                 // preload Timer2 to interrupt every 250 msec
  TIMSK2  = _BV(TOIE2);               // enable the Timer2 overflow interrupt
  TCCR2B  |= _BV(CS22) |_BV(CS21) | _BV(CS20);   // init clock prescaler to 1024. Table 18-9, page 164.
  interrupts();                       // enable all interrupts
 
  //Serial.println("Ready...");
}

ISR(TIMER1_OVF_vect) {
  // do nothing. this is just a dummy ISR in case it actually overflows.
  //Serial.println("Inside Timer1 Overflow Interrupt.");
}

ISR(TIMER2_OVF_vect) {
  //Serial.print("TCNT1: ");
  //Serial.println(TCNT1);
  freq = TCNT1;
  //Serial.println(freq);
  TCNT1 = 0;
  TCNT2 = TICK_CNT;
}

void loop()
{
  while(Serial.available() == 0);
  {
  int pos = Serial.read() - '0';
  myservo.write(pos);
  Serial.flush();
  }

  if (freq != 0)
  {
      freq = freq * 62;      // multiple the frequency * 4 (using leftshift 2 places). 250ms*4 = 1 sec.
      //sped = freq * .03225;  // multiplying freq * 0.03225 will give speed in mph. 31Hz == 1 mph.
                             // see: http://www.microwave-solutions.com/contents/en-uk/d13_System_Design.html
     // Serial.print("Freq: ");
      Serial.println(freq, DEC);
      //Serial.print(" Hz, Speed: ");
      //Serial.print(sped, 0);
      //Serial.println(" mph");
      freq = 0;
  }
}

AWOL

Quote
it didn't work
Code: [Select]
int data = Serial.read() - '0';
 int pos = map(data, 0, 180, 0, 180);
 pos = constrain(pos, 0, 180);
 myservo.write(pos);
 Serial.flush();

Handling multi digit values from a serial port is one of the commonest questions here.
Why didn't you look at some solutions?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dattasaurabh82

So is there any solution here? :(

AWOL

http://arduino.cc/en/Serial/ParseInt
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH

Do you have a good reason for mucking about with the timer hardware like that? It seems unrelated to the problem you describe, and I wouldn't assume that your changes to the timers are done correctly and without any side effects. (For example, those commented-out calls to Serial.println() immediately raise red flags. That code should never ever have been there.)
I only provide help via the forum - please do not contact me for private consultancy.

Go Up