Go Down

Topic: Servo control (Read 2050 times) previous topic - next topic

dattasaurabh82

Can I use the serial value from a processing app to change an analog PIN's value of arduino  with Serial.read() & analoWrite() & connect that analog pin to another arduino's Same/Another Analog PIN. Then use the analogRead() in the 2nd arduino to read the value coming over that PIN & set  position of a servo according to the value. Can it be done? I can use an ATtiny in that case to drive the servo separate. This way it won't have to be dependent on the RADAR-ARDUINO's timers. there's a reason I want to do that

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

When I tried this( sketches below), it didn't work.No motion in servo.

-------------------ARDUINO CODE----------------------
Arduino 1 code:

void setup()
{
 Serial.begin(9600);
}


void loop()
{
 while(Serial.available() == 0);
 {
   int pos = Serial.read() - '0';
   analogWrite(A0, pos);
   Serial.flush();
 }
}

--------------------------------------------------------------------------------------------
Arduino 2 code:

#include <Servo.h>

Servo myservo;

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


void loop()
{
 int data = analogRead(A0);
 Serial.println(data);
 myservo.write(data);
 //Servo.flush();
}

------------------------PROCESSING APPLET-------------------

code:

import processing.serial.*;
import controlP5.*;

Serial port;
ControlP5 cp5;

int tilt = 0;

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

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

cp5 = new ControlP5(this);

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

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


dattasaurabh82

power will also be provided to the second arduino.

AWOL

On anything but a Due, "analogWrite" modifies the duty cycle of a digital signal, so an "analogRead" of such a signal will not return the value expected, without filtering.
"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

I'm new to this. So what shall exactly be done?

AWOL

I have no idea - you've set out a method of doing something that isn't feasible, without saying what it is you want to do.

Quote
This way it won't have to be dependent on the RADAR-ARDUINO's timers.

Whatever that means.
"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

I think I've given a link back up there about another discussion. It'll give you a better insight about what I'm up-to. please go through 'em

dattasaurabh82

And more over, it would be just helpful if you could help me with the problem I raised here. This way you would be saved of a lot of burden. I need to know about the filtering as you said up there & modifying it in my code will help me better understand .
Thank you

AWOL

It would be helpful, after getting on for 50 posts, if you would put code inside [font=Verdana][[/font]code][font=Verdana][[/font]/code] tags and save me the burden of doing it for you.
"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

Code: [Select]

//-------------------ARDUINO CODE----------------------//
//Arduino 1 code:

void setup()
{
  Serial.begin(9600);
}


void loop()
{
  while(Serial.available() == 0);
  {
    int pos = Serial.read() - '0';
    analogWrite(A0, pos);
    Serial.flush();
  }
}


Code: [Select]

//Arduino 2 code:

#include <Servo.h>

Servo myservo;

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


void loop()
{
  int data = analogRead(A0);
  Serial.println(data);
  myservo.write(data);
  //Servo.flush();
}


Code: [Select]

//------------------------PROCESSING APPLET-------------------//

//code:

import processing.serial.*;
import controlP5.*;

Serial port;
ControlP5 cp5;

int tilt = 0;

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

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

cp5 = new ControlP5(this);

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

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

dattasaurabh82

Searching for a while, i found these
http://arduino.cc/forum/index.php?topic=116015.0
where a similar problem was raised & a person suggested something like I thought with analog read & write ()s .

PeterH

And out of all the methods discussed on that thread, you decided to go with the one that required hardware you don't have?
I only provide help via the forum - please do not contact me for private consultancy.

dattasaurabh82

Well I tried with both the low pass filter way & the  i2c way. low pass filter had some glitches. Sometimes it worked just fine. The system is not dependable for crucious moments. moving on to I2C.

The code for the arduino which will receive serail data from the Processing app is :

Code: [Select]
#include <Wire.h>

int data = 0;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
}

void loop()
{
  while(Serial.available() == 0)
  {
    int data = Serial.read() - '0';
    Wire.beginTransmission(4);
    Wire.write(data);
    Wire.endTransmission();
    Serial.flush();
  }
  delay(50);
}


& the code for the arduino that is connected to the previous arduino with I2C & a servo is

Code: [Select]

#include <Wire.h>
#include <Servo.h>

Servo myservo;
int x = 0;

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  //Serial.begin(9600);
  myservo.attach(9);
}

void loop()
{
  //delay(100);
  myservo.write(x);
  //Servoflush();
}

void receiveEvent(int howMany)
{
  while( Wire.available() == 0) // loop through all but the last
  {
      x = Wire.read();    // receive byte as an intege
  }
}


The problem is the servo keeps on moving . Never stops & is not affected by the serial data coming over the first arduino.
I guess there is some random data like the -49 for the subtraction (- '0') that we are employing in the first arduino program is causing the problem. My best guess. Any more problems sorted out & fixable is needed. help again. :( 

PeterH

What does this code do?

Code: [Select]

while(Wire.available() == 0)
{
  x = Wire.read();    // receive byte as an intege
}
I only provide help via the forum - please do not contact me for private consultancy.

dattasaurabh82

\until & unless the data over I2C is 0, it'll keep on reading & set that value to a variable x. This variable is then used to define the position of a servo

PeterH


\until & unless the data over I2C is 0, it'll keep on reading & set that value to a variable x. This variable is then used to define the position of a servo


Look again.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up