Dear all:
I’ll try to explain my needs/unsolved problems as schematic as posible, so you can understand it better.
Purpose of my sketch: pulse counting (rpm) of a wheel with an LCD 1602 display with keypad and connected to an excel spreadsheet.
Actual state: sketch (1st code) who works perfectly and fulfills my needs,but…
Problems/needs:
-I would like to configure 2 buttons of the keypad for adding/substracting 1 unit on every push of each button
I would like to store the value of the counter on the EEPROM (by pushing another of the buttons) and to recover this value if I reset or connect again the arduino sketch.
I tried to use parts of the following (2nd)code but with no luck.
Could you help me with the sketch, adding this buttons ? They have to do following:
Button UP: calibr = calibr + 0.001;
Button DOWN: calibr = calibr - 0.001;
Button LEFT = (store counter in eeprom)
Thank you very much in advance!!
/* Pulse counter with 1602 lcd1602 keypad shield*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const float calibr = 1.673; // calibration factor
const int sensor = 2;
long pulsecounter = 0; // pulse counter
int actualcounter = 0; // actual state of counter
int previouscounter= 0; // previous state of counter
int second = 0;
int minute = 0;
int msg=0;
float meters = 0.00;
long lastEvent = 0; // time for last pulse
int interval = 30; // debounce time for ignore double pressing
void setup()
{
Serial.begin(19200);
pinMode(sensor, INPUT); // initializing sensor as input
lcd.begin(16, 2); // lcd library starting
lcd.setCursor(0,0);
lcd.print("C=");
lcd.setCursor(2,0);
lcd.print(calibr,3);
if(msg==0) //welcome message show only once
{
lcd.setCursor(0,0);
lcd.print("Counter");
lcd.setCursor(1,1);
lcd.print("Reset OK");
delay(1000);
msg = 1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("C=");
lcd.setCursor(2,0);
lcd.print(calibr,3);
}
}
void loop()
{
actualcounter = digitalRead(sensor);
minute = millis() / 60000 ;
second = (millis() / 1000 ) - ( minute* 60 ) ;
lcd.setCursor(11,1);
if (minute < 10) lcd.print("0"); // if minutes <10 it writes a "0" before
lcd.print(minute); // without this code it would be: H:M:S (1:M:S)
lcd.print(":");
if (second < 10) lcd.print("0"); // same reason as above
lcd.print(second);
actualcounter= digitalRead(sensor); // digital read of sensor state
if (actualcounter != previouscounter) // compares actual state with it previous
{
if (actualcounter == HIGH && millis() - lastEvent > interval)
// ignores a second press if it’s too close to the previous
{
lastEvent = millis(); // writes time for last valid pulse/press
counterr++;
meters= counter * calibr / 1000 ;
Serial.println(counter);
{
lcd.setCursor(0,1); // cursor on second line "1" and 0 spaces
lcd.print("Dis=");
lcd.setCursor(4,1) ;
lcd.print(meters,2);
lcd.setCursor(8,0);
lcd.print("P=");
lcd.setCursor(10,0);
lcd.print(pulsecounter);
}
}
}
previouscounter = actualcounter;
// saves the actual state as the previous one through the loop
}
and the second one which I tried to use some code..but without results... (uploading itself alone it works fine!)
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_key = 0;
int adc_key_in = 0;
int msg = 0 ;
float calibr = 1.673;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int read_LCD_buttons()
{
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE;
msg=0;
}
void setup()
{
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Press buttons");
}
void loop()
{
lcd.setCursor(9,1);
lcd.print(millis()/1000);
lcd.setCursor(0,1);
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnRIGHT:
{
calibr = calibr + 0.0002;
delay ( 250) ;
lcd.setCursor(0,0);
lcd.print("C=");
lcd.setCursor(2,0);
lcd.print(calibr,3);
break ;
}
case btnLEFT:
{
calibr = calibr - 0.0002;
delay ( 250) ;
lcd.setCursor(0,0);
lcd.print("C=");
lcd.setCursor(2,0);
lcd.print(calibr,3);
break;
}
case btnUP:
{
millis()=0;
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}