Serial communication querry

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?

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?

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. :slight_smile:

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?

Thanks for that piece of information.

umm :~ facing problem here!!

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

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------------------------

#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----------------------------

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:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

int pos = map(data, 0, 180, 0, 180);

Maybe you need a call to pow(pos, 1) after this useless call.

  Serial.flush();

If you can't explain EXACTLY what this is doing, you don't need to be calling it. So, don't.

help!!!

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

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.

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

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.

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;
}
}

it didn't work

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?

So is there any solution here? :frowning:

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.)

It may be due to the timers or the bi directional communication. I tried to drive the servo from an simple UI of just slider & it worked.
--------------arduino code------------
#include <Servo.h>

Servo myservo;

void setup()
{
myservo.attach(9);
Serial.begin(115200);
}

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

------------processing code-------------

import processing.serial.;
import controlP5.
;

Serial port;
ControlP5 cp5;

int tilt = 0;

void setup()
{
size(500, 500);

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

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

void draw()
{
background(0);
port.write(tilt);
}

.

I tried to drive the servo from an simple UI of just slider & it worked.

As long as you don't move the slider very far, that will work.

If adding code to diddle with timer2 stops the servo from working, then the only logical conclusion is that the servo library uses that timer in conjunction with that pin.

comments are comments right? those are meant to check the speed results in the serial monitor.
Now I'm just porting the frequency data to the port from the arduino & TRYING TO ACCEPept slider value from the port to the arduino.
I'm not that much High end with timers though. I explained my aim also that why i need those timer interrupts as then the radar won't work.

So how this shall be solved? Is there any way? Sorry but, I'm needing a solution here as I'm not much of into hardcore micro-controller stuffs.
And the parseInt link that's been forwarded to me, doesn't have any example code.

And I don't have any extra port here as I'm using UNO R3. would it work if I use two different ports for servo & RAdar.Would it help me if I go for a mega?