Einrad mit LED basierend auf arduino + MPU6050

20. Februar 2016 at 09:19
Print Friendly, PDF & Email

Hier wird über ein arduino die Beschleunigung des Rades gemessen.

Abhängig hiervon wird die Beleuchtung geregelt.

 

MP6050-Wheel-arduino-1

[codesyntax lang=”c”]

//
// unicycle LED controller
// 2016-02-20 by Thomas Hoeser
// V0.1

// The output scale for any setting is [-32768, +32767] for each of the six axes.

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"

#include "Wire.h"

MPU6050 accelgyro; // default I2C address is 0x68

int16_t ax, ay, az;
int16_t gx, gy, gz;
int agstatus = 0;
int brightness = 0;
int minBrightness = 5;
int maxAccLo = -32767; //sensor max 32767
int maxAccHi = 32767; //sensor max 32767

#define LED_PIN 13
#define LED2_PIN 5 // PWM
bool blinkState = false;
unsigned long refreshTimer, flashTimer, tempTimer;

void printDec(int decVal);

//---------------------------------------------------------------------------------------------------
void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    agstatus = accelgyro.testConnection();
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    // configure Arduino LED for
    pinMode( LED_PIN, OUTPUT);
    pinMode(LED2_PIN, OUTPUT);

    refreshTimer = flashTimer = millis();
} // end of setup()

//---------------------------------------------------------------------------------------------------
void loop() {

    if(agstatus){
    	// read raw gyro measurements from device
    	accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    	if(abs(az)> 1000){ // if wheel is flat on the ground, switch LED off
    		analogWrite(LED2_PIN, 0); // PWM output
    	}
    	else {
    		// map(value, fromLow, fromHigh, toLow, toHigh)
    		brightness = abs(gz);
    		brightness = map (brightness,0,maxAccHi,0,255);//-32768, +32767
    		if (brightness < minBrightness) { // disregard jitter / noise
    			analogWrite(LED2_PIN, 0);
    		} else {
    			analogWrite(LED2_PIN, brightness); // PWM output
    		}
    	} // wheel not upright


        // display tab-separated gyro x/y/z values
        Serial.print("gyro raw:\t");
        printDec(gx);Serial.print("\t");
        printDec(gy);Serial.print("\t");
        printDec(gz);Serial.print("\t");
        printDec(brightness);Serial.print("\n");
        // delay(100); // serial console might be overloaded - grant some delay

        // blink LED to indicate activity
		tempTimer = millis();
		if (tempTimer - flashTimer < 750) {
			digitalWrite(LED_PIN,0);
		}
		else if (tempTimer - flashTimer > 749) {
			digitalWrite(LED_PIN,1);
		} // tempTimer
		if (tempTimer - flashTimer > 1024) {
			flashTimer = millis(); // reset timer after 1 second(ish)
		}

    } // MPU6050 not available
} // end of loop()

//---------------------------------------------------------------------------------------------------
void printDec(int decVal){
	String printStr;
	String stringVal;

	if(decVal <0){
		printStr = String("-");
		decVal = decVal * -1;
	}
	else {
		printStr = String(" ");
	}

	stringVal = String(decVal, DEC);
	if(decVal <10000) { printStr = String(" " + printStr ); }
	if(decVal < 1000) { printStr = String(" " + printStr ); }
	if(decVal <  100) { printStr = String(" " + printStr ); }
	if(decVal <   10) { printStr = String(" " + printStr ); }
	printStr = String(printStr + stringVal );
	Serial.print(printStr);

}  // end of printDec()

[/codesyntax]