Skip to main content
Code formatting.
Source Link
Edgar Bonet
  • 44.3k
  • 4
  • 41
  • 79

#define TRIG A0 //Module pins
#define ECHO A1 
#define TRIG1 A2 //Module pins
#define ECHO1 A3 
#define Buzzerpin 13

int ctn = 0;
    

void setup() { 
  
  Serial.begin(9600); // Serial monitoring 
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(ECHO, INPUT_PULLUP);
  pinMode(TRIG1, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(ECHO1, INPUT_PULLUP);
  pinMode(Buzzerpin, OUTPUT);
  } 
  
  void loop() { 

  digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 
  delayMicroseconds(2); 

  digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging according to specs 
  delayMicroseconds(20);
    
  digitalWrite(TRIG, LOW); // Send pin low again
  delayMicroseconds(2); 

  digitalWrite(TRIG1, LOW); // Set the trigger pin to low for 2uS. Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  delayMicroseconds(2); 
    
  digitalWrite(TRIG1, HIGH); // Send a 10uS high to trigger ranging according to specs 
  delayMicroseconds(20); 
    
    
  digitalWrite(TRIG1, LOW); // Send pin low again  
  delayMicroseconds(2); 

int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 

int distance1 = pulseIn(ECHO1, HIGH,26000); // Read in times pulse 
  
    
distance= distance/58; 

distance1= distance1/58; 

if (distance < 25) {
    Serial.print("\r");
    delay(1000);
    Serial.print("AT+CMGF=1\r");
    delay(1000);
    /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
    Serial.print("AT+CMGS=\"+YYXXXX\"\r"); // YY is the country code XXX is the number
    delay(1000); // 
    //The text of the message to be sent.
    Serial.print("HELLO There");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);

}

else{}


if (distance1 < 25 && ctn < 150) { 
ctn += 1;
digitalWrite(Buzzerpin, HIGH);
}

  else{
    digitalWrite(Buzzerpin, LOW);
}

if (distance1 > 25 && ctn >= 150) { 
ctn = 0;
digitalWrite(Buzzerpin, HIGH);
}

    
}```



  
#define TRIG A0 //Module pins
#define ECHO A1
#define TRIG1 A2 //Module pins
#define ECHO1 A3
#define Buzzerpin 13

int ctn = 0;

void setup() {
  Serial.begin(9600); // Serial monitoring
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input
  pinMode(ECHO, INPUT_PULLUP);
  pinMode(TRIG1, OUTPUT); // Initializing Trigger Output and Echo Input
  pinMode(ECHO1, INPUT_PULLUP);
  pinMode(Buzzerpin, OUTPUT);
}

void loop() {

  digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS
  delayMicroseconds(2);

  digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging according to specs
  delayMicroseconds(20);

  digitalWrite(TRIG, LOW); // Send pin low again
  delayMicroseconds(2);

  digitalWrite(TRIG1, LOW); // Set the trigger pin to low for 2uS. Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  delayMicroseconds(2);

  digitalWrite(TRIG1, HIGH); // Send a 10uS high to trigger ranging according to specs
  delayMicroseconds(20);

  digitalWrite(TRIG1, LOW); // Send pin low again
  delayMicroseconds(2);

  int distance = pulseIn(ECHO, HIGH, 26000); // Read in times pulse
  int distance1 = pulseIn(ECHO1, HIGH, 26000); // Read in times pulse
  distance = distance/58;
  distance1 = distance1/58;

  if (distance < 25) {
    Serial.print("\r");
    delay(1000);
    Serial.print("AT+CMGF=1\r");
    delay(1000);
    /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
    Serial.print("AT+CMGS=\"+YYXXXX\"\r"); // YY is the country code XXX is the number
    delay(1000); //
    //The text of the message to be sent.
    Serial.print("HELLO There");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);
  }
  else {}

  if (distance1 < 25 && ctn < 150) {
    ctn += 1;
    digitalWrite(Buzzerpin, HIGH);
  }
  else {
    digitalWrite(Buzzerpin, LOW);
  }

  if (distance1 > 25 && ctn >= 150) {
    ctn = 0;
    digitalWrite(Buzzerpin, HIGH);
  }
}

#define TRIG A0 //Module pins
#define ECHO A1 
#define TRIG1 A2 //Module pins
#define ECHO1 A3 
#define Buzzerpin 13

int ctn = 0;
    

void setup() { 
  
  Serial.begin(9600); // Serial monitoring 
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(ECHO, INPUT_PULLUP);
  pinMode(TRIG1, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(ECHO1, INPUT_PULLUP);
  pinMode(Buzzerpin, OUTPUT);
  } 
  
  void loop() { 

  digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 
  delayMicroseconds(2); 

  digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging according to specs 
  delayMicroseconds(20);
    
  digitalWrite(TRIG, LOW); // Send pin low again
  delayMicroseconds(2); 

  digitalWrite(TRIG1, LOW); // Set the trigger pin to low for 2uS. Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  delayMicroseconds(2); 
    
  digitalWrite(TRIG1, HIGH); // Send a 10uS high to trigger ranging according to specs 
  delayMicroseconds(20); 
    
    
  digitalWrite(TRIG1, LOW); // Send pin low again  
  delayMicroseconds(2); 

int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 

int distance1 = pulseIn(ECHO1, HIGH,26000); // Read in times pulse 
  
    
distance= distance/58; 

distance1= distance1/58; 

if (distance < 25) {
    Serial.print("\r");
    delay(1000);
    Serial.print("AT+CMGF=1\r");
    delay(1000);
    /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
    Serial.print("AT+CMGS=\"+YYXXXX\"\r"); // YY is the country code XXX is the number
    delay(1000); // 
    //The text of the message to be sent.
    Serial.print("HELLO There");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);

}

else{}


if (distance1 < 25 && ctn < 150) { 
ctn += 1;
digitalWrite(Buzzerpin, HIGH);
}

  else{
    digitalWrite(Buzzerpin, LOW);
}

if (distance1 > 25 && ctn >= 150) { 
ctn = 0;
digitalWrite(Buzzerpin, HIGH);
}

    
}```



  
#define TRIG A0 //Module pins
#define ECHO A1
#define TRIG1 A2 //Module pins
#define ECHO1 A3
#define Buzzerpin 13

