Day 5

Parts were finally here. I wired and got the line follower working. The code itself is very simple. Just let the robot go straight and adjust the based on the difference and it’s all good .

const int MOTOR_LEFT_POS=7;
const int MOTOR_LEFT_NEG=8;
const int MOTOR_RIGHT_POS=12;
const int MOTOR_RIGHT_NEG=13;
const int MOTOR_LEFT_PWM=10;
const int MOTOR_RIGHT_PWM=11;

#include <Wire.h>
#define uchar unsigned char
uchar t;
//void send_data(short a1,short b1,short c1,short d1,short e1,short f1);
uchar data[16];

void setup() {
pinMode(MOTOR_LEFT_POS,OUTPUT);
pinMode(MOTOR_LEFT_NEG,OUTPUT);
pinMode(MOTOR_RIGHT_POS,OUTPUT);
pinMode(MOTOR_RIGHT_NEG,OUTPUT);
digitalWrite(MOTOR_LEFT_POS,HIGH);
digitalWrite(MOTOR_RIGHT_POS,LOW);
digitalWrite(MOTOR_LEFT_NEG,LOW);
digitalWrite(MOTOR_RIGHT_NEG,HIGH);
Serial.begin(9600);
Wire.begin();        // join i2c bus (address optional for master)
t = 0;
//delay(5000);
}

void drive(int left, int right){
analogWrite(MOTOR_LEFT_PWM,left);
analogWrite(MOTOR_RIGHT_PWM,right);
}

void loop() {
Wire.requestFrom(9, 16);    // request 16 bytes from slave device #9
while (Wire.available())   // slave may send less than requested
{
 data[t] = Wire.read(); // receive a byte as character
 if (t < 15)
   t++;
 else
   t = 0;
}

int readingLeft=(data[0]+data[2]+data[4])/3;
int readingRight=(data[10]+data[12]+data[14])/3;
int readingMid=(data[6]+data[8])/2;
Serial.print(readingLeft);
Serial.print(' ');
Serial.print(readingMid);
Serial.print(' ');
Serial.println(readingRight);

int diff=readingMid-(readingLeft+readingRight)/2;
drive(127+diff,127-diff);
delay(10);


}