noNoseOnTV part 3

Voici le dernier code source de l’appareil NoNoseOnTV.

Et voici le schéma :


Sensor_bb

// # Author  : Hugues Lepesant
// # Date    : 30/04/2014
// # Version : 0.3
 
// # URM Code based on Jiang's work from DFRobot
// # Product name:ultrasonic scanner Kit
// # Product SKU:SEN0001
  
// # Description:
// # The Sketch for scanning 180 degree area 4-500cm detecting range
  
// # Connection:
// #       Pin 1 VCC (URM V3.2) -> VCC (Arduino)
// #       Pin 2 GND (URM V3.2) -> GND (Arduino)
// #       Pin 4 PWM (URM V3.2) -> Pin 6 (Arduino)
// #       Pin 6 COMP/TRIG (URM V3.2) -> Pin 5 (Arduino)
// # Pin mode: PWM
// # Working Mode: PWM passive control mode.
// # If it is your first time to use it,please make sure the two jumpers to the right hand
// # side of the device are set to TTL mode. You'll also find a secondary jumper on
// # the left hand side, you must break this connection or you may damage your device.


#include <IRremote.h>
#include <NewTone.h>


const int URPWM = 6;      // PWM Output 0-25000us,every 50us represent 1cm
const int URTRIG = 5;     // PWM trigger pin

const int greenLedPin = 2;
const int yellowLedPin = 4;
const int redLedPin = 7;
const int buzzerPin = 8;
 
const int distMinimum = 100; // 1 meter
const int distMedium  = 150; // 1 meter and half

const int duration = 800;
int alert = 0;
int tvPower = 1;
 
//boolean up=true;       // create a boolean variable
//unsigned long time;    // create a time variable
//unsigned long urmTimer = 0; // timer for managing the sensor reading flash rate
unsigned int Distance=0;

IRsend irsend;
 
uint8_t EnPwmCmd[4]={0x44,0x22,0xbb,0x01}; // distance measure command
  
void setup() {             // Serial initialization
    Serial.begin(9600);    // Sets the baud rate to 9600
 
    pinMode(greenLedPin, OUTPUT);
    digitalWrite(greenLedPin, LOW);
     
    pinMode(yellowLedPin, OUTPUT);
    digitalWrite(yellowLedPin, LOW);
     
    pinMode(redLedPin, OUTPUT);
    digitalWrite(redLedPin, LOW);
 
    PWM_Mode_Setup();
}
  
void loop() {
  Distance = PWM_Mode();

  
  Serial.print("Distance = ");
  Serial.print(Distance);
  Serial.println(" cm");
   
  if ( Distance > distMedium ) {
    digitalWrite(greenLedPin, HIGH);
    digitalWrite(yellowLedPin, LOW);
    digitalWrite(redLedPin, LOW);
    
    if ( tvPower == 0 ) {
      SendPowerCode(tvPower);
      tvPower = 1;
    }
    
    alert = 0;
  }
 
  if ( Distance > distMinimum && Distance < distMedium ) {
    digitalWrite(greenLedPin, LOW);
    digitalWrite(yellowLedPin, HIGH);
    digitalWrite(redLedPin, LOW);
  }
 
  if ( Distance < distMinimum ) {
    digitalWrite(greenLedPin, LOW);
    digitalWrite(yellowLedPin, LOW);
    digitalWrite(redLedPin, HIGH);
    
    alert++;

    NewTone(buzzerPin, 440, duration);


    if (alert > 2) {
      if ( tvPower == 1 ) {
        SendPowerCode(tvPower);
      }
      tvPower = 0;
    }
 }

  delay(1000);
  
  Serial.print("ALERT = ");
  Serial.println(alert);
}
 
  
void PWM_Mode_Setup(){
    pinMode(URTRIG,OUTPUT);       // A low pull on pin COMP/TRIG
    digitalWrite(URTRIG,HIGH);    // Set to HIGH
  
    pinMode(URPWM, INPUT);        // Sending Enable PWM mode command
     
    for (int i=0;i<4;i++) {
        Serial.write(EnPwmCmd[i]);
    }
}
  
int PWM_Mode(){                   // a low pull on pin COMP/TRIG  triggering a sensor reading
    digitalWrite(URTRIG, LOW);
    digitalWrite(URTRIG, HIGH);   // reading Pin PWM will output pulses
      
    unsigned long DistanceMeasured=pulseIn(URPWM,LOW);
      
    if (DistanceMeasured==50000) { // the reading is invalid.
      return 0;
    } else {
        return DistanceMeasured/50; // every 50us low level stands for 1cm
    }
}

void SendPowerCode(int tvPower)
{
  for (int i = 0; i < 3; i++) {
    irsend.sendSony(0xa90, 12); // Sony TV power code
    delay(100);
  }
  
  if ( tvPower == 1 ) {
    Serial.print("Signal Off sent");
  } else {
    Serial.print("Signal On sent");
  }
}

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *