I am new to the arduino and have bought myself the maker kit as well as a gyro model L3G4200D. I have an LCD attached and have read the values from it. I deleted the print code completely from the code and my LCD still shows the output values. I used lcd.print(x); to print my x value and it worked. I cannot think of how it could possibly do something that isn't there. Could someone please tell me why it would do something so weird?
//Arduino 1.0+ only
#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal.h>#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24Servo myservo;
int potpin = 0;
int val;int L3G4200D_Address = 105; //I2C address of the L3G4200D
int x;
int y;
int z;LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup(){
lcd.begin(16, 2);
myservo.attach(0);
Wire.begin();
Serial.begin(9600);//lcd.println("starting up L3G4200D");
setupL3G4200D(2000); // Configure L3G4200 - 250, 500 or 2000 deg/secdelay(1500); //wait for the sensor to be ready
delay(1500);
lcd.clear();
}void loop(){
getGyroValues(); // This will update x, y, and z with new values
int alex;Serial.print(" Y:");
Serial.print(y);Serial.print(" Z:");
Serial.println(z);
digitalWrite(0, HIGH);
pinMode(2, INPUT);
if (digitalRead(2) == HIGH){
myservo.detach();
alex = 100000;
val = 0;
}
val = x -12; // reads the value of the potentiometer (value between 0 and 1023)
//digitalWrite(10, HIGH);
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled valuedelay(100); //Just here to slow down the serial to make it more readable
}void getGyroValues(){
byte xMSB = readRegister(L3G4200D_Address, 0x29);
byte xLSB = readRegister(L3G4200D_Address, 0x28);
x = ((xMSB << 8) | xLSB);byte yMSB = readRegister(L3G4200D_Address, 0x2B);
byte yLSB = readRegister(L3G4200D_Address, 0x2A);
y = ((yMSB << 8) | yLSB);byte zMSB = readRegister(L3G4200D_Address, 0x2D);
byte zLSB = readRegister(L3G4200D_Address, 0x2C);
z = ((zMSB << 8) | zLSB);
}int setupL3G4200D(int scale){
//From Jim Lindblom of Sparkfun's code// Enable x, y, z and turn off power down:
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);// If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you'd like to configure INT1
// or INT2 otherwise, consult the datasheet:
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);// CTRL_REG4 controls the full-scale range, among other things:
if(scale == 250){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
}else if(scale == 500){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
}else{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
}// CTRL_REG5 controls high-pass filtering of outputs, use it
// if you'd like:
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}void writeRegister(int deviceAddress, byte address, byte val) {
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}int readRegister(int deviceAddress, byte address){
int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // register to read
Wire.endTransmission();Wire.requestFrom(deviceAddress, 1); // read a byte
while(!Wire.available()) {
// waiting
}v = Wire.read();
return v;
}
Moderator edit: Smileys removed using "Additional options - Don't use smileys."