SENSING

I will be using 3 types of sensors for this project (Ultrasonic, IR digital, and IR analog). The ultrasonic sensor will be used in the front to detect an upcoming wall and its distance away from that wall. The digital IR sensor will determine if there is a gap in the maze coming up and will be placed on the front right and left of the robot on an angle. The last sensor, analog IR, will be used on the left and right side of the bot and will keep the robot in line so it can travel in a straight line.

 

Ultrasonic — the ultrasonic sensor has 2 pins (echo and trig). The trig sends a signal and the echo receives that signal and uses the time in between to determine how far away it is from an object (in this case, an upcoming wall)

sample code, basic functionality for ultrasonic (taken from Ultrasonic Sensor and Arduino Tutorial)

// defines pins numbers

const int trigPin = 9;

const int echoPin = 10;

 

// defines variables

long duration;

int distance;

 

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

}

 

void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

 

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

 

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

 

// Calculating the distance .034 resembles speed of sound… the “/2” is because the distance covers there and back and we just need one way //(half)

distance= duration*0.034/2

 

// Prints the distance on the Serial Monitor

Serial.print(“Distance: “);

Serial.println(distance);

}

 

 

IR

Digital

ANALOG SENSOR (taken from Arduino Project Hub for Analog Sensors)

int sensorpin = 0;                 // analog pin used to connect the sharp sensor

int val = 0;                 // variable to store the values from sensor(initially zero)

 

void setup()

{

Serial.begin(9600);               // starts the serial monitor

}

 

void loop()

{

val = analogRead(sensorpin);       // reads the value of the sharp sensor

Serial.println(val);            // prints the value of the sensor to the serial monitor

delay(400);                    // wait for this much time before printing next value

}