Use 3pi as slave and arduino uno as master

I’m new to coding . I have connected 3pi pololu robot to arduino uno via UART, using arduino uno as master controller. I want to control the movement of the wheels of 3 pi . can i get master and slave code for it . I’ve read the master and slave codes on pololu guide but can’t understand it .

Hello.

We do not have a serial master and slave program written specifically for an Arduino to control the 3pi robot. You can still use our serial slave program for the 3pi, but you would need to write your own master program for the Arduino. The master program essentially sends certain commands that the slave program responds to via serial. (The available commands are listed under the “Serial slave program” section in the Pololu 3pi Robot User’s Guide.)

You can use the source code in the “Serial master program” section of the user’s guide as a reference for your Arduino code. For example, you can probably use the slave_set_motors function in your Arduino sketch by replacing serial_send_blocking(message,4) with Serial.write(message,4). You can find more details on Serial.write() on the Reference page on the official Arduino website.

- Amanda

Thank you mam , I’ll try and revert back to you .

I’ve written a master code for arduino uno in arduino IDE , using Serial.write . But now I’m stuck with slave program , cause the code given on pololu website for slave is usin C/C++ and their different libraries .

Can you help me with a simple arduino code for the slave 3pi , to read the commands of the motor .

//this is my master code for arduino uno
#include <Pixy2.h>
#include <SPI.h>


Pixy2 pixy;
//int Pixy_OnOff = 0;
int signature = 0;
int xblocks = 0;
int yblocks = 0;
unsigned int width = 0;
unsigned int height = 0;
unsigned int area = 0;
unsigned int newarea = 0;
int Xmin = 70;  //left
int Xmax = 200; //right
int maxArea = 0;
int minArea = 0;
static int isignature = 0;


// set the motor speeds
void slave_set_motors(int speed1, int speed2)
{ char message[4]={0xC1,speed1,0xC5,speed2};
  if(speed1<0)
  { message[0]=0xC2; //m1 backward
    message[1]=-speed1;}
  if(speed2<0)
  { message[2]=0xC6; //m2 backward
    message[3]=-speed2;} 
  Serial.write(message,4); 
  }



void Pixy_scan()
{
  uint16_t blocks;
  blocks = pixy.ccc.getBlocks();
  signature = pixy.ccc.blocks[isignature].m_signature;
  xblocks = pixy.ccc.blocks[isignature].m_x;
  width = pixy.ccc.blocks[isignature].m_width;
  height = pixy.ccc.blocks[isignature].m_height;
}

void setup() {
   // Initialize serial communications at 9600 bps:
  Serial.begin(9600);

  // Initialize Pixy2
  pixy.init();

}

void loop() {
    Pixy_scan();
    area = width * height;
    maxArea = area + 1000;
    minArea = area - 1000;

  // Pixy Auto Demo
  
    if (signature == 1)
    {
      newarea = width * height;

      if (xblocks < Xmin)
      {
        slave_set_motors(-125,125);
        Serial.print("TurnLeft\n");
      }
      else if (xblocks > Xmax)
      {
        slave_set_motors(125,-125);
        Serial.print("TurnRight\n");
      }
      else if (newarea < minArea)
      {
        slave_set_motors(125,125);
        Serial.print("MoveForward\n");
      }
      else if (newarea > maxArea)
      {
        slave_set_motors(-125,-125);
        Serial.print("MoveBackward\n");
      }

      else
      {
        slave_set_motors(0,0);
        Serial.print("Stay\n");
      }
    }
    else
    {
      slave_set_motors(0,0);
      Serial.print("Stay\n");
    }
  }

It sounds like you might be trying to program your 3pi robot using the Arduino IDE. Is that correct? Instead of rewriting the slave program, I recommend just using Atmel Studio to program the 3pi with the serial slave program as-is. That way is much easier than porting over code for your specific setup. You can look at the Pololu AVR Programming Quick Start Guide to help you get started. If you’re unfamiliar with using Atmel Studio, you can find lots of tutorials online.

- Amanda