All Forums > Programming Questions

plz help me in servo problem

(1/1)

amrhamed:
Hello Wiring i have written this code of controlling 2 servos motor by  2 pots  wirelessly by 2 arduinos & 2 Xbees & 2 Arduino Wireless shield , but the servos making random moves without any control with the pots , so what's the error in these codes :

--- Code: ---Pots Code @ the First Arduino :

//Define Pins
int potPin = 0;
int potPin2 = 1;

void setup()
{
  //Create Serial Object (9600 Baud)
  Serial.begin(9600);
}

void loop()
{
  int val = map(analogRead(potPin), 0, 1023, 0, 179);
  int val2 = map(analogRead(potPin2), 0, 1023, 0, 179);
  Serial.println(val);
  Serial.println(val2);
  delay(50);
 
}

__________________________________________________________________

Motor Code @ the sec. Arduino :

#include <Servo.h>

//Define Pins
int servoPin = 9;
int servoPin2 = 10;

//Create Servo Object
Servo Servo1;
Servo Servo2;

void setup()
{
 //Start Serial
 Serial.begin(9600);
 
  //Attaches the Servo to our object
  Servo1.attach(servoPin);
  Servo2.attach(servoPin2);
 
  delay(500);
}

void loop()
{

  while( Serial.available() == 0);
  int data = Serial.read() -'0';

  int pos = map(data, 0, 1023, 0, 179);
  int pos2 = map(data, 0, 1023, 0, 179);
  pos = constrain(pos, 0, 179);
  pos2 = constrain(pos, 0, 179);

  //Turn the servo
  Serial.println(pos);
   Serial.println(pos2);
  Servo1.write(pos);
  Servo2.write(pos2);
  Serial.flush();
 
 
}
--- End code ---

sincerely - amr

barragan:
Which software are you using to program your arduino boards? Wiring or Arduino?

Navigation

[0] Message Index

Go to full version