Principe :
La détection d’un mouvement affiche l’horloge.
Matériel :
* Un arduino UNO,
* Un afficheur à segments à base de TM1637
* Un détecteur PIR.
L’horloge est logicielle
La librairie de l’afficheur est ici et celle du timer là
Pour le branchement du PIR, soulevez le cache et repéres le VCC (+5v), GND et DIO (la broche servant à tester la détection).
Le skecth est téléchargeable ici
/*******************************************************************************/
/* Horloge à Ultrasons
/* Détecte la proximité d'un objet ou d'un corps et en fonction de la distance
/* augmente l'intensité de la luminosité de l'affichage
/*******************************************************************************/
#include <TimerOne.h> //-- Librairie pour l'horloge
#include "TM1637.h" //-- Librairie pour l'afficheur
#define ON 1
#define OFF 0
int8_t TimeDisp[] = {0x00,0x00,0x00,0x00};
unsigned char ClockPoint = 1;
unsigned char Update;
unsigned char halfsecond = 0;
//-- Réglez ici l'heure d'initialisation de l'horloge
unsigned char second;
unsigned char minute = 04;
unsigned char hour = 19;
//-- Pour le PIR
const int inputPin = 3; // PIR sensor input pin
int pirState = LOW; // Pour détection, LOW = no motion detected
int val = 0; // Pour lire le statut du PIR
//-- Afficheur TM1637
#define CLK 4 // pins pour le TM1637
#define DIO 5
TM1637 tm1637(CLK,DIO);
//-- Setup
void setup()
{ Serial.begin(9600);
tm1637.set();
tm1637.init();
Timer1.initialize(500000); //-- timing de 500ms
Timer1.attachInterrupt(TimingISR); //-- declaration de la routine d'interruption:TimingISR
pinMode(inputPin, INPUT); //-- declare sensor as input
}
void loop()
{ val = digitalRead(inputPin); //-- read input value
if (val == HIGH) { //-- check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
// On affiche l'heure
tm1637.set(0x07);
}
} else {
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
// On efface l'affichage de l'heure
tm1637.set(8);//8 annule 88 !
}
}
//-- Si l'interruption est levée
if(Update == ON)
{TimeUpdate();
tm1637.display(TimeDisp);
}
}
//-- Routine d'interruption
void TimingISR()
{ halfsecond ++;
Update = ON;
if(halfsecond == 2){
second ++;
if(second == 60)
{
minute ++;
if(minute == 60)
{
hour ++;
if(hour == 24)hour = 0;
minute = 0;
}
second = 0;
}
halfsecond = 0;
}
ClockPoint = (~ClockPoint) & 0x01;
}
//-- Mise à jour de l'affichage
void TimeUpdate(void)
{ if(ClockPoint)tm1637.point(POINT_ON);
else tm1637.point(POINT_OFF);
TimeDisp[0] = hour / 10;
TimeDisp[1] = hour % 10;
TimeDisp[2] = minute / 10;
TimeDisp[3] = minute % 10;
Update = OFF;
}
Votre commentaire