int ctn = 0;

void setup() {
  Serial.begin(9600); // Serial monitoring
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input
  pinMode(ECHO, INPUT_PULLUP);
  pinMode(TRIG1, OUTPUT); // Initializing Trigger Output and Echo Input
  pinMode(ECHO1, INPUT_PULLUP);
  pinMode(Buzzerpin, OUTPUT);
}

void loop() {

  digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS
  delayMicroseconds(2);

  digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging according to specs
  delayMicroseconds(20);

  digitalWrite(TRIG, LOW); // Send pin low again
  delayMicroseconds(2);

  digitalWrite(TRIG1, LOW); // Set the trigger pin to low for 2uS. Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  delayMicroseconds(2);

  digitalWrite(TRIG1, HIGH); // Send a 10uS high to trigger ranging according to specs
  delayMicroseconds(20);

  digitalWrite(TRIG1, LOW); // Send pin low again
  delayMicroseconds(2);

  int distance = pulseIn(ECHO, HIGH, 26000); // Read in times pulse
  int distance1 = pulseIn(ECHO1, HIGH, 26000); // Read in times pulse
  distance = distance/58;
  distance1 = distance1/58;

  if (distance < 25) {
    Serial.print("\r");
    delay(1000);
    Serial.print("AT+CMGF=1\r");
    delay(1000);
    /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
    Serial.print("AT+CMGS=\"+YYXXXX\"\r"); // YY is the country code XXX is the number
    delay(1000); //
    //The text of the message to be sent.
    Serial.print("HELLO There");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);
  }
  else {}

  if (distance1 < 25 && ctn < 150) {
    ctn += 1;
    digitalWrite(Buzzerpin, HIGH);
  }
  else {
    digitalWrite(Buzzerpin, LOW);
  }

  if (distance1 > 25 && ctn >= 150) {
    ctn = 0;
    digitalWrite(Buzzerpin, HIGH);
  }
}
Source Link
Zac
  • 11
  • 2

Running Arduino with 2 outputs and 2 inputs

Totally new to Arduino, 3 weeks old!

I am doing a project with 2 inputs (2 x ultrasonic sensors) and 2 outputs (buzzer and send SMS)

The code to run the buzzer and sms works perfectly individually. For the buzzer, it will stop buzzing after approx 5 seconds.

I would love them to run concurrently but it didn't work as planned when I upload the code on the board.

I've read on forums to use millis / blinkwithoutdelay but am unsure how I can incorporate those functions on my mode as the examples posted online are different from my project.

The following is the code I have created.

Would appreciate if I can get some help from the experienced folks here.


#define TRIG A0 //Module pins
#define ECHO A1 
#define TRIG1 A2 //Module pins
#define ECHO1 A3 
#define Buzzerpin 13

int ctn = 0;
    

void setup() { 
  
  Serial.begin(9600); // Serial monitoring 
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(ECHO, INPUT_PULLUP);
  pinMode(TRIG1, OUTPUT); // Initializing Trigger Output and Echo Input 
  pinMode(ECHO1, INPUT_PULLUP);
  pinMode(Buzzerpin, OUTPUT);
  } 
  
  void loop() { 

  digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 
  delayMicroseconds(2); 

  digitalWrite(TRIG, HIGH); // Send a 10uS high to trigger ranging according to specs 
  delayMicroseconds(20);
    
  digitalWrite(TRIG, LOW); // Send pin low again
  delayMicroseconds(2); 

  digitalWrite(TRIG1, LOW); // Set the trigger pin to low for 2uS. Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  delayMicroseconds(2); 
    
  digitalWrite(TRIG1, HIGH); // Send a 10uS high to trigger ranging according to specs 
  delayMicroseconds(20); 
    
    
  digitalWrite(TRIG1, LOW); // Send pin low again  
  delayMicroseconds(2); 

int distance = pulseIn(ECHO, HIGH,26000); // Read in times pulse 

int distance1 = pulseIn(ECHO1, HIGH,26000); // Read in times pulse 
  
    
distance= distance/58; 

distance1= distance1/58; 

if (distance < 25) {
    Serial.print("\r");
    delay(1000);
    Serial.print("AT+CMGF=1\r");
    delay(1000);
    /*Replace XXXXXXXXXX to 10 digit mobile number & ZZ to 2 digit country code*/
    Serial.print("AT+CMGS=\"+YYXXXX\"\r"); // YY is the country code XXX is the number
    delay(1000); // 
    //The text of the message to be sent.
    Serial.print("HELLO There");
    delay(1000);
    Serial.write(0x1A);
    delay(1000);

}

else{}


if (distance1 < 25 && ctn < 150) { 
ctn += 1;
digitalWrite(Buzzerpin, HIGH);
}

  else{
    digitalWrite(Buzzerpin, LOW);
}

if (distance1 > 25 && ctn >= 150) { 
ctn = 0;
digitalWrite(Buzzerpin, HIGH);
}

    
}```