Our first project: A car of our own (Part 4 obstacle avoidance and temporary end)

First thing:┬áThis project is going to have a temporary end. The reason is because this is the final stage of the ‘basics’. We’ve already covered digitalWrite, serial communication and a few other things. This time we’ll be going in to digitalRead and using an LCD screen.

Will we return to this later? Probably, but for now at the end of this part we’ll have a car that can be controlled without fear of driving into stuff.

But again, here’s how I finished up:

Parts:

  • The ‘tank’ previously made
  • LCD screen
  • Two IR distance sensors

Assembly:

Starting to get a little confusing… So here’s the new additions:

  • Right IR sensor GND -> GND, VCC -> 5v, OUT -> D8
  • Left IR sensor GND -> GND, VCC -> 5v, OUT -> D7
  • LCD screen (with IIC) GND -> GND, VCC -> 5v, SDA -> SDA, SCL -> SCL

I’m particularly leaving the LCD’s SDA and SCL as that because those pins are sometimes different between different Arduinos so it is better to make sure with which one you have.

Also: Reminder about uploading code while the Bluetooth module is plugged in. You might encounter problems, so you might need to disconnect it while uploading code.

Code:

Anyway, here’s some Arduino code!

#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>
byte serialA;
int RF = 11;
int RR = 10;
int LF = 9;
int LR = 6;
int IRRight = 8;
int IRLeft = 7;
int valRight = 0;
int valLeft = 0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address, make sure 0x27 is your LCD's I2C address
void setup()
{
Serial.begin(9600);
pinMode(RF, OUTPUT);
pinMode(RR, OUTPUT);
pinMode(LF, OUTPUT);
pinMode(LR, OUTPUT);
pinMode(IRRight, INPUT);
pinMode(IRLeft, INPUT);
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
for(int i = 0; i< 1; i++)
{
lcd.backlight();
delay(50);
lcd.noBacklight();
delay(50);
}
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Let's start");
}
void loop(){
if (Serial.available() > 0) {
serialA = Serial.read();
Serial.println(serialA);
lcd.clear();
printAll(serialA);
Serial.println("top");
changeMotors(serialA);
delay(100);
while((Serial.available()>0)){
serialA=Serial.read();
printAll(serialA);
Serial.println("Bottom");
}
delay(100);
}
valRight = digitalRead(IRRight);
valLeft = digitalRead(IRLeft);
//When the IR sensors sense nothing, they return 1, so when they sense something they return 0. If their sum is less than 2 they sense something and must stop.
if (valRight + valLeft < 2) {
changeMotors(48);
delay(1000);
}
}
void printAll(byte b){
lcd.write(b);
Serial.print("Received the following: ");
Serial.println(b);
Serial.print("Serial.available is... ");
Serial.println(Serial.available());
}
void changeMotors(byte a){
printAll(a);
switch (a) {
case 48: //Stop on text 0 through serial
digitalWrite(LF, LOW);
digitalWrite(RF, LOW);
digitalWrite(LR, LOW);
digitalWrite(RR, LOW);
break;
case 49: //Forward on text 1 through serial
digitalWrite(RF, HIGH);
digitalWrite(LF, HIGH);
digitalWrite(LR, LOW);
digitalWrite(RR, LOW);
break;
case 50: //LEFT on text 2 through serial
digitalWrite(LF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RF, HIGH);
digitalWrite(RR, LOW);
break;
case 51: //RIGHT on text 3 through serial
digitalWrite(LF, HIGH);
digitalWrite(RF, LOW);
digitalWrite(LR, LOW);
digitalWrite(RR, HIGH);
break;
case 52: //Reverse on text 4 through serial
digitalWrite(LF, LOW);
digitalWrite(RF, LOW);
digitalWrite(LR, HIGH);
digitalWrite(RR, HIGH);
break;
}
}

Instead of multiple if statements for each IR sensor both are dealt with at once because the same thing is going to be done for both for now. “If you see something. Stop.”

You might notice the code is kinda neatened up as well. If you can read it and understand it you win a prize! That prize is the gift of knowledge.

So, hopefully this particular project has been interesting. Next up will be another interesting project. Which one will it be? I dunno, whichever one I can be bothered documenting. I have one that has scorch marks on it, another that is half finished and OH this one has food on it, yum!

Just remember when you build this project or when you’re about to build your next project.

Why? Because we can.

Leave a comment

Your email address will not be published. Required fields are marked *