Pages: [1] 2   Go Down
Author Topic: Servo control  (Read 1738 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



* Presentation1.png (228.98 KB, 1344x960 - viewed 28 times.)

* Presentation1.png (228.98 KB, 1344x960 - viewed 25 times.)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

power will also be provided to the second arduino.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be helpful, after getting on for 50 posts, if you would put code inside [code][/code] tags and save me the burden of doing it for you.
Logged

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//-------------------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:
//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:
//------------------------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);
  }
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And out of all the methods discussed on that thread, you decided to go with the one that required hardware you don't have?
Logged

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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. smiley-sad 


* 130212-235520.jpg (78.83 KB, 587x480 - viewed 22 times.)

* no-3.png (108.8 KB, 940x672 - viewed 24 times.)
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What does this code do?

Code:
while(Wire.available() == 0)
{
  x = Wire.read();    // receive byte as an intege
}
Logged

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 82
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

\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.
Logged

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

Pages: [1] 2   Go Up
Jump to: