Re: Serial Input Basics

Robin2:
…continued from previous post

Binary data

So far we have been receiving character data - for example the number 137 is represented by the characters ‘1’, ‘3’ and ‘7’. It is also possible to send that value as binary data in a single byte.

If you need to receive binary data rather than ascii characters just change the line
char receivedChars[numChars];
to
byte receivedChars[numChars];

and the line
char rc;
to
byte rc;

and if you want to continue using the showNewData() function you will also need to change
Serial.println(receivedChars);
to
Serial.println((char*)receivedChars);

Hey Robin, now Im learning and using this demo. I found that I would like to send 4 numbers which ranges from 0-180 to control servo and those numbers are changed whenever I change the potentiometers. Therefore, I put in array[4]
For instance
Array[0]=150;
Array[1]=180;
Array[2]=90;
Array[3]=70;
However I found that to use your code. I need put starmaker and end marker after and before number, so my code does not work If I use your code.
To use your code, in my sending code, I need to :

void loop() {
print("<");

print(array[0]):

print[">");

print("<");

print(array[1]);

print(">");

print("<");

print(array[2]);

print(">");

print("<");

print(array[3]);

print(">");

}

I think that it is so complicated. Do you have any suggestion to solve problem. I also would like to mention that whenever I change value of 4 potentiomers and send them, how I can receive correct 4 numbers in order to control servo.
Sincere thanks!

I have asked the Moderator to move your question to its own Thread as it is about your Project rather than about my Tutorial.

First, please post a complete program, not just a snippet.

Your code does not look like Arduino code. A regular Arduino program uses Serial.print() rather than print().

What would be wrong with something like (not tested)

Serial.print('<');
for (byte n = 0; n < numElements; n++) {
   Serial.print(array[n]);
   if (n < numElements - 1) {
      Serial.print(',');
   }
}
Serial.println('>');

…R

Robin doesn't have to answer every Serial question, so I'd like to help out.

The pseudocode posted has a problem: how does the receiver know which number it's receiving? If you switch the devices on one at a time, the first number received might be the 3rd number transmitted. Robin's code uses start/end markers around all four numbers, so it's obvious when a whole block is recieved. It also transmits the numbers in a human-readable format instead of using binary. You're welcome to use binary but it's usually much easier to work with it if you can simply look at the data stream in the serial monitor.

MorganS:
Robin doesn't have to answer every Serial question, so I'd like to help out.

Very welcome. Thanks.

...R

Hey Robin, now Im learning and using this demo. I found that I would like to send 4 numbers which ranges from 0-180 to control servo and those numbers are changed whenever I change the potentiometers.

Some servo/pot/serial Tx test code and some servo/serial Rx test code

Tx

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing 
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval3-2) || newval3 > (oldval3+2)){  
    myservo3.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval4 < (oldval4-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval5 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo5.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing, adjust as needed
}

Rx

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}