FastFashionDetektor

From SGMK-SSAM-WIKI
Revision as of 11:35, 9 April 2024 by Dusjagr (talk | contribs)
Jump to navigation Jump to search


Code for Seeed Xiao (SAMD21)

Download here:

// 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;

}