1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#define trigPin 5 //Trig
#define echoPin 4 //Echo
#define buzzer 3 //Buzzer sonore
int mindist = 30; //On indique la distance en dessous de laquelle nous souhaitons voir la led s'allumer
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 10); // TX-Pin11, RX-Pin10
void updateSerial()
{
delay(2000);
while (Serial.available()) {
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySerial.available()) {
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT); //On défini Trig comme une sortie
pinMode(echoPin, INPUT); //On défini Echo comme une entrée
pinMode(LED_BUILTIN, OUTPUT); //On défini la led comme une sortie
pinMode(buzzer,OUTPUT);//On défini le buzzer comme une sortie
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
long duree, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //Trig envois pendant 10ms
digitalWrite(trigPin, LOW);
// On calcul le temps pour l'aller retour du signal
duree = pulseIn(echoPin, HIGH);
distance = duree*340/(2*10000);
if (distance < mindist) { // On allume la led si on est moins loin que "mindist", mindist étant défini en début de programme
digitalWrite(LED_BUILTIN,HIGH);
digitalWrite(buzzer, LOW);
delay(100);
mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
mySerial.println("AT+CSQ"); //Signal quality test, value range is 0-31, 31 is the best
updateSerial();
mySerial.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
updateSerial();
mySerial.println("AT+CREG?"); //Check whether it has registered in the network
updateSerial();
mySerial.println("AT+SNFS=0"); //Adjust to earphone mode(AT+SNFS=1 is microphone mode)
updateSerial();
mySerial.println("AT+CRSL=2"); //Adjust volume, volume range is 0-15, maximum:15
updateSerial();
while(1)
{
if(mySerial.available())
{
Serial.write(mySerial.read()); //Forward what Software Serial received to Serial Port
if(Serial.available())
{
mySerial.write(Serial.read()); //Forward what Serial received to Software Serial Port
}
}
}
mySerial.println("AT"); // Once the handshake test is successful, it will back to OK
updateSerial();
mySerial.println("AT+CMGF=1"); // Configuring mode is TEST, only English texts are available
updateSerial();
mySerial.println("AT+CMGS=\"000000000\"");//xxxxxxxxxxx is the phone number
updateSerial();
mySerial.print("HELLO "); //text content
updateSerial();
mySerial.write(26);
while(1)
{
if(mySerial.available())
{
Serial.write(mySerial.read());//Data received by mySerial will be outputted by Serial
if(Serial.available())
{
mySerial.write(Serial.read());//Data received by Serial will be outputted by mySerial
}
}
}
}
else { //sinon on éteind la led
digitalWrite(buzzer,LOW);
digitalWrite(LED_BUILTIN,LOW);
}
delay(100);
} |
Partager