Skip to content

Instantly share code, notes, and snippets.

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 kostasprint/cb95bdf1abec52cb0106 to your computer and use it in GitHub Desktop.
Save kostasprint/cb95bdf1abec52cb0106 to your computer and use it in GitHub Desktop.
/* http://playground.arduino.cc/Main/MaxSonar
Maxbotix simple test
Instructions:
- At least one of: (comment the appropriate code below)
* PW is digital pin 8
* TX is digital pin 6
* AN is analog pin A0
- Change code below according to your model (LV, XL and
HRLV supported)
Note:
For convenience, the getRange method will always return centimeters.
You can use convert fuctions to convert to another unit (toInches and
toCentimeters are available)
*/
#include "Maxbotix.h"
#define DEBUG 1
#define VIBE 7 // attach Vibe motor to pin n°
#define VIBELENGHT 100 //
#define VIBEPAUSE 200 //
Maxbotix rangeSensorPW(8, Maxbotix::PW, Maxbotix::LV);
int distancePrevious;
void setup()
{
pinMode(VIBE, OUTPUT);
Serial.begin(9600);
}
void vibe1(int repeat)
{
// più è vicino più volte vibra
for(int i=0; i<repeat; i++){
#ifdef DEBUG
Serial.print("\t\t<<\t - VIBRA 1- \t>>");
#endif
digitalWrite(VIBE, HIGH); // turn the LED on (HIGH is the voltage level)
delay(VIBELENGHT); // vibe a moment
digitalWrite(VIBE, LOW); // turn the LED off by making the voltage LOW
delay(VIBEPAUSE); // wait a moment (for multiple vibes)
}
}
void vibe2(int frequence)
{
// vibra con impulsi diversi
#ifdef DEBUG
Serial.print("\t\t<<\t - VIBRA2 - \t>> freq. - ");
Serial.print(frequence);
#endif
digitalWrite(VIBE, HIGH); // turn the LED on (HIGH is the voltage level)
delay(VIBELENGHT); // vibe a moment
digitalWrite(VIBE, LOW); // turn the LED off by making the voltage LOW
delay(int(VIBEPAUSE/frequence)); // wait a moment (for multiple vibes)
}
void loop()
{
const int triggerFar = 120;
const int triggerBetween = 70;
const int triggerNear = 30;
int beep=0;
unsigned long start;
int distance;
// PW
start = millis();
distance = int(rangeSensorPW.getRange()); // Centimeters
#ifdef DEBUG
Serial.print("Reading...\t");
Serial.print("PW: ");
Serial.print(distance);
Serial.print(" - ");
Serial.print(distancePrevious);
Serial.print("cm - ");
Serial.print(millis() - start);
Serial.print("ms");
#endif
if (distance < triggerFar){
beep = 1;
if(distance < triggerBetween){
beep = 2;
if (distance < triggerNear){
beep = 3;
}
}
// vibra / suona / fai qualcosa di spettacolare: è il tuo momento !!!
vibe2(beep);
}
distancePrevious = distance;
#ifdef DEBUG
Serial.println(" ");
#endif
// delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment