top of page
Rechercher

21.1 - Afficher les valeurs d'un tilt capteur d'inclinaison

  • L C
  • 10 sept. 2024
  • 1 min de lecture

Matériel


  • 1 carte Arduino Uno

  • 1 breadboard

  • 1 capteur d'inclinaison tilt

  • Fils de connexion


Montage



Sketch


//Paramètre

const int tiltPin = 2;


//Variables

bool tiltStatus = false;

bool oldTiltStatus = false;

unsigned long lastDebounceTime = 0;

unsigned long debounceDelay = 50;


void setup() {

//Init Serial USB

Serial.begin(9600);

Serial.println(F("Initialisation du système"));

//Init digital input

pinMode(tiltPin, INPUT);

}


void loop() {

debounceTilt();

}


void debounceTilt( ) {

////debounce TiltSwitch

int reading = digitalRead(tiltPin);

if (reading != oldTiltStatus) {

lastDebounceTime = millis();

}

if ((millis() - lastDebounceTime) > debounceDelay) {

if (reading != tiltStatus) {

tiltStatus = reading;

Serial.print(F("Sensor state : ")); Serial.println(tiltStatus);

}

}

oldTiltStatus = reading;

}


Explication


Une bille fait le contact et ferme l’interrupteur.

Il arrive que cette bille rebondisse au changement d’état.

Pour éviter de prendre en compte ces rebonds parasites, on utilise une logique anti-rebond (debounce).


Réalisation



bottom of page