/*
*
* DAS IST DIE FIRMWARE, WELCHE AUF DEM D1 VORINSTALLIERT IST
*
*
DESING UND INFORMATIK BEGREIFEN 2021
F.G. 03.03.2021
Einfache Ansteck-Übungen als Warm-Up
Pin-Belegung
D1 --> SCL
D2 --> SDA
D3 --> Pushbutton
D5 --> LED (PWM)
D6 --> LED
D7 --> Interrupt für Accelerometer
A0 --> Potentiometer
D8 --> LED
3.3 --> Accelerometer
3.3 --> Potentiometer (nicht 5V!)
*/
// Accelerometer:
#include "Wire.h"
#define MPU6050_ADDR 0x68 // Alternatively set AD0 to HIGH --> Address = 0x69
#define MPU6050_ACCEL_CONFIG 0x1C // Accelerometer Configuration Register
#define MPU6050_PWR_MGT_1 0x6B // Power Management 1 Register
#define MPU6050_INT_PIN_CFG 0x37 // Interrupt Pin / Bypass Enable Configuration Register
#define MPU6050_INT_ENABLE 0x38 // Interrupt Enable Register
#define MPU6050_LATCH_INT_EN 0x05 // Latch Enable Bit for Interrupt
#define MPU6050_ACTL 0x07 // Active-Low Enable Bit
#define MPU6050_WOM_EN 0x06 // Wake on Motion Enable bit
#define MPU6050_WOM_THR 0x1F // Wake on Motion Threshold Register
#define MPU6050_MOT_DUR 0x20 // Motion Detection Duration Register
#define MPU6050_ACCEL_INTEL_CTRL 0x69 // Accelaration Interrupt Control Register
#define MPU6050_SIGNAL_PATH_RESET 0x68 // Signal Path Reset Register
byte interruptPin=13;
volatile bool accEvent = false;
long timestamp = 0; // Fuer das Ein-/Ausschalten des LEDs
void setup() {
// Accelerometer:
Wire.begin();
writeRegister(MPU6050_PWR_MGT_1, 0);
setInterrupt(1); // set Wake on Motion Interrupt / Sensitivity; 1(highest sensitivity) - 255
attachInterrupt(digitalPinToInterrupt(interruptPin), motion, RISING);
pinMode(A0, INPUT);
pinMode(D4, OUTPUT); // onboard LED
pinMode(D5, OUTPUT); // LED (PWM)
pinMode(D6, OUTPUT); // LED
pinMode(D8, OUTPUT); // LED
digitalWrite(D4, HIGH); // onboard LED ausschalten
digitalWrite(D6, HIGH); // LED einschalten
digitalWrite(D8, LOW); // LED ausschalten
pinMode(D3, INPUT_PULLUP); // Taster
Serial.begin(9600);
Serial.println("boot up complete");
}
void loop() {
Serial.println(analogRead(A0));
analogWrite(D5, analogRead(A0)); // LED entsprechend dimmen (PWM)
if (digitalRead(D3) == LOW) { // Taster Status auslesen
timestamp = millis();
}
// Accelerometer:
if(accEvent){
Serial.println("motion detected");
timestamp = millis();
accEvent = false;
attachInterrupt(digitalPinToInterrupt(interruptPin), motion, RISING);
}
checkLEDStatus(); // LED steuern
}
// Fuer die LED Steuerung
void checkLEDStatus() {
if (millis() - timestamp < 1000) {
digitalWrite(D4, LOW); // onboard LED einschalten
digitalWrite(D8, HIGH); // LED einschalten
} else {
digitalWrite(D4, HIGH); // onboard LED ausschalten
digitalWrite(D8, LOW); // LED ausschalten
}
}
// Accelerometer:
void setInterrupt(byte threshold){
//writeRegister(MPU6050_SIGNAL_PATH_RESET, 0b00000111); // not(?) needed
//writeRegister(MPU6050_INT_PIN_CFG, 1<<MPU6050_ACTL); // 1<<MPU6050_LATCH_INT_EN
writeRegister(MPU6050_ACCEL_CONFIG, 0b00000001);
writeRegister(MPU6050_WOM_THR, threshold);
writeRegister(MPU6050_MOT_DUR, 0b00000001); // set duration (LSB = 1 ms)
//writeRegister(MPU6050_ACCEL_INTEL_CTRL, 0x15); // not needed (?)
writeRegister(MPU6050_INT_ENABLE, 1<<MPU6050_WOM_EN);
}
void writeRegister(uint16_t reg, byte value){
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
void motion(){
accEvent = true;
detachInterrupt(interruptPin);
}