Arduino Motors – Proximity Sensor Operated
11/19/2015

Special ‘Arduino Motor’ shield is designed to operate motors.
Depending on the speed of each motor, the projection would change its shape. The speed of the motors is controlled by a sensor – SR04 Ultrasonic sensor – or analog switches.
Code example for Arduino is provided below.
int val1 = 0; // value from the sensor for motor A
int val2 = 0; //value from the sensor for motor B
#include <NewPing.h>
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop(){
//defining sensor values for the motors
val1 = sonar.ping_cm();
val2 = sonar.ping_cm(); //mapping sensor values to motor speed
val1 = map(val1, 0, 100, 100, 200);
val2 = map(val2, 0, 100, 0, 255);
//Motor A
digitalWrite(12, HIGH); //Establishes forward direction
digitalWrite(9, LOW); //Disengage the Brake
analogWrite(3, val1); //Spins the motor at full speed
//Motor B
digitalWrite(13, HIGH); //Establishes backward direction
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, val2); //Spins the motor at half speed
delay(50); // Wait 50ms between pings Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
Serial.println(val1);
Serial.println(val2);
}