Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gryo-microview.ino: MicroView (Arduino-based) from SparkFun with Parallax Gyroscope Module 3-Axis L3G4200D
// Demo of MicroView (Arduino-based) from SparkFun with Parallax Gyroscope Module 3-Axis L3G4200D
// Author: Drew Fustini (@pdp7)
// LICENSE:
// This code is licensed as CC0 ("No Rights Reserved") except for example code used from:
// http://learn.parallax.com/KickStart/27911 (Creative Commons 3.0 SA license)
// http://learn.microview.io/doco/html/index.html (GPL v3)
#include <Wire.h>
#include <MicroView.h>
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
int Addr = 105; // I2C address of gyro
int x, y, z;
MicroViewWidget *widgetx, *widgety, *widgetz;
void setup() {
Wire.begin();
writeI2C(CTRL_REG1, 0x1F); // Turn on all axes, disable power down
writeI2C(CTRL_REG3, 0x08); // Enable control ready signal
writeI2C(CTRL_REG4, 0x80); // Set scale (500 deg/sec)
delay(100); // Wait to synchronize
uView.begin(); // start MicroView
uView.clear(PAGE);
uView.print("bringahack");
uView.setCursor(0, 15);
uView.print("x");
uView.setCursor(0, 28);
uView.print("y");
uView.setCursor(0, 40);
uView.print("z");
widgetx = new MicroViewSlider(10, 15, 0, 100); // draw Slider widget at x=0,y=0,min=0, max=100
widgety = new MicroViewSlider(10, 28, 0, 100); // draw Slider widget at x=0,y=0,min=0, max=100
widgetz = new MicroViewSlider(10, 40, 0, 100); // draw Slider widget at x=0,y=0,min=0, max=100
delay(100);
}
void loop() {
getGyroValues(); // Get new values
// In following Dividing by 114 reduces noise
// uView.print("X: "); uView.print(x / 114);
// uView.print("Y:"); uView.print(y / 114);
// uView.print("Z:"); uView.println(z / 114);
widgetx->setValue(x / 114); // give a value to widget
widgety->setValue(y / 114); // give a value to widget
widgetz->setValue(z / 114); // give a value to widget
uView.display(); // display current page buffer
delay(500); // Short delay between reads
}
void getGyroValues () {
byte MSB, LSB;
MSB = readI2C(0x29);
LSB = readI2C(0x28);
x = ((MSB << 8) | LSB);
MSB = readI2C(0x2B);
LSB = readI2C(0x2A);
y = ((MSB << 8) | LSB);
MSB = readI2C(0x2D);
LSB = readI2C(0x2C);
z = ((MSB << 8) | LSB);
}
int readI2C (byte regAddr) {
Wire.beginTransmission(Addr);
Wire.write(regAddr); // Register address to read
Wire.endTransmission(); // Terminate request
Wire.requestFrom(Addr, 1); // Read a byte
while (!Wire.available()) { }; // Wait for receipt
return (Wire.read()); // Get result
}
void writeI2C (byte regAddr, byte val) {
Wire.beginTransmission(Addr);
Wire.write(regAddr);
Wire.write(val);
Wire.endTransmission();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.