Pages: [1] 2 3 ... 5   Go Down
Author Topic: Wireless control system problem  (Read 3362 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Before the reads?

http://gammon.com.au/serial
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11138
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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?
it does compile. Other than that nothing works.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So im using two xbees.
About which you have told us what? Nothing. Nothing about the type. Nothing about how they are attached to the Arduino. Nothing about how they are configured. Nothing about whether you know that they are communicating.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So im using two xbees.
About which you have told us what? Nothing. Nothing about the type. Nothing about how they are attached to the Arduino. Nothing about how they are configured. Nothing about whether you know that they are communicating.
Well that shouldnt matter to much should it? they are just standard xbees attached with a shield. I have config them with a custom channel so only they can talk to eachother.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this?
Code:
void loop()
{
  if (Serial.available ())

   while((char)Serial.read() !='.');
   
   byte vals[5];
   vals[0] = Serial.read();
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well that shouldnt matter to much should it?
I guess that it doesn't matter that they are compatible. Doesn't matter that they might be attached to shields that are configured to use something other than the Serial port. Doesn't matter that they might not be configured correctly.

You have two Arduinos that are failing to properly exchange data. Ignoring the mechanism that is used to exchange the data hardly seems like a good idea.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well that shouldnt matter to much should it?
I guess that it doesn't matter that they are compatible. Doesn't matter that they might be attached to shields that are configured to use something other than the Serial port. Doesn't matter that they might not be configured correctly.

You have two Arduinos that are failing to properly exchange data. Ignoring the mechanism that is used to exchange the data hardly seems like a good idea.
I have tested those radios before and they can talk to each other and communicate. The shields are xbee shields and those have worked before also. Its not a problem with the radios or anything physical i have had all of it working before. Its just code issues.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this?

No. Did my site have code like that there? Look,you have this:

Code:

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

If there is no data Serial.date will return -1, so the "while" part will work OK. It will sit there until a dot arrives. Then, very quickly, you grab 5 more bytes, which since your processor is much faster than a serial port, will almost certainly be -1. So you have filled vals array with -1 (turned into 0xFF since vals is not an int).

Look at the code on the link I posted. First you just read and fill an array with data (using Serial.available() for every byte). Then when some delimiter comes, you go back and process it.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've added some more examples to the link above.

In particular, this shows how fast you are reading at 9600 baud compared to how fast the data arrives:



See that line at the bottom? That is all 5 reads executing (in my test code). Even at a much faster baud rate, it can't even read a second byte:


Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 192
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this?

No. Did my site have code like that there? Look,you have this:

Code:

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

If there is no data Serial.date will return -1, so the "while" part will work OK. It will sit there until a dot arrives. Then, very quickly, you grab 5 more bytes, which since your processor is much faster than a serial port, will almost certainly be -1. So you have filled vals array with -1 (turned into 0xFF since vals is not an int).

Look at the code on the link I posted. First you just read and fill an array with data (using Serial.available() for every byte). Then when some delimiter comes, you go back and process it.
Are you talking about something like this?

Code:
void loop()
{

   while((char)Serial.read() !='.');
   
   if (Serial.available() >0)
   {
   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]);
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11138
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Are you talking about something like this?


Why do you keep ignoring what Nick Gammon tells you? You code looks nothing like what he is suggesting.
Logged

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

Pages: [1] 2 3 ... 5   Go Up
Jump to: