I am trying to make a line following robot using a Pixy2. The robot only has two DC motors on it so when we need to make a turn we just shut one of the motors off. Im not sure what im doing but for some reason my arduino isnt comparing x0 and x1 the way i thought it would. Can you help me see whats wrong?
#include <Pixy2.h>
#include <Pixy2Line.h>
#include <PIDLoop.h>
Pixy2 pixy;
const uint8_t xcenter = 39;
const int en1 = 3;
const int leftmotorpos = 2;
const int leftmotorneg = 4;
const int en2 = 5;
const int rightmotorneg = 7;
const int rightmotorpos = 6;
const int high = 80;
const int low = 0;
PIDLoop headingLoop(5000, 0, 0, false);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println(“Starting…”);
pixy.init();
pixy.setLamp(1, 1);
pixy.changeProg(“line_tracking”);
pinMode (en1, OUTPUT);
pinMode (en2, OUTPUT);
digitalWrite (leftmotorpos, HIGH);
digitalWrite (leftmotorneg, LOW);
digitalWrite(rightmotorpos, HIGH);
digitalWrite(rightmotorneg, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
//uint8_t xtail = m_y0;
//uint8_t ytail = m_y0;
//uint8_t xhead=m_x1;
//uint8_t yhead= m_y1;
int32_t error;
int8_t x1 = pixy.line.vectors->m_x1;
int8_t x0 = pixy.line.vectors->m_x0;
int8_t y1 = pixy.line.vectors->m_y1;
int8_t y0 = pixy.line.vectors->m_y0;
int res = pixy.line.getMainFeatures(1);
// Serial.println (res);
if (res <= 0)
{
analogWrite(en1, 0);
analogWrite(en2, 0);
//Serial.println(res);
return;
}
if (res & LINE_VECTOR)
{
error = (int32_t)pixy.line.vectors->m_x1 - (int32_t) xcenter;
// pixy.line.vectors->print();
headingLoop.update(error);
Serial.println(x0,x1);
//delay(100);
// if (x1 = x0) {
// analogWrite(en1, 200);
// analogWrite(en2, 200);
// }
// else if (x1 > x0) {
//
// analogWrite(en1, 200);
// analogWrite(en2, 0);
// }
// else if (x1 < x0){
// analogWrite(en1, 0);
// analogWrite(en2, 200);
//
// }
//}
}}