Anchal2-50 Merged Organized
Anchal2-50 Merged Organized
Anchal2-50 Merged Organized
APPENDIX A
//Libraries
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
//Variables
float oldacc;
float avgacc;
float newacc;
int trigPin1 = 4;
int echoPin1 = 3;
int trigPin2 = 7;
int echoPin2 = 6;
int trigPin3 = 9;
int echoPin3 = 8;
long duration1, duration2, duration3, duration4, cm1, cm2, cm3, cm4, inches1, inches2,
inches3, inches4;
int motor_speed;
int motor_speed1;
void setup(void) {
delay(10); // Will pause until serial console opens //Initializng the Motor Pins
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
Serial.println("MPU6050 test!");
// Try to initialize!
if (!mpu.begin()) {
while (1) {
delay(10); } }
Serial.println("MPU6050 Found!");
//Calibration
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break; \
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
Serial.println("");
delay(100);
void loop() {
sensors_event_t a, g, temp;
/* Serial.print("Acceleration X: ");
Serial.print(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);
Serial.println(" m/s^2"); */
//SMOOTHING PART (cycles 25 times, adds them to each other to take average) for (int
cycle = 0; cycle < 25; cycle++) {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.println(degree);
newacc = oldacc;
newacc = 0;
climbing();
climbing();
} else {
detection();
Serial.println("");
delay(10);
}
// Detecion function
void detection() {
// Set motors to 55
analogWrite(enA, 55);
analogWrite(enB, 55);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
pinMode(echoPin1, INPUT);
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
digitalWrite(trigPin3, LOW);
delayMicroseconds(5);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
digitalWrite(trigPin4, LOW);
delayMicroseconds(5);
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin4, LOW);
pinMode(echoPin4, INPUT);
Serial.print(inches2);
Serial.print("in2, ");
Serial.print(inches3);
Serial.print("in3, ");
Serial.print(inches4);
Serial.print("in4, ");
Serial.print(cm1);
Serial.print("cm1 ");
Serial.print(cm2);
Serial.print("cm2 ");
Serial.print(cm3);
Serial.print("cm3 ");
Serial.print(cm4);
Serial.print("cm4 ");
Serial.println();*/
Serial.print("SEARCHING");
{ Serial.print("Stairs found");
Serial.println("");
//Just to be sure that gyro is not zero and robot started to climbing the stairs
analogWrite(enA, 100);
analogWrite(enB, 100);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
void climbing() {
Serial.print("CLIMBING");
analogWrite(enA, 100);
analogWrite(enB, 100);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW); }