Arduino Uno Sketches


Arduino sketch (line follower) and Edge detection

// This is the basic code which runs a loop that makes the 
//Robot follow a line and avoid falling off an edge using 3 IR sensors.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M4
Adafruit_DCMotor *leftMotor = AFMS.getMotor(2);
//You can also make another motor on port M1
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);

int analogPin3 = 3;     // Infrared Sensor right
int analogPin2 = 2;    //IR sensor middle
int analogPin1 = 1;    //IR sensor left
                               
int val1 = 0;           // variable to store the value read
int val2 = 0;
int val3 = 0;


//------------------------------------------------------------------------------------------------

void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motors are working");
  delay(2000);

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  

}

// The following Loop is to make the Robot go straight when the Infrared value is below 100

void loop()

{
   val1 = analogRead(analogPin1);    // read the input pins
   val2 = analogRead(analogPin2);
   val3 = analogRead(analogPin3);
 Serial.print("Left Sensor :");
  Serial.print(val1);    // debug value
  Serial.print("\t");
 Serial.print("Center Sensor  :");
  Serial.print(val2); 
  Serial.print("\t");
  Serial.print("right Sensor  ");
  Serial.println(val3); 
  
   
if (val1 >100 && val2 >100 && val3 <100) //condition: 1|1|0
 {
  leftMotor->setSpeed(110);
  rightMotor->setSpeed(110);
    leftMotor->run(BACKWARD);
   rightMotor->run(FORWARD);
 }
 else if (val1 <100 && val2 >100 && val3 >100)//condition: 0|1|1
 {
     leftMotor->setSpeed(110);
  rightMotor->setSpeed(110);
    leftMotor->run(FORWARD);
   rightMotor->run(BACKWARD);
 }
 if (val1 >90 && val2 <100 && val3 <90) //condition: 1|0|0
 {
    leftMotor->setSpeed(110);
  rightMotor->setSpeed(110);
     leftMotor->run(BACKWARD);
   rightMotor->run(FORWARD);
 }
 else if (val1 <100 && val2 <100 && val3 >100)//condition: 0|0|1
 {
   leftMotor->setSpeed(110);
  rightMotor->setSpeed(110);
     leftMotor->run(FORWARD);
   rightMotor->run(BACKWARD);
 }
 else if (val1 <100 && val2 >100 && val3 <100)//condition: 0|1|0

  {
      leftMotor->setSpeed(150);
  rightMotor->setSpeed(150);
   leftMotor->run(FORWARD);
   rightMotor->run(FORWARD);
  }
  
  else if (val1 <100 && val2 <100 && val3 <100)//condition 0|0|0
     {
    leftMotor->setSpeed(110);
  rightMotor->setSpeed(110);
     leftMotor->run(FORWARD);
   rightMotor->run(FORWARD);
     }
  //If the robot approaches an overhang reverse for 1 sec and turn right
  else if (val1 >950 && val2 >950 && val3 >950)

{
   leftMotor->run(BACKWARD);
   rightMotor->run(BACKWARD);
   delay(1500);
  leftMotor->run(FORWARD);
   rightMotor->run(BACKWARD);
   delay(1000);
    leftMotor->run(RELEASE);
   rightMotor->run(RELEASE);

  }

}



Bumper

//This is a basic code that detects 
//when the bot has hit a wall and reverses and changes direction.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 


// Select motor ports M1 and M2
Adafruit_DCMotor *leftMotor = AFMS.getMotor(2);
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);

// Assign bumbers to pins
int bumper1 = 10; //micro switch left
int bumper2 = 6;//micro switch right


void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motors are working");
  delay(2000);

  AFMS.begin();  // create with the default frequency 1.6KHz

 pinMode(bumper1, INPUT_PULLUP); 
 pinMode(bumper2, INPUT_PULLUP); 
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  leftMotor->setSpeed(100);
  rightMotor->setSpeed(100);
  
}
void loop()
{
 //read the motor switch values into a variable  
 int sensorVal1 = digitalRead(bumper1);
 int sensorVal2 = digitalRead(bumper2);

 //Print the values to the serial monitor
 Serial.print(sensorVal1);
 Serial.println(sensorVal2);
 delay(500);


 if (sensorVal1 ==HIGH && sensorVal2 == HIGH)
  {
   leftMotor->run(FORWARD);
   rightMotor->run(FORWARD);
   Serial.print(sensorVal1);
 Serial.println(sensorVal2);

  }
 else if (sensorVal1 == LOW || sensorVal2 == LOW)
    {
   leftMotor->run(BACKWARD);
   rightMotor->run(BACKWARD);
   delay(1000);
   leftMotor->run(FORWARD);
   rightMotor->run(BACKWARD);
   delay(1000);
   leftMotor->run(FORWARD);
   rightMotor->run(FORWARD);
   
  } 
  
}
Maze Solver


