Programa Sensor de Aproximação FITEC

#include <NewTone.h>
#include <NewPing.h>


#define TRIGGER_PIN  31  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     33  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.


float seno;
float cmMsec;
int frequencia;
int buzzer = 53; //porta buzzer
int ledR = 41;   //porta led Vermelho
int ledG = 45;   //porta led Verde
int ledY = 43;   //porta led Amarelo
//(3,4,5,6,7,8,9,10) portas do display na ordem de baixo direita para a esquerda, e de cima esquerda para direita

void setup()
{
  Serial.begin(9600);
  Serial.println("Lendo dados do sensor...");
  pinMode(buzzer, OUTPUT);
  pinMode(ledR, OUTPUT);
  pinMode(ledY, OUTPUT);
  pinMode(ledG, OUTPUT);

  //Display

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  // realisa teste inicial
 
  digitalWrite(3, HIGH);delay(100);
  digitalWrite(4, HIGH);delay(100);
  digitalWrite(5, HIGH);delay(100);
  digitalWrite(7, HIGH);delay(100);
  digitalWrite(8, HIGH);delay(100);
  digitalWrite(9, HIGH);delay(100);
  digitalWrite(10, HIGH);delay(100);
  digitalWrite(ledR, HIGH);delay(100);
  digitalWrite(ledY, HIGH);delay(100);
  digitalWrite(ledG, HIGH);delay(100);
  NewTone(buzzer,1000);delay(200);
  noNewTone(buzzer);

  digitalWrite(3, LOW);delay(100);
  digitalWrite(4, LOW);delay(100);
  digitalWrite(5, LOW);delay(100);
  digitalWrite(7, LOW);delay(100);
  digitalWrite(8, LOW);delay(100);
  digitalWrite(9, LOW);delay(100);
  digitalWrite(10, LOW);delay(100);
  digitalWrite(ledR, LOW);delay(100);
  digitalWrite(ledY, LOW);delay(100);
  digitalWrite(ledG, LOW);delay(100);
  NewTone(buzzer,100);delay(200);
  noNewTone(buzzer);
 
}

void loop(){
 
  cmMsec = sonar.ping_cm();

  Serial.print("Distancia em cm: ");
  Serial.print(cmMsec);
  Serial.print("\n");
  if (cmMsec < 10) {
    for (int x = 0; x < 180; x++) {             //variar a frequancia para ficar com som de sirene
      seno = (sin(x * 3.1416 / 180));
      frequencia = 2000 + (int(seno * 1000));   //frequencia varia
      NewTone(buzzer, frequencia);              //buzinar na porta buzzer, com a frequencia
      delay(2);
      //mostrar 0 no display
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(10, LOW);

    }
  }
  if ((cmMsec >= 10) && (cmMsec < 20)) {        //se 10>=distancia<20
    noNewTone(buzzer);
    digitalWrite(ledR, HIGH);                  //só led vermelho ligado
    digitalWrite(ledY, LOW);
    digitalWrite(ledG, LOW);

    // mostra 1 no display;
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
  }
  if ((cmMsec >= 20) && (cmMsec < 30)) {      //se 20<=distancia<30
    noNewTone(buzzer);
    digitalWrite(ledR, HIGH);                 //só led vermelho ligado
    digitalWrite(ledY, LOW);
    digitalWrite(ledG, LOW);

    // mostra 2 no display;
   
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
  }
  if ((cmMsec >= 30) && (cmMsec < 40)) {      //se 30<=distancia<40
    noNewTone(buzzer);
    digitalWrite(ledR, HIGH);                //só led vermelho ligado
    digitalWrite(ledY, LOW);
    digitalWrite(ledG, LOW);
   
    // mostra 3 no display;
   
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
  }
  if ((cmMsec >= 40) && (cmMsec < 50)) {    //se 40<=distancia<50
    noNewTone(buzzer);
    digitalWrite(ledR, LOW);
    digitalWrite(ledY, HIGH);              //só led amarelo ligado
    digitalWrite(ledG, LOW);
   
    // mostra 4 no display;
   
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
  }
  if ((cmMsec >= 50) && (cmMsec < 60)) {    //se 50<=distancia<60
    noNewTone(buzzer);
    digitalWrite(ledR, LOW);
    digitalWrite(ledY, HIGH);              //só led amarelo ligado
    digitalWrite(ledG, LOW);
   
    // mostra 5 no display;
   
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
  }
  if ((cmMsec >= 60) && (cmMsec < 70)) {    //se 60<=distancia<70
    noNewTone(buzzer);
    digitalWrite(ledR, LOW);
    digitalWrite(ledY, HIGH);              //só led amarelo ligado
    digitalWrite(ledG, LOW);
   
    // mostra 6 no display;
   
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
  }
  if ((cmMsec >= 70) && (cmMsec < 80)) {    //se 70<=distancia<80
    noNewTone(buzzer);
    digitalWrite(ledR, LOW);
    digitalWrite(ledY, LOW);
    digitalWrite(ledG, HIGH);              //só led verde ligado
   
    // mostra 7 no display;
   
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
  }
  if ((cmMsec >= 80) && (cmMsec < 90)) {    //se 80<=distancia<90
    noNewTone(buzzer);
    digitalWrite(ledR, LOW);
    digitalWrite(ledY, LOW);
    digitalWrite(ledG, HIGH);              //só led verde ligado
   
    // mostra 8 no display;
   
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
  }
  if ((cmMsec >= 90) && (cmMsec < 100)) {    //se 90<=distancia<100
    noNewTone(buzzer);
    digitalWrite(ledR, LOW);
    digitalWrite(ledY, LOW);
    digitalWrite(ledG, HIGH);                //só led verde ligado
   
    // mostra 9 no display;
   
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
  }
  if ((cmMsec >= 100) || (cmMsec == 0)) {    //se distancia>100
   
    noNewTone(buzzer);
    digitalWrite(ledR, LOW);
    digitalWrite(ledY, LOW);
    digitalWrite(ledG, LOW);                 //nenhum led ligado
   
    // mostra F no display;
   
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
  }

}

Nenhum comentário:

Postar um comentário