hello guy:
This is my code Acting:
1.4x4keypad input value print on lcd1602 i2c(this success)
2.IMU input value print on lcd1602 i2c(this success)
3.I hope can keypad input value and IMU input value Compare ,if IMU input value >= keypad input value LED1 HIGH ,else LED1 LOW.(bug)
but i hope can Compare keypad input value and IMU input value ,what wrong with my code?
Thank !!!
#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define KEY_ROWS 4
#define KEY_COLS 4
#define LCD_ROWS 2
#define LCD_COLS 16
#define LED1 5
#define PRINT_CALCULATED
//#define PRINT_RAW
#define PRINT_SPEED 10 // 250 ms between prints
#define password_Length 10
#define DECLINATION -8.58 // Declination (degrees) in Boulder, CO.
LiquidCrystal_I2C lcd(0x27, 16, 2);
LSM9DS1 imu;
char keymap[KEY_ROWS][KEY_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[KEY_ROWS] = {13, 12, 11, 10};
byte colPins[KEY_COLS] = {9, 8, 7, 6};
Keypad keypad = Keypad(makeKeymap(keymap), rowPins, colPins, KEY_ROWS, KEY_COLS);
static unsigned long lastPrint = 0; // Keep track of print time
//Function definitions
void resetLocker();
void printAccel();
void clearRow2();
float Vkm;
float vX=0;
bool acceptKey = true;
char Data[password_Length];
byte data_count=6;
String keyword = "";
char key ;
int intV,num;
long lastTime=0;
void resetLocker() {
lcd.clear();
lcd.print("Set V:");
acceptKey = true;
keyword = "";
lcd.setCursor(0, 1);
lcd.print("Real V:");
}
void clearRow2(byte n) {
byte last = LCD_COLS - n;
lcd.setCursor(n, 1);
for (byte i = 0; i < last; i++) {
lcd.print(" ");
}
lcd.setCursor(n, 1);
}
void setup()
{
Serial.begin(115200);
Wire.begin();
if (imu.begin() == false) // with no arguments, this uses default addresses (AG:0x6B, M:0x1E) and i2c port (Wire).
{
Serial.println("Failed to communicate with LSM9DS1.");
Serial.println("Double-check wiring.");
Serial.println("Default settings in this sketch will " \
"work for an out of the box LSM9DS1 " \
"Breakout, but may need to be modified " \
"if the board jumpers are.");
while (1);
}
lcd.begin();
lcd.backlight();
pinMode(LED1,OUTPUT);
digitalWrite(LED1,LOW);
resetLocker();
}
void loop()
{
key = keypad.getKey();
num=int(key)-48;//ASCLL TRANSFER int
if (acceptKey && key != NO_KEY){
if (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'){
Data[data_count] = key;
lcd.setCursor(data_count,0);
lcd.print(num);
data_count++;
}
else if(key=='#'){
int len;
len=(sizeof(Data)-1);
if(len>6){
data_count=6;
resetLocker();
lcd.setCursor(data_count,0);
lcd.print("");
data_count++;
}
}
}
else if(key=='*'){
clearRow2(7);
}
// Update the sensor values whenever new data is available
if ( imu.accelAvailable() )
{
// To read from the accelerometer, first call the
// readAccel() function. When it exits, it'll update the
// ax, ay, and az variables with the most current data.
imu.readAccel();
}
if ((lastPrint + PRINT_SPEED) < millis())
{
printAccel();
Serial.println();
lastPrint = millis(); // Update lastPrint time
}
if (intV >=num) {
digitalWrite(LED1,HIGH);
} else {
digitalWrite(LED1,LOW);
}
}
void printAccel()
{
float Axy=(imu.calcAccel(imu.ax)-imu.calcAccel(imu.ay));
long newTime=millis();
float accelX= imu.calcAccel(imu.ax);
vX = vX + accelX * (newTime - lastTime)/1000;
Vkm=vX*3.6;
intV=Vkm;
lastTime=newTime;
//Realvelocity=Vkm;
Serial.print("A: ");
#ifdef PRINT_CALCULATED
// If you want to print calculated values, you can use the
// calcAccel helper function to convert a raw ADC value to
// g's. Give the function the value that you want to convert.
Serial.print(imu.calcAccel(imu.ax), 2);
Serial.print(", ");
Serial.print(imu.calcAccel(imu.ay), 2);
Serial.print(", ");
Serial.print(Axy, 2);
Serial.print(" g");
Serial.print(" ");
Serial.print("V: ");
// Serial.print(Vkm, 2);
#elif defined PRINT_RAW
Serial.print(imu.ax);
Serial.print(", ");
Serial.print(imu.ay);
Serial.print(", ");
Serial.print(imu.az);
#endif
if(Vkm<0){
lcd.setCursor(8,1);
lcd.print(0);
Serial.print(0, 2);
}
else if(Vkm>=0){
lcd.setCursor(8,1);
lcd.print(intV);
Serial.print(Vkm, 2);
Serial.print("intV: ");
Serial.print(intV);
}
}
troublerev1.0.ino (4.92 KB)