//Maze solver
// This is the basic code which solves a line maze with NO loops in it.
// the adafruit motorshield libraries are used.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();


// Select motor ports M4 and M1
Adafruit_DCMotor *leftMotor = AFMS.getMotor(2);
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);

//include infrared sensors
int analogPin3 = 3;     // Infrared Sensor right
int analogPin2 = 2;    //IR sensor middle
int analogPin1 = 1;    //IR sensor left
                             
int val1 = 0;           // variable to store the value read
int val2 = 0;
int val3 = 0;

//------------------------------------------------------------------------------------------------

void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motors are working");
  delay(2000);

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz


}

// The following Loop is to make the Robot go staright when the Infrared value is below 100

void loop()

{
  val1 = analogRead(analogPin1);    // read the input pins
   val2 = analogRead(analogPin2);
   val3 = analogRead(analogPin3);
 Serial.print("Left Sensor :");
  Serial.print(val1);    // debug value
  Serial.print("\t");
 Serial.print("Center Sensor  :");
  Serial.print(val2);
  Serial.print("\t");
  Serial.print("right Sensor  ");
  Serial.println(val3);          

if (val1 <100 && val2 >100 && val3 <100)// condition: 0|1|0 go STRAIGHT
  {
   leftMotor->setSpeed(100);
   rightMotor->setSpeed(100);
   leftMotor->run(FORWARD);
   rightMotor->run(FORWARD);
  }
 
else if (val1 >100 && val2 >100 && val3 <100)//condition: 1|1|0 turn LEFT
 {
   leftMotor->setSpeed(90);
   rightMotor->setSpeed(90);
   leftMotor->run(FORWARD);
   rightMotor->run(BACKWARD);
 }
 else if (val1 <100 && val2 >100 && val3 >100)//condition: 0|1|1 go STRAIGHT
 {
   leftMotor->setSpeed(100);
   rightMotor->setSpeed(100);
   leftMotor->run(FORWARD);
   rightMotor->run(FORWARD);
 }
 else if (val1 <90 && val2 <100 && val3 >90)//condition: 0|0|1 turn RIGHT
 {
   leftMotor->setSpeed(100);
   rightMotor->setSpeed(100);
   leftMotor->run(FORWARD);
   rightMotor->run(BACKWARD);
 }
 else if (val1 >90 && val2 <100 && val3 <90)//condition: 1|0|0 turn LEFT
 {
   leftMotor->setSpeed(100);
   rightMotor->setSpeed(100);
   leftMotor->run(BACKWARD);
   rightMotor->run(FORWARD);
 }
 else if (val1 <100 && val2 <100 && val3 <100)//condition: 0|0|0 TURN AROUND
 {
   leftMotor->setSpeed(100);
   rightMotor->setSpeed(100);
   leftMotor->run(BACKWARD);
   rightMotor->run(FORWARD);
 }
 else if (val1 >100 && val2 >100 && val3 >100)//condition: 1|1|1 turn LEFT
 {
  leftMotor->setSpeed(100);
   rightMotor->setSpeed(100);
   leftMotor->run(FORWARD);
   rightMotor->run(BACKWARD);
 }

}

 
This is the Photosensor Sketch

// LDR to Pin A0
//1 Kilo ohm resistor
//The built-in Arduino code "ColorMixingLamp" was used to write this code.


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select selecting the motors
Adafruit_DCMotor *leftMotor = AFMS.getMotor(2);
//You can also make another motor on port M2
Adafruit_DCMotor *rightMotor = AFMS.getMotor(3);


const int LDR = A0;  
int val1 = 0;           // variable to store the value read

void setup() {
 Serial.begin(9600);
 Serial.println("Motors are working");
  delay(2000);

  AFMS.begin();  

}

void loop() {
val1 = analogRead(LDR);
Serial.print("raw LDR Value  " );
Serial.print(LDR);

Serial.print("Mapped LDR Value  ");
Serial.println(val1);

//Photosensor reading the ambient light should be below 200
if (val1 < 200)
{
  leftMotor->setSpeed(100);
  rightMotor->setSpeed(100);
    leftMotor->run(FORWARD);
   rightMotor->run(FORWARD);
 }
//If the photosensor reads a large increase in light the motors are shut off
 else if (val1 > 250)
{
  leftMotor->setSpeed(70);
  rightMotor->setSpeed(70);
    leftMotor->run(RELEASE);
   rightMotor->run(RELEASE);
 }

}