ARDUINO PID LINE FOLLOWER ROBOT part-1

circuit diagram

arduino code



#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
int a0 = A0;
int a1 = A1;
int a2 = A2;
int a3 = A3;
int a4 = 12;
int ENA=10;
int ENB=11;
int INA1=2;
int INA2=3;
int INB1=4;
int INB2=5;

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{
  Serial.begin(9600);
    pinMode(a0, INPUT);
   pinMode(a1, INPUT);
    pinMode(a2, INPUT);
     pinMode(a3, INPUT);
      pinMode(a4, INPUT);
      pinMode(ENA, OUTPUT);
       pinMode(ENB, OUTPUT);
        pinMode(INA1, OUTPUT);
         pinMode(INA2, OUTPUT);
         pinMode(INB1, OUTPUT);
         pinMode(INB2, OUTPUT);
       
  digitalWrite(ENA,HIGH);
  digitalWrite(ENB,HIGH);
  // initialize the OLED object
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  
  

  }
}

void loop()
{
  display.clearDisplay();
  int o4 = digitalRead(a0);
  int o3 = digitalRead(a1);
  int o2 = digitalRead(a2);
  int o1 = digitalRead(a3);
  int o0 = digitalRead(a4);

  if (o0==1)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(10, 10, 10, WHITE);
display.display();
}
  if (o1==1)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(35, 10, 10, WHITE);
display.display();

}
  if (o2==1)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(60, 10, 10, WHITE);
display.display();
}
  if (o3==1)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(85, 10, 10, WHITE);
display.display();

}
  if (o4==1)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(110, 10, 10, WHITE);
display.display();

}

 if (o0==0)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(10, 10, 10, BLACK);
display.display();

}
  if (o1==0)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(35, 10, 10, BLACK);
display.display();

}
  if (o2==0)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(60, 10, 10, BLACK);
display.display();

}
  if (o3==0)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(85, 10, 10, BLACK);
display.display();

}
  if (o4==0)
  {
display.setTextSize(1);
display.setTextColor(WHITE);
display.fillCircle(110, 10, 10, BLACK);
display.display();

}
if (o0==1&&o2==0&&o3==1)
{
  digitalWrite(INA1,HIGH);
  digitalWrite(INA2,LOW);
  digitalWrite(INB1,HIGH);
  digitalWrite(INB2,LOW);
}
if((o0==0&&o2==0&&o3==1)||(o0==0&&o2==1&&o3==1))
{
  digitalWrite(INA1,HIGH);
  digitalWrite(INA2,LOW);
  digitalWrite(INB1,LOW);
  digitalWrite(INB2,LOW);
}
if((o0==1&&o2==0&&o3==0)||(o0==1&&o2==1&&o3==0))
{
  digitalWrite(INA1,LOW);
  digitalWrite(INA2,LOW);
  digitalWrite(INB1,HIGH);
  digitalWrite(INB2,LOW);
}
if (o0==0&&o2==0&&o3==0)
{
  digitalWrite(INA1,LOW);
  digitalWrite(INA2,LOW);
  digitalWrite(INB1,LOW);
  digitalWrite(INB2,LOW);
}
}

Leave a comment