Skip to content

Instantly share code, notes, and snippets.

@gasp
Created March 26, 2016 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gasp/a5618c40d2320ddff0b6 to your computer and use it in GitHub Desktop.
Save gasp/a5618c40d2320ddff0b6 to your computer and use it in GitHub Desktop.
My first arduino program using HC SRO4, Adafruit Motor Shield 2.3 and an old RC car
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
// these constants won't change:
const int pinTrig = 13; // trigger sound with digital pin 13 (yellow)
const int pinEcho = 12; // green
const int pinLed = 10; // orange
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
const int safeDistance = 10; // min distance 10 cm
int hasBooted = 0;
// 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, M1
Adafruit_DCMotor *enigneMain = AFMS.getMotor(4);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
void setup() {
pinMode(pinTrig, OUTPUT); // ping
pinMode(pinEcho, INPUT); // pong
pinMode(pinLed, OUTPUT);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
enigneMain->setSpeed(225);
enigneMain->run(FORWARD);
// turn on motor
enigneMain->run(RELEASE);
Serial.begin(9600);
}
void loop() {
if(!hasBooted){
// advertize booting
shortBlink();
Serial.println("booted");
}
hasBooted = 1;
if (Serial.available() > 0) {
// read the incoming byte:
int incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
shortBlink();
}
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
digitalWrite(pinTrig, LOW);
delayMicroseconds(2);
digitalWrite(pinTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrig, LOW);
pinMode(pinEcho, INPUT);
duration = pulseIn(pinEcho, HIGH);
cm = microsecondsToCentimeters(duration);
if(isSafe(cm)) {
enigneMain->run(FORWARD);
}
else {
enigneMain->run(RELEASE);
Serial.print("proximity alert: ");
Serial.println(cm);
}
// warn if it isn't safe
warn(!isSafe(cm));
delay(threshold);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
// cm {long}
// returns a boolean
int isSafe(long cm) {
if(cm < safeDistance) {
return 0;
}
return 1;
}
// warn: light a red led
// @param {int} st status (use as a boolean)
void warn(int st) {
if(st == 1) {
digitalWrite(pinLed, HIGH);
}
else {
digitalWrite(pinLed, LOW);
}
}
// shortly blinks at lowintensity
void shortBlink() {
for(int i=0; i < 5; i++) {
// on at mid intensity
for(int j=0; j < 100; j++) {
digitalWrite(pinLed, HIGH);
delayMicroseconds(50);
digitalWrite(pinLed, LOW);
delayMicroseconds(500);
}
delay(100);
digitalWrite(pinLed, LOW);
delay(100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment