FastFashionDetektor: Difference between revisions
Jump to navigation
Jump to search
(Created page with " <code> →Ping))) Sensor for Fast Fashion Detektorology https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping: // this constant won't change. It's the pin number of the sensor's output: const int pingPin = 7; const int buzzerPin = 10; const int lightPin = 6; const int ledPin = 3; const int senseTime = 100; const int senseDist = 50; int sensDur1 = 0; int toneDur1 = 0; int sensDur2 = 0; int toneDur2 = 0; int sensDur3 = 0; int toneDur3 = 0; long duration, inch...") |
No edit summary |
||
| Line 1: | Line 1: | ||
<code> | <code> | ||
/* | /* | ||
Ping))) Sensor for Fast Fashion Detektorology | Ping))) Sensor for Fast Fashion Detektorology | ||
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping | https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping | ||
*/ | */ | ||
// this constant won't change. It's the pin number of the sensor's output: | // this constant won't change. It's the pin number of the sensor's output: | ||
const int pingPin = 7; | const int pingPin = 7; | ||
| Line 15: | Line 12: | ||
const int senseTime = 100; | const int senseTime = 100; | ||
const int senseDist = 50; | const int senseDist = 50; | ||
int sensDur1 = 0; | int sensDur1 = 0; | ||
int toneDur1 = 0; | int toneDur1 = 0; | ||
| Line 22: | Line 18: | ||
int sensDur3 = 0; | int sensDur3 = 0; | ||
int toneDur3 = 0; | int toneDur3 = 0; | ||
long duration, inches, cm, lastcm; | long duration, inches, cm, lastcm; | ||
| Line 36: | Line 31: | ||
void loop() { | void loop() { | ||
sens(); | sens(); | ||
if (cm <= senseDist+1 && lastcm >= senseDist+2) { | if (cm <= senseDist+1 && lastcm >= senseDist+2) { | ||
tone(buzzerPin, 800, 100); | tone(buzzerPin, 800, 100); | ||
| Line 46: | Line 40: | ||
noTone(buzzerPin); | noTone(buzzerPin); | ||
} | } | ||
if (cm <= senseDist) { | if (cm <= senseDist) { | ||
digitalWrite(ledPin,HIGH); | digitalWrite(ledPin,HIGH); | ||
| Line 86: | Line 79: | ||
delayMicroseconds(5); | delayMicroseconds(5); | ||
digitalWrite(pingPin, LOW); | digitalWrite(pingPin, LOW); | ||
// The same pin is used to read the signal from the PING))): a HIGH pulse | // The same pin is used to read the signal from the PING))): a HIGH pulse | ||
// whose duration is the time (in microseconds) from the sending of the ping | // whose duration is the time (in microseconds) from the sending of the ping | ||
| Line 92: | Line 84: | ||
pinMode(pingPin, INPUT); | pinMode(pingPin, INPUT); | ||
duration = pulseIn(pingPin, HIGH); | duration = pulseIn(pingPin, HIGH); | ||
cm = microsecondsToCentimeters(duration); | cm = microsecondsToCentimeters(duration); | ||
} | } | ||
| Line 156: | Line 147: | ||
return microseconds / 29 / 2; | return microseconds / 29 / 2; | ||
} | } | ||
</code> | </code> | ||
Revision as of 11:33, 9 April 2024
/*
Ping))) Sensor for Fast Fashion Detektorology
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Ping
- /
// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;
const int buzzerPin = 10;
const int lightPin = 6;
const int ledPin = 3;
const int senseTime = 100;
const int senseDist = 50;
int sensDur1 = 0;
int toneDur1 = 0;
int sensDur2 = 0;
int toneDur2 = 0;
int sensDur3 = 0;
int toneDur3 = 0;
long duration, inches, cm, lastcm;
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(lightPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
detect3();
}
void loop() {
sens();
if (cm <= senseDist+1 && lastcm >= senseDist+2) {
tone(buzzerPin, 800, 100);
detect1();
detect2();
detect3();
}
else {
noTone(buzzerPin);
}
if (cm <= senseDist) {
digitalWrite(ledPin,HIGH);
digitalWrite(lightPin,LOW);
//delay(10);
//digitalWrite(lightPin,LOW);
tone(buzzerPin, sensDur1>>2, toneDur1);
delay(sensDur1>>4);
digitalWrite(lightPin,HIGH);
tone(buzzerPin, sensDur2, toneDur2);
delay(sensDur1>>5);
tone(buzzerPin, sensDur3>>2, toneDur3);
delay(toneDur3>>4);
digitalWrite(lightPin,LOW);
}
else {
digitalWrite(ledPin,LOW);
digitalWrite(lightPin,HIGH);
noTone(buzzerPin);
}
/*
Serial.print(cm);
Serial.print("cm ");
Serial.print(lastcm);
Serial.print("cm");
Serial.println();
*/
delay(50);
lastcm = microsecondsToCentimeters(duration);
}
void sens(){
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the ping
// to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
}
void detect1() {
digitalWrite(lightPin,LOW);
digitalWrite(ledPin,HIGH);
tone(buzzerPin, 300, 100);
delay(senseTime);
digitalWrite(ledPin,LOW);
delay(senseTime/2);
digitalWrite(lightPin,HIGH);
sens();
toneDur1 = duration;
digitalWrite(ledPin,HIGH);
tone(buzzerPin, 300, 100);
delay(senseTime);
sens();
sensDur1 = duration;
digitalWrite(ledPin,LOW);
delay(senseTime/2);
}
void detect2() {
digitalWrite(lightPin,HIGH);
sens();
toneDur2 = duration;
digitalWrite(ledPin,HIGH);
tone(buzzerPin, 300, 100);
delay(senseTime);
sens();
sensDur2 = duration;
digitalWrite(ledPin,LOW);
delay(senseTime/2);
}
void detect3() {
digitalWrite(lightPin,HIGH);
sens();
toneDur3 = duration;
digitalWrite(ledPin,HIGH);
tone(buzzerPin, 600, 300);
delay(senseTime);
sens();
sensDur3 = duration;
digitalWrite(ledPin,LOW);
delay(senseTime/2);
}
long microsecondsToInches(long microseconds) {
// According to Parallax's datasheet for the PING))), there are 73.746
// microseconds per inch (i.e. sound travels at 1130 feet per second).
// This gives the distance travelled by the ping, outbound and return,
// so we divide by 2 to get the distance of the obstacle.
// See: https://www.parallax.com/package/ping-ultrasonic-distance-sensor-downloads/
return microseconds / 74 / 2;
}
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;
}