Components and supplies
Arduino Nano R3
Arduino 4 Relays Shield
433MHz TRANSMITTER RECEIVER MODULE
6 DOF Sensor - MPU6050
Breadboard (generic)
Tools and machines
Premium Female/Male Extension Jumper Wires, 40 x 6" (150mm)
Apps and platforms
Arduino IDE
Project description
Code
CODE
c_cpp
COPY OR DOWNLOAD
1#define SIMPLE_IMPLEMENTATION false 2 3const int frontLed = 3; 4const 5 int bottomLed = 5; 6const int rightLed = 6; 7const int leftLed = 9; 8long 9 int lastPrintTime; 10 11typedef struct 12{ 13 byte pin; 14 byte positionInsideGroup; 15 16 char thePosition; // Left, Right, Up, Down 17 byte minAngle; 18 19 byte maxAngle; 20} ledConfig; 21 22typedef struct 23{ 24 byte maximumAcceptedMovement 25 = 4; 26 unsigned int millisToConsiderStill = 3000; 27 byte firstActualAngle 28 = 0; 29 unsigned long firstActualAngleMillis = 0; 30} axysStillness; 31axysStillness 32 xAxys; 33 34ledConfig leds[] = { 35 {3, 1, 'u', 31, 45}, 36 {12, 2, 37 'u', 16, 30}, 38 {11, 3, 'u', 5, 15}, 39 {5, 1, 'd', 5, 15}, 40 {6, 41 2, 'd', 16, 30}, 42 {7, 3, 'd', 31, 45}, 43 {8 , 1, 'r', 5, 23}, 44 45 {9, 2, 'r', 24, 45}, 46 {10, 1, 'l', 5, 23}, 47 {4, 2, 'l', 24, 45}, 48}; 49 50#include 51 <I2Cdev.h> 52#include <MPU6050_6Axis_MotionApps20.h> 53#if I2CDEV_IMPLEMENTATION 54 == I2CDEV_ARDUINO_WIRE 55 #include <Wire.h> 56#endif 57 58MPU6050 mpu; 59 60bool 61 dmpReady = false; // set true if DMP init was successful 62uint8_t mpuIntStatus; 63 // holds actual interrupt status byte from MPU 64uint8_t devStatus; // 65 return status after each device operation (0 = success, !0 = error) 66uint16_t 67 packetSize; // expected DMP packet size (default is 42 bytes) 68uint16_t fifoCount; 69 // count of all bytes currently in FIFO 70uint8_t fifoBuffer[64]; // FIFO 71 storage buffer 72 73// orientation/motion vars 74Quaternion q; // [w, 75 x, y, z] quaternion container 76VectorInt16 aa; // [x, y, z] accel 77 sensor measurements 78VectorInt16 aaReal; // [x, y, z] gravity-free 79 accel sensor measurements 80VectorInt16 aaWorld; // [x, y, z] world-frame 81 accel sensor measurements 82VectorFloat gravity; // [x, y, z] gravity 83 vector 84float euler[3]; // [psi, theta, phi] Euler angle container 85float 86 ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector 87volatile 88 bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high 89 90void 91 setup() 92{ 93 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 94 Wire.begin(); 95 96 TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz) 97 #elif I2CDEV_IMPLEMENTATION 98 == I2CDEV_BUILTIN_FASTWIRE 99 Fastwire::setup(400, true); 100 #endif 101 102 103 Serial.begin(9600); 104 while (!Serial); // wait for Leonardo enumeration, 105 others continue immediately 106 Serial.println(F("Initializing I2C devices...")); 107 108 mpu.initialize(); 109 Serial.println(F("Testing device connections...")); 110 111 Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : 112 F("MPU6050 connection failed")); 113 Serial.println(F("Initializing DMP...")); 114 115 devStatus = mpu.dmpInitialize(); 116 mpu.setXGyroOffset(220); 117 mpu.setYGyroOffset(76); 118 119 mpu.setZGyroOffset(-85); 120 mpu.setZAccelOffset(1788); // 1688 factory default 121 for my test chip 122 if (devStatus == 0) { 123 // turn on the DMP, now 124 that it's ready 125 Serial.println(F("Enabling DMP...")); 126 mpu.setDMPEnabled(true); 127 128 Serial.println(F("Enabling interrupt detection (Arduino external interrupt 129 0)...")); 130 attachInterrupt(0, dmpDataReady, RISING); 131 mpuIntStatus 132 = mpu.getIntStatus(); 133 Serial.println(F("DMP ready! Waiting for first 134 interrupt...")); 135 dmpReady = true; 136 packetSize = mpu.dmpGetFIFOPacketSize(); 137 138 } else { 139 Serial.print(F("DMP Initialization failed (code ")); 140 141 Serial.print(devStatus); 142 Serial.println(F(")")); 143 } 144 145 if (SIMPLE_IMPLEMENTATION) { 146 initializeLEDsSimple(); 147 } else 148 { 149 initializeLEDsMultiple(); 150 } 151 lastPrintTime = millis(); 152 153} 154 155void updateStillness(byte angle, bool forceReset) 156{ 157 if 158 (abs(xAxys.firstActualAngle - angle) > xAxys.maximumAcceptedMovement || forceReset) 159 { 160 xAxys.firstActualAngle = angle; 161 xAxys.firstActualAngleMillis 162 = millis(); 163 } 164} 165 166bool isAxysStill(byte angle) 167{ 168 return 169 millis() - xAxys.firstActualAngleMillis >= xAxys.millisToConsiderStill; 170} 171 172void 173 loop() 174{ 175 if (!dmpReady) return; 176 mpuInterrupt = false; 177 mpuIntStatus 178 = mpu.getIntStatus(); 179 fifoCount = mpu.getFIFOCount(); 180 if ((mpuIntStatus 181 & 0x10) || fifoCount == 1024) { 182 mpu.resetFIFO(); 183 Serial.println(F("FIFO 184 overflow!")); 185 } else if (mpuIntStatus & 0x02) { 186 while (fifoCount 187 < packetSize) { 188 fifoCount = mpu.getFIFOCount(); 189 } 190 mpu.getFIFOBytes(fifoBuffer, 191 packetSize); 192 fifoCount -= packetSize; 193 mpu.dmpGetQuaternion(&q, 194 fifoBuffer); 195 mpu.dmpGetGravity(&gravity, &q); 196 mpu.dmpGetYawPitchRoll(ypr, 197 &q, &gravity); 198 int x = ypr[0] * 180/M_PI; 199 int y = ypr[1] * 200 180/M_PI; 201 int z = ypr[2] * 180/M_PI; 202 //Serial.print(y);Serial.print("\ ");Serial.println(z); 203 204 updateStillness(x, false); 205 if (isAxysStill(x)) { 206 Serial.println("axys 207 still at"); 208 Serial.println(x); 209 updateStillness(x, 210 true); 211 } 212 if (SIMPLE_IMPLEMENTATION) { 213 flashLEDsSimple(x, 214 y, z); 215 } else { 216 flashLEDsMultiple(x, y, z); 217 } 218 219 } 220} 221 222void initializeLEDsSimple() 223{ 224 pinMode(frontLed, OUTPUT); 225 226 pinMode(bottomLed, OUTPUT); 227 pinMode(rightLed, OUTPUT); 228 pinMode(leftLed, 229 OUTPUT); 230} 231 232void initializeLEDsMultiple() 233{ 234 for (int i=0; 235 i<10; i++) { 236 Serial.println(leds[i].pin); 237 pinMode(leds[i].pin, 238 OUTPUT); 239 } 240 delay(3000); 241} 242 243void flashLEDsSimple(int x, int 244 y, int z) 245{ 246 if (y > 0) { 247 analogWrite(rightLed, y*4); 248 analogWrite(leftLed, 249 0); 250 } else { 251 analogWrite(leftLed, y*4*-1); 252 253 analogWrite(rightLed, 0); 254 } 255 if (z > 0) { 256 analogWrite(bottomLed, 257 z*4); 258 analogWrite(frontLed, 0); 259 } else { 260 analogWrite(frontLed, 261 z*4*-1); 262 analogWrite(bottomLed, 0); 263 } 264} 265 266void 267 flashLEDsMultiple(int x, int y, int z) 268{ 269 for (int i=0; i<10; i++) { 270 271 //Serial.print(z);Serial.print(",");Serial.print(leds[i].thePosition);Serial.print(",");Serial.println(leds[i].minAngle); 272 273 bool modified = false; 274 if (z < 0 && leds[i].thePosition == 'u' 275 && abs(z) > leds[i].minAngle) { 276 digitalWrite(leds[i].pin, HIGH); 277 278 modified = true; 279 } 280 if (z > 0 && leds[i].thePosition 281 == 'd' && abs(z) > leds[i].minAngle) { 282 digitalWrite(leds[i].pin, 283 HIGH); 284 modified = true; 285 } 286 if (y < 0 && leds[i].thePosition 287 == 'l' && abs(y) > leds[i].minAngle) { 288 digitalWrite(leds[i].pin, 289 HIGH); 290 modified = true; 291 } 292 if (y > 0 && leds[i].thePosition 293 == 'r' && abs(y) > leds[i].minAngle) { 294 digitalWrite(leds[i].pin, 295 HIGH); 296 modified = true; 297 } 298 if (!modified) { 299 300 digitalWrite(leds[i].pin, LOW); 301 } 302 } 303} 304 305void 306 dmpDataReady() 307{ 308 mpuInterrupt = true; 309} 310
Downloadable files
RECEIVER CIRCUIT
RECEIVER CKT
RECEIVER CIRCUIT
RECEIVER CIRCUIT
RECEIVER CKT
RECEIVER CIRCUIT
TRANSMITTER CIRCUIT
TRANSMITTER CKT
TRANSMITTER CIRCUIT
Documentation
CODE
COPY AND PASTE OR DOWNLOAD
CODE
CODE
COPY AND PASTE OR DOWNLOAD
CODE
CODE
COPY AND PASTE OR DOWNLOAD
CODE
Comments
Only logged in users can leave comments