Go Down

Topic: Wireless control system problem (Read 5 times) previous topic - next topic

njkl44

So im using two xbees. i need this code to work for a few hours so it needs to be solid. Let me know if you have any questions. Here is what i have:

Send:

Code: [Select]
int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3
int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12

int Finger1 = 2;
int Finger2 = 3;
int Finger3 = 4;
int Finger4 = 5;
int Rotation =  0;


void setup()
{
  Serial.begin(9600);
 pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
 pinMode(dir_a, OUTPUT);
 digitalWrite(dir_a, LOW);
 
}

void loop()
{  
   while((char)Serial.read() !='.');
   
   byte val = Serial.read();
   
   analogWrite(pwm_a, val);  
   
  int FingerV1 = analogRead(Finger1);
  int FingerV2 = analogRead(Finger2);
  int FingerV3 = analogRead(Finger3);
  int FingerV4 = analogRead(Finger4);
  int RotationV1 = analogRead(Rotation);
 
  if (FingerV1 < 30) FingerV1 = 30;
  else if (FingerV1 > 80) FingerV1 = 80;
 
  if (FingerV2 < 45) FingerV2 = 45;
  else if (FingerV2 > 69) FingerV2 = 69;
 
  if (FingerV3 < 22) FingerV3 = 22;
  else if (FingerV3 > 87) FingerV3 = 87;
 
  if (FingerV4 < 12) FingerV4 = 12;
  else if (FingerV4 > 62) FingerV4 = 62;
 
  if (RotationV1 < 300) RotationV1 = 300;
  else if (RotationV1 > 600) RotationV1 = 600;
 
  byte servoVal1 = map(FingerV1,30, 80, 0, 255);//middle
  byte servoVal2 = map(FingerV2,69, 45, 0, 100);//thumb
  byte servoVal3 = map(FingerV3,87, 22, 0, 255);//ring
  byte servoVal4 = map(FingerV4,12, 62, 0, 255);//pointer
  byte servoVal5 = map(RotationV1,300, 600, 0, 255);//Rotation
 
  Serial.print(".");
  Serial.print(servoVal1);
  Serial.print(",");
  Serial.print(servoVal2);
  Serial.print(",");
  Serial.print(servoVal3);
  Serial.print(",");
  Serial.print(servoVal4);
  Serial.print(",");
  Serial.print(RotationV1);
 

  delay(10);
}


Receive:

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

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;

int potpin = A0;
int val;

void setup()
{
  Serial.begin(9600);
 
  myservo1.attach(2); //middle
  myservo2.attach(3); //thumb
  myservo3.attach(4); //ring
  myservo4.attach(5); //pinky
  myservo5.attach(6); //pointer
  myservo7.attach(7); //swivel
 
  Serial.print(".");
}

void loop()
{

  while((char)Serial.read() !='.');
 
  byte vals[5];
  vals[0] = Serial.read();
  Serial.read();
  vals[1] = Serial.read();
  Serial.read();
  vals[2] = Serial.read();
  Serial.read();
  vals[3] = Serial.read();
  Serial.read();
  vals[4] = Serial.read();
 
   myservo1.write(vals[0]);
   myservo2.write(vals[1]);
   myservo3.write(vals[2]);
   myservo4.write(vals[2]);
   myservo5.write(vals[3]);
   myservo7.write(vals[4]);
   
 val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
 byte val = map(val, 0, 950, 0, 255);     // scale it to use it with the servo (value between 0 and 180)                 // sets the servo position according to the scaled value
 if (val>250){val=250;}
 Serial.print(".");
 Serial.print(val);
 
}


dxw00d

Quote
Let me know if you have any questions.


What question are you asking?

In both sketches, you are doing Serial.reads, without checking that there is anything to read.

njkl44


Quote
Let me know if you have any questions.


What question are you asking?

In both sketches, you are doing Serial.reads, without checking that there is anything to read.

So do I just use if serial available? If so where would I put it?

Nick Gammon


PeterH



Let me know if you have any questions.



Does it compile?

Does it do what you intend?

Does it do it reliably for as long as you need?
I only provide help via the forum - please do not contact me for private consultancy.

Go Up