Pages: 1 2 [3] 4 5   Go Down
Author Topic: Wireless control system problem  (Read 4639 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

Quote
How am i throwing away the character read?
You call Serial.read() to get the next value in the buffer. If you don't store the value, what do you think happens to it?
Doesnt this store it?
Code:
vals[4] = Serial.read();
So im waiting until i have all 5 values then im moving the servos with them right?
Logged

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

Quote
Doesnt this store it?
Yes.

This, on the other hand, throws it away:
Code:
   Serial.read();
Logged

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

Quote
Doesnt this store it?
Yes.

This, on the other hand, throws it away:
Code:
   Serial.read();
So i get rid of those. Separate it at the commas and convert them?
Logged

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

Quote
So i get rid of those. Separate it at the commas and convert them?
Yes.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you still calling Serial.read(), and throwing away the character read?

He's getting rid of what he hopes, without any justification whatsoever, is a comma.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Capture the whole lot into a buffer.
So i have this now:

Code:
...
   vals[0] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[1] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[2] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[3] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[4] = Serial.read();
   flash ();
...


I don't think you are paying the slightest bit of attention to what I am saying. You look at the page, grab a bit of code, stick it in, and hope for the best. What is the "flash ()" for?

What you are doing there is not putting the incoming text into a buffer, as I have recommended about three or four times now.
Logged


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

Why are you still calling Serial.read(), and throwing away the character read?

He's getting rid of what he hopes, without any justification whatsoever, is a comma.
So i do want to keep the Serial.read() to get rid of the comma?
Logged

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

Capture the whole lot into a buffer.
So i have this now:

Code:
...
   vals[0] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[1] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[2] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[3] = Serial.read();
   flash ();
   Serial.read();
   flash ();
   
   vals[4] = Serial.read();
   flash ();
...


I don't think you are paying the slightest bit of attention to what I am saying. You look at the page, grab a bit of code, stick it in, and hope for the best. What is the "flash ()" for?

What you are doing there is not putting the incoming text into a buffer, as I have recommended about three or four times now.
I guess im just unsure how to but the incoming text into a buffer. Im new at coding and come here for help. I thank you so much for putting effort in trying to teach and help! I just need to understand a bit more. How would i put the incoming text into a buffer?

Thank you again!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The link I gave you about 2 or 3 pages back now, more than once, shows how to put text into a buffer. You are choosing to ignore the methods shown there, which I put there to teach people how to do this, and sticking with your code, which we have said won't work. The top part of that page. Where it says "Buffering input".
Logged


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

The link I gave you about 2 or 3 pages back now, more than once, shows how to put text into a buffer. You are choosing to ignore the methods shown there, which I put there to teach people how to do this, and sticking with your code, which we have said won't work. The top part of that page. Where it says "Buffering input".
So help me understand this

Do i go something like this?
Code:
const unsigned int MAX_INPUT = 50;

void setup ()
{
  Serial.begin(9600);
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {
  // for now just display it
  Serial.println (data);
  }  // end of process_data
 

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0)            **change this to something like:  if (Serial.available () > 8) because there are 9 characters **
    {
    char inByte = Serial.read ();              **times this by 9? **

    switch (inByte)
      {

      case '\n':   // end of text
        input_line [input_pos] = 0;  // terminating null byte
       
        // terminator reached! process input_line here ...  **Have the  char inByte's control the servos here? **
        process_data (input_line);
       
        // reset buffer for next time
        input_pos = 0; 
        break;
 
      case '\r':   // discard carriage return
        break;
 
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop
Do i need to add more to anything else to this? Also would it be good to add that flash(); function to read the better for this too?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This works with what I presume is your data:

Code:
/*

Author: Nick Gammon
Date:   2nd February 2012

 example input: .123,100,92,42,99/

*/

// how much serial data we expect before a terminator
const unsigned int MAX_INPUT = 100;
const char TERMINATOR = '/';
const char STARTER = '.';

void setup ()
{
  Serial.begin(115200);
} // end of setup

int middle, thumb, ring, pointer, rotation;

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {

  // convert strings into numbers:

  middle = atoi (strtok (data, ","));
  thumb = atoi (strtok (NULL, ","));
  ring = atoi (strtok (NULL, ","));
  pointer = atoi (strtok (NULL, ","));
  rotation = atoi (strtok (NULL, ","));
 
// control the servos here!

// for now I am displaying what I got ...

  Serial.print ("middle = ");
  Serial.println (middle);
  Serial.print ("thumb = ");
  Serial.println (thumb);
  Serial.print ("ring = ");
  Serial.println (ring);
  Serial.print ("pointer = ");
  Serial.println (pointer);
  Serial.print ("rotation = ");
  Serial.println (rotation);
 
  }  // end of process_data
 

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0)
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case TERMINATOR:   // end of text
        input_line [input_pos] = 0;  // terminating null byte
       
        // terminator reached! process input_line here ...
        process_data (input_line);
       
        // reset buffer for next time
        input_pos = 0; 
        break;
 
      case STARTER: 
        // reset buffer
        input_pos = 0;       
        break;
 
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop

Quote
  if (Serial.available () > 0)            **change this to something like:  if (Serial.available () > smiley-cool because there are 9 characters **

No. It gets a byte at a time. That's the WHOLE idea.

Quote
    char inByte = Serial.read ();              **times this by 9? **

No. It adds to a buffer. You don't do 9 reads. How do you know it is 9? How do you know in advance if you are getting 1-digit, 2-digit, 3-digit numbers? Try to think through what you are sending.
Logged


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

This works with what I presume is your data:

Code:
/*

Author: Nick Gammon
Date:   2nd February 2012

 example input: .123,100,92,42,99/

*/

// how much serial data we expect before a terminator
const unsigned int MAX_INPUT = 100;
const char TERMINATOR = '/';
const char STARTER = '.';

void setup ()
{
  Serial.begin(115200);
} // end of setup

int middle, thumb, ring, pointer, rotation;

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {

  // convert strings into numbers:

  middle = atoi (strtok (data, ","));
  thumb = atoi (strtok (NULL, ","));
  ring = atoi (strtok (NULL, ","));
  pointer = atoi (strtok (NULL, ","));
  rotation = atoi (strtok (NULL, ","));
 
// control the servos here!

// for now I am displaying what I got ...

  Serial.print ("middle = ");
  Serial.println (middle);
  Serial.print ("thumb = ");
  Serial.println (thumb);
  Serial.print ("ring = ");
  Serial.println (ring);
  Serial.print ("pointer = ");
  Serial.println (pointer);
  Serial.print ("rotation = ");
  Serial.println (rotation);
 
  }  // end of process_data
 

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0)
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case TERMINATOR:   // end of text
        input_line [input_pos] = 0;  // terminating null byte
       
        // terminator reached! process input_line here ...
        process_data (input_line);
       
        // reset buffer for next time
        input_pos = 0; 
        break;
 
      case STARTER: 
        // reset buffer
        input_pos = 0;       
        break;
 
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop

Quote
  if (Serial.available () > 0)            **change this to something like:  if (Serial.available () > smiley-cool because there are 9 characters **

No. It gets a byte at a time. That's the WHOLE idea.

Quote
    char inByte = Serial.read ();              **times this by 9? **

No. It adds to a buffer. You don't do 9 reads. How do you know it is 9? How do you know in advance if you are getting 1-digit, 2-digit, 3-digit numbers? Try to think through what you are sending.
Alright. So do i even need to send the commas? So i am sending 5 sensor values, once it reads those it store them in a buffer? but then how do i access them and move the servos with them?
Logged

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

Quote
So do i even need to send the commas? So i am sending 5 sensor values, once it reads those it store them in a buffer? but then how do i access them and move the servos with them?
Yes, you need to send the commas. If you didn't, how would you know where one value ended and the next began. Here is a string of servo values. Move your servos to the correct positions:
125678011

Now, here is another string:
125,67,8,0,11

The second string allows you to know where to break it, using strtok() to get tokens. With the tokens, you can call atoi() to get integer values that the servos can be moved to.
Logged

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

This works with what I presume is your data:

Code:
/*

Author: Nick Gammon
Date:   2nd February 2012

 example input: .123,100,92,42,99/

*/

// how much serial data we expect before a terminator
const unsigned int MAX_INPUT = 100;
const char TERMINATOR = '/';
const char STARTER = '.';

void setup ()
{
  Serial.begin(115200);
} // end of setup

int middle, thumb, ring, pointer, rotation;

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {

  // convert strings into numbers:

  middle = atoi (strtok (data, ","));
  thumb = atoi (strtok (NULL, ","));
  ring = atoi (strtok (NULL, ","));
  pointer = atoi (strtok (NULL, ","));
  rotation = atoi (strtok (NULL, ","));
 
// control the servos here!

// for now I am displaying what I got ...

  Serial.print ("middle = ");
  Serial.println (middle);
  Serial.print ("thumb = ");
  Serial.println (thumb);
  Serial.print ("ring = ");
  Serial.println (ring);
  Serial.print ("pointer = ");
  Serial.println (pointer);
  Serial.print ("rotation = ");
  Serial.println (rotation);
 
  }  // end of process_data
 

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0)
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case TERMINATOR:   // end of text
        input_line [input_pos] = 0;  // terminating null byte
       
        // terminator reached! process input_line here ...
        process_data (input_line);
       
        // reset buffer for next time
        input_pos = 0; 
        break;
 
      case STARTER: 
        // reset buffer
        input_pos = 0;       
        break;
 
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop

Quote
  if (Serial.available () > 0)            **change this to something like:  if (Serial.available () > smiley-cool because there are 9 characters **

No. It gets a byte at a time. That's the WHOLE idea.

Quote
    char inByte = Serial.read ();              **times this by 9? **

No. It adds to a buffer. You don't do 9 reads. How do you know it is 9? How do you know in advance if you are getting 1-digit, 2-digit, 3-digit numbers? Try to think through what you are sending.
So luckily i was reading this over and i saw that there was more code that you posted. code doesnt seem to be very mobile friendly. I added in the servo part and everything. does this look good? do i need the flash(); function if im going to send at 9600?
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;

// how much serial data we expect before a terminator
const unsigned int MAX_INPUT = 100;
const char TERMINATOR = '/';
const char STARTER = '.';

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
} // end of setup

int middle, thumb, ring, pointer, rotation;

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {

  // convert strings into numbers:

  middle = atoi (strtok (data, ","));
  thumb = atoi (strtok (NULL, ","));
  ring = atoi (strtok (NULL, ","));
  pointer = atoi (strtok (NULL, ","));
  rotation = atoi (strtok (NULL, ","));
 
    myservo1.write(middle);
    myservo2.write(thumb);
    myservo3.write(ring);
    myservo4.write(ring);
    myservo5.write(pointer);
    myservo7.write(rotation);

// for now I am displaying what I got ...

  Serial.print ("middle = ");
  Serial.println (middle);
  Serial.print ("thumb = ");
  Serial.println (thumb);
  Serial.print ("ring = ");
  Serial.println (ring);
  Serial.print ("pointer = ");
  Serial.println (pointer);
  Serial.print ("rotation = ");
  Serial.println (rotation);
 
  }  // end of process_data
 

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0)
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case TERMINATOR:   // end of text
        input_line [input_pos] = 0;  // terminating null byte
       
        // terminator reached! process input_line here ...
        process_data (input_line);
       
        // reset buffer for next time
        input_pos = 0; 
        break;
 
      case STARTER: 
        // reset buffer
        input_pos = 0;       
        break;
 
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18767
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So luckily i was reading this over and i saw that there was more code that you posted. code doesnt seem to be very mobile friendly.

It was indeed lucky that you read the code I posted. Otherwise my time would have been wasted.
Logged


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