Compare commits
2 Commits
main
...
Interrupts
Author | SHA1 | Date | |
---|---|---|---|
9ff331ba66 | |||
e2adb04342 |
@ -12,9 +12,3 @@
|
||||
platform = espressif32
|
||||
board = upesy_wroom
|
||||
framework = arduino
|
||||
lib_deps = adafruit/Adafruit MPU6050@^2.2.6
|
||||
|
||||
; How to change bit-per-second rate for your project:
|
||||
; Add right under [env:upesy_wroom]:
|
||||
; "monitor_speed = x" (without the "")
|
||||
; x == baudrate of selected choice, bsp: 115200, 8000, 300
|
155
src/main.cpp
155
src/main.cpp
@ -1,141 +1,50 @@
|
||||
//--------------------------------------------------------------------------------------------------------------------------//
|
||||
// Setting up the Adafruit_MPU6050 library for the accelerator chip
|
||||
// Handling Interrupts correctly
|
||||
//--------------------------------------------------------------------------------------------------------------------------//
|
||||
|
||||
// Check the other guide in the .docx as to how to INSTALL the necessary library //
|
||||
// What are Interrupts? //
|
||||
// You can see them as certain parts of a programm reacting to a condition at ANY time and place your programm is currently in.
|
||||
// You use them to time reads for example, or for signals, etc etc.
|
||||
|
||||
// Must be included for most things! //
|
||||
#include <Arduino.h> // Only include once of course.
|
||||
#include <Adafruit_MPU6050.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
|
||||
// Create instance of the sensor library //
|
||||
Adafruit_MPU6050 mpu;
|
||||
#include <Arduino.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200); // For Serial communication, check if the right bit-per-second rate is selected!
|
||||
// If you want to change said bit-per-second, look in the .ini file for information.
|
||||
// How do you know the one selected does not work / there's something wrong in the .ini file?
|
||||
// Check the terminal, if that is the case everything printed is Unicode gibberish.
|
||||
// Initializing the timer //
|
||||
void IRAM_ATTR onTimer();
|
||||
|
||||
while(!Serial) delay(10); // Wait until the Serial communication is set up and initialized.
|
||||
Serial.println("Starting...");
|
||||
hw_timer_t *My_timer = 0;
|
||||
My_timer = timerBegin(0, 80, true);
|
||||
timerAttachInterrupt(My_timer, &onTimer, true);
|
||||
timerAlarmWrite(My_timer, 5000, true);
|
||||
timerAlarmEnable(My_timer);
|
||||
|
||||
if(!mpu.begin()) // Exception for the case of the mpu not starting up
|
||||
{
|
||||
// If this is the case, check the wiring and whether or not the right pins are connected.
|
||||
// Another issue that could lead to this is too little or too much current going through the chip.
|
||||
Serial.println("Failed to find MPU6050 chip.");
|
||||
while(1)
|
||||
{
|
||||
delay(10);
|
||||
// This is crutial and has to be in the setup.
|
||||
// You can change the frequency as to when it gets called (Check it out online), as well as the reason.
|
||||
|
||||
// You can attach a timer to a Pin for example as well.
|
||||
// That would look something like this:
|
||||
//
|
||||
// pinMode(SWITCH_PIN, INPUT_PULLUP);
|
||||
// attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), switchChanged, CHANGE);
|
||||
// But it's not necessary if you're using Interrupts as a timer.
|
||||
}
|
||||
}
|
||||
Serial.println("MPU6050 has been found.");
|
||||
|
||||
mpu.setAccelerometerRange(MPU6050_RANGE_8_G); // Set to the range of your choice for the accelerator values
|
||||
// depending on the range you want your read in values to vary in.
|
||||
Serial.println("Accelerator range set to: ");
|
||||
switch(mpu.getAccelerometerRange()) // Check setting and print for insight.
|
||||
{
|
||||
case MPU6050_RANGE_2_G:
|
||||
Serial.println("+-2G");
|
||||
break;
|
||||
case MPU6050_RANGE_4_G:
|
||||
Serial.println("+-4G");
|
||||
break;
|
||||
case MPU6050_RANGE_8_G:
|
||||
Serial.println("+-8G");
|
||||
break;
|
||||
case MPU6050_RANGE_16_G:
|
||||
Serial.println("+-16G");
|
||||
break;
|
||||
}
|
||||
|
||||
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
|
||||
Serial.print("Gyro range set to: ");
|
||||
switch (mpu.getGyroRange())
|
||||
{
|
||||
case MPU6050_RANGE_250_DEG:
|
||||
Serial.println("+- 250 deg/s");
|
||||
break;
|
||||
case MPU6050_RANGE_500_DEG:
|
||||
Serial.println("+- 500 deg/s");
|
||||
break;
|
||||
case MPU6050_RANGE_1000_DEG:
|
||||
Serial.println("+- 1000 deg/s");
|
||||
break;
|
||||
case MPU6050_RANGE_2000_DEG:
|
||||
Serial.println("+- 2000 deg/s");
|
||||
break;
|
||||
}
|
||||
|
||||
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
|
||||
Serial.print("Filter bandwidth set to: ");
|
||||
switch (mpu.getFilterBandwidth())
|
||||
{
|
||||
case MPU6050_BAND_260_HZ:
|
||||
Serial.println("260 Hz");
|
||||
break;
|
||||
case MPU6050_BAND_184_HZ:
|
||||
Serial.println("184 Hz");
|
||||
break;
|
||||
case MPU6050_BAND_94_HZ:
|
||||
Serial.println("94 Hz");
|
||||
break;
|
||||
case MPU6050_BAND_44_HZ:
|
||||
Serial.println("44 Hz");
|
||||
break;
|
||||
case MPU6050_BAND_21_HZ:
|
||||
Serial.println("21 Hz");
|
||||
break;
|
||||
case MPU6050_BAND_10_HZ:
|
||||
Serial.println("10 Hz");
|
||||
break;
|
||||
case MPU6050_BAND_5_HZ:
|
||||
Serial.println("5 Hz");
|
||||
break;
|
||||
}
|
||||
|
||||
delay(100);
|
||||
|
||||
// What is the difference between "void setup()" and "void loop()"? Why put all this in here? //
|
||||
// The setup fuction is only initialized once, and though that's true for the entire lifetime of the programm,
|
||||
// the started services continue to run in here on this threat, allowing the loop to be executed simultaniously.
|
||||
// Mind, and that's IMPORTANT!!, that you should refer from using "while(1)" in this function as much as possible.
|
||||
// (I'm aware there is one in here in this project, but only because it is handled correctly and carefully.)
|
||||
}
|
||||
|
||||
// Put your variables here for testing, otherwise I'd recommend putting it in a class //
|
||||
float _x_accel = 0; // x acceleration
|
||||
float _y_accel = 0; // y acceleration
|
||||
float _calc = 0; // for calculations
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Save vals if you want to send them: //
|
||||
sensors_event_t _a;
|
||||
sensors_event_t _g;
|
||||
sensors_event_t _temp; // Used as placeholder for now in the next function, even if you don't need temperature values.
|
||||
|
||||
mpu.getEvent(&_a, &_g, &_temp); // Reads in your values.
|
||||
}
|
||||
|
||||
_x_accel = _a.acceleration.x; // acceleration in x.
|
||||
_y_accel = _a.acceleration.y; // acceleration in y.
|
||||
|
||||
// Basic example for use //
|
||||
_calc = _y_accel * 12.5; // Calculate postion if you want to.
|
||||
if(_calc > 100) // reduce values if you only want values from (-100) - 100 for example
|
||||
// Handling the function //
|
||||
void IRAM_ATTR onTimer()
|
||||
{
|
||||
_calc = 100;
|
||||
}
|
||||
else if(_calc < (-100))
|
||||
{
|
||||
_calc = (-100);
|
||||
// This function is called when the interrupt has been triggered.
|
||||
|
||||
// Keep your operations in here as SIMPLE as possible, and as quick as possible.
|
||||
// DO NOT do heavy processing on this threat, or perform long lasting operations.
|
||||
// ^ That includes any sort of printing or writing to a file for example.
|
||||
// Also refrain from using while(1) AT ALL COSTS. Short lasting while loops are fine, but also make sure they don't take too long to break out again.
|
||||
}
|
||||
|
||||
// And done! //
|
||||
// Mind that everything in this loop repeats itself over and over, only stopping until the programm fails to execute or //
|
||||
// the current is cut. If you'd like to restart everything, press the little button titled "RST" or "BOOT" (if the first does nothing) on the ESP. //
|
||||
}
|
||||
// That is basically it for now regarding simple interrupts.
|
||||
// It's useful regarding sending data only like every 5 minutes, or every 10 milliseconds for example. Use it carefully, and happy coding. :D
|
Loading…
Reference in New Issue
Block a user