#include<Wire.h>
long accelx,accely,accelz; // define accelerometer variables
float gforcex,gforcey,gforcez;
int irsensorbackward=A0;
// same for backward direction
int pin13=12; // pin13,pin12,pin11,pin10 these pins are the data pin of ht12e ic
int pin12=10;
int pin11=8;
int pin10=7;
int value; // this takes the analog reading of irsensorbackward
void setup()
{
Serial.begin(9600);
Wire.begin(); // initialising
pinMode(irsensorbackward,INPUT);
pinMode(pin13,OUTPUT);
pinMode(pin12,OUTPUT);
pinMode(pin11,OUTPUT);
pinMode(pin10,OUTPUT);
setupmpu(); // call the function
}
void loop()
{
value=analogRead(irsensorbackward);
accelregistervalue();
Serial.println(value);
// reading from gforcey variable only
if(value > 255)
{
if(gforcey>-0.39 || gforcey<0.39)
{
digitalWrite(pin13,LOW); // the car will move in the forward direction
digitalWrite(pin12,HIGH);
digitalWrite(pin11,LOW);
digitalWrite(pin10,HIGH);
}
if(gforcey<-0.40) // if the condition will true then the car will move in the left side
{
digitalWrite(pin13,LOW);
digitalWrite(pin12,HIGH);
digitalWrite(pin11,HIGH);
digitalWrite(pin10,HIGH);
}
if(gforcey>0.40) // if this condition will true then the car will turn right
{
digitalWrite(pin13,HIGH);
digitalWrite(pin12,HIGH);
digitalWrite(pin11,LOW);
digitalWrite(pin10,HIGH);
}
}
if(value<255) // all motors are off mode
{
digitalWrite(pin13,HIGH);
digitalWrite(pin12,HIGH);
digitalWrite(pin11,HIGH);
digitalWrite(pin10,HIGH);
}
}
void setupmpu()
{
Wire.beginTransmission(0b1101000); // send slave address
Wire.write(0x6B); // 6B regidter which i want to access
Wire.write(0b00000000); // set all bits of these register are 0
Wire.endTransmission();
// accelerometer configuration
Wire.beginTransmission(0b1101000);
Wire.write(0x1c);
Wire.write(0b00000000);
Wire.endTransmission();
}
void accelregistervalue()
{
Wire.beginTransmission(0b1101000);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while(Wire.available()<6);
accelx=Wire.read()<<8| Wire.read();
accely=Wire.read()<<8 | Wire.read();
accelz=Wire.read()<<8 | Wire.read();
gforcex=accelx/16384.0;
gforcey=accely/16384.0;
gforcez=accelz/16384.0;
}
Comments
Please log in or sign up to comment.