Serial issue

Hello all I am trying to download this program onto my Arduino uno and there is a problem with the serial. and serial1. I need both in order to use the program so I’m unsure of what i need to add or fix.

Arduinoweb.txt (3.53 KB)

(deleted)

1) There is no Serial1 on an UNO. It only has Serial. To get another serial port, the best alternative is AltSoftSerial on pins 8 & 9. Next best is NeoSWSerial on any two pins. Worst choice is SoftwareSerial.

2) Your program is small enough to fit in a code block. Just paste it in the post, select all the code text, then press the “Code” button in the upper left (the </> button). It will look like this:

const int PING_MAP[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};


const char input_template[] = "<+##,+##>";


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

void loop()
{
  // Get serial input from RPi
  String serial_input = get_serial_in();

  // Process serial input from RPi
  process_serial_in(serial_input);

  // Process local data to send to RPi
  String serial_output = process_serial_out();

  // Send serial data to RPi
  send_serial_out(serial_output);

}

String get_serial_in()
{

  Serial.println("Getting serial buffer");

  // Wait until we have a command to execute
  while (true)
  {

    // If at least one command is in the serial buffer

    if (Serial.available() >= sizeof(input_template) - 1)  {

      // If there's two or more commands in the buffer
      // Get rid of all but one

      while (Serial.available() > 2*sizeof(input_template)) {
        Serial.read();
      }


      // If there's less than two commands, grab the full one

      char rc;

      // Look for the starting character


      while ((rc = Serial.read()) != '<') {
        //rc = Serial.read();
      }

      String serial_string;

      // While we haven't read the end character

      while ((rc = Serial.read()) != '>') {
        serial_string += rc;
      }

      // serial_string should now be "+##,+##"

      return serial_string;

      // Else wait for a full command
    }

  }

}

void process_serial_in(String serial_input)
{
  serial_input.trim();

  // serial_input is of the form "+###,+###"

  int sign_x = (serial_input[0] == '+') ? 1 : -1;
  int sign_z = (serial_input[serial_input.indexOf(',')+1] == '+') ? 1 : -1;

  String x_str = serial_input.substring(1,serial_input.indexOf(',')-1);
  String z_str = serial_input.substring(serial_input.indexOf(',')+2);

  int x = sign_x * x_str.toInt();
  int z = sign_z * z_str.toInt();

  write_motor_commands( x, z);

  //Serial.print("X value is ");
  //Serial.print(x);
  //Serial.print("| Z value is ");
  //Serial.println(z);
}

String process_serial_out()
{
  String serial_out = "";

  for ( int i = 1; i < sizeof(PING_MAP)/sizeof(int); i++) {
    serial_out += String(read_ping(PING_MAP[i]));
    serial_out += '_';
  }

  return serial_out;
}

void send_serial_out(String serial_output)
{
  Serial.println(serial_output);
}

long read_ping(int pin_number)
{
  pinMode(pin_number, OUTPUT);
  digitalWrite(pin_number, LOW);

  delayMicroseconds(3);

  digitalWrite(pin_number, HIGH);

  delayMicroseconds(10);

  digitalWrite(pin_number, LOW);

  pinMode(pin_number, INPUT);

  long result = pulseIn(pin_number, HIGH);
  delay(25);


  return result;
}

void write_motor_commands(int x, int rotz)
{
  int left_int = (x + rotz)/100*128 + 127;
  int right_int = (x - rotz)/100*128 + 127;

  byte left_byte = (byte)left_int;
  byte right_byte = (byte)right_int;

  write_left_motor(left_byte);
  write_right_motor(right_byte);
} 

void write_left_motor(byte value)
{
  Serial1.write(0xFF);
  Serial1.write(1);
  Serial1.write(value); 
}

void write_right_motor(byte value)
{
  Serial1.write(0xFF);
  Serial1.write(2);
  Serial1.write(value); 

}

And what’s with all those blank lines? I deleted more than half the lines in the program!

3) Read Serial Input Basics. Your program structure is inside-out from what it should be doing: gradually accumulate an buffer with a special begin and end char. Then parse the buffer.

4) Don’t Use String™. It will eventually cause problems that are hard to find.

I really don't think you all understand how much this has helped me. Thank you so much

There is a Serial1 on the 32U4 based boards.