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 107 108 109 110 111 112 113 114
| #define interruptPin 2
int16_t nbrInterrupt;
volatile boolean compteur = 0;
volatile boolean mesureEnCours = false;
volatile boolean mesureNouvelle = false;
volatile unsigned long tempoDepart = millis();
volatile unsigned long topSyncMilliSec = millis();
volatile unsigned long departSeconde = millis();
int ledSeconde = 12;
int ledMinute = 13;
const unsigned int topSyncMinimum = 1000;
void setup()
{
Serial.begin(115200);
pinMode (ledSeconde, OUTPUT);
digitalWrite(ledSeconde, LOW);
pinMode (ledMinute, OUTPUT);
digitalWrite(ledMinute, LOW);
// Initialise le Timer 2 pour déclencher les interruptions à intervalle régulier,
TCCR2A = 0; //on peut aussi écrire bitClear (TCCR2A, WGM21); // WGM21 = 0 -----//bitClear (TCCR2A, WGM20); // WGM20 = 0
TCCR2B = 0b00000110; // clk/256 est incrémenté toutes les 16uS
TIMSK2 = 0b00000001; // TOIE2 Autorise l'interruption quand le compteur déborde
pinMode(interruptPin, INPUT_PULLUP);
pinMode(ledMinute, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), impulsionInterruption, CHANGE);
}
void loop()
{
if (mesureNouvelle == true)
{
topSyncAction();
mesureNouvelle = false;
}
if (digitalRead (ledSeconde) == HIGH)
{
departSeconde = millis();
routineSeconde();
}
}
void impulsionInterruption()
{
if (digitalRead(interruptPin) == LOW && !mesureEnCours)
{
tempoDepart = millis();
mesureEnCours = true;
}
if (digitalRead(interruptPin) == HIGH && mesureEnCours)
{
topSyncMilliSec = millis() - tempoDepart;
mesureEnCours = false;
mesureNouvelle = true;
}
}
void topSyncAction()
{
if (topSyncMilliSec >= topSyncMinimum)
{
nbrInterrupt = 0;
digitalWrite (ledMinute, HIGH);
delay(50);
digitalWrite (ledMinute, LOW);
//digitalWrite (ledSeconde,HIGH);
//routineSeconde();
//digitalWrite (ledSeconde,HIGH);
}
else
{
digitalWrite(ledMinute, LOW);
}
}
ISR(TIMER2_OVF_vect)
{
TCNT2 = 6; //on met TCNT2 a 7 pour avoir un comptage de 249 incrementations 256 -7= 249
if (nbrInterrupt++ >= 249) //interruption toutes les secondes
{
nbrInterrupt = 0; //on remet nbrIterrupt à 0
compteur = 1;
digitalWrite(ledSeconde , HIGH);
}
}
void routineSeconde()
{
if (millis() >= departSeconde == 50)
{
digitalWrite (ledSeconde, LOW);
}
} |
Partager