Counter

Hello all

I have a problem with part of my code for a batch counter.
I want to move a blinking cursor from right to left over a series of eight 0's and then change the value of the 0.
But my problem is when i press a button to move the cursor it jumps twice i have attached the piece of code for all to view.

Help please

bc_edit.ino (1.47 KB)

OPs code:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonChange = 9;

int butChangeCounter = 11;

int butChange = 0;
int butChangeLBS = 0;

int la = 0;
int lb = 0;
int lc = 0;
int ld = 0;
int le = 0;
int lf = 0;
int lg = 0;
int lh = 0;

void setup() {

  pinMode(buttonChange, INPUT);


  // set up the LCD number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("  Batch counter");
  lcd.setCursor(0, 1);
  lcd.print("      V1.0");
  delay(1000);
  lcd.clear();

  //sets display for eight 0's  for lower display:

  // lower display settings:
  lcd.setCursor(4, 1);
  lcd.print  (la);
  lcd.setCursor(5, 1);
  lcd.print (lb);
  lcd.setCursor(6, 1);
  lcd.print (lc);
  lcd.setCursor(7, 1);
  lcd.print (ld);
  lcd.setCursor(8, 1);
  lcd.print (le);
  lcd.setCursor(9, 1);
  lcd.print (lf);
  lcd.setCursor(10, 1);
  lcd.print(lg);
  lcd.setCursor(11, 1);
  lcd.print(lh);
  lcd.setCursor(11, 1);
  lcd.blink();

}
void loop() {




  // read the state of the pushbutton value:
  butChange = digitalRead(buttonChange);

  if (butChange != butChangeLBS) {


    if (butChange == HIGH);
    delay(50);
    lcd.blink();
    delay(50);
    {
      (butChangeCounter--);
      lcd.setCursor(butChangeCounter , 1);

      if (butChangeCounter <= 3)
      {
        butChangeCounter = 11;
      }

      lcd.blink();

    }


    delay(50);
  }

  butChangeLBS = butChange;
}
    if (butChange == HIGH);

It's pretty rare to see an if statement that properly ends with a ;. This is NOT one of those times.

Ok thanks for that but i still have the problem f the cursor jumping 2 characters

   if (butChange == HIGH)
    delay(50);
    lcd.blink();
    delay(50);

Which lines of code will be executed if butChange is HIGH ?
Which lines of code will be executed whatever the state of butChange ?

Here is the code Auto Formatted in the IDE which may give you a better idea if you don't know already.

    if (butChange == HIGH)
      delay(50);
    lcd.blink();
    delay(50);

My advice would be to always put curly brackets around code blocks even if there is only one command or statement to be executed. Furthermore, putting each curly bracket on its own line makes the code block even more obvious.

Hi Bob thanks

to answer the questions above if butChange is high it should move a blinking cursor 1 space to the left (but its going twice)
and lines of code will be executed whatever the state of butChange will ask for another button input to change that character

here is the rest of the code to show what i mean.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int buttonChange = 9;
const int buttonDigit = 6;
const int buttonEnter = 7;
const int buttonPause = 8;
int butChangeCounter = 11;
int butChange = 0;
int butChangeLBS = 0;
int butDigit = 0;
int butDigitCounter = 0;
int butDigitLBS = 0;
int butEnter = 0;
int butEnterLBS = 0;
int butPause = 0;
int butPauseLBS = 0;
int ua = 0;
int ub = 0;
int uc = 0;
int ud = 0;
int ue = 0;
int uf = 0;
int ug = 0;
int uh = 0;
int la = 0;
int lb = 0;
int lc = 0;
int ld = 0;
int le = 0;
int lf = 0;
int lg = 0;
int lh = 0;

void setup() {

pinMode(buttonChange, INPUT);
// pinMode(buttonDigit, INPUT);
// pinMode(buttonEnter, INPUT);
// pinMode(buttonPause, INPUT);
Serial.begin(9600);

// set up the LCD number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(" Batch counter");
lcd.setCursor(0, 1);
lcd.print(" V1.0");
delay(1000);
lcd.clear();

//sets display for eight 0's upper display and eight 0' for lower display:
// upper display settings:

lcd.setCursor(4, 0);
lcd.print (ua);
lcd.setCursor(5, 0);
lcd.print (ub);
lcd.setCursor(6, 0);
lcd.print (uc);
lcd.setCursor(7, 0);
lcd.print (ud);
lcd.setCursor(8, 0);
lcd.print (ue);
lcd.setCursor(9, 0);
lcd.print (uf);
lcd.setCursor(10, 0);
lcd.print(ug);
lcd.setCursor(11, 0);
lcd.print(uh);
lower display settings:
lcd.setCursor(4, 1);
lcd.print (la);
lcd.setCursor(5, 1);
lcd.print (lb);
lcd.setCursor(6, 1);
lcd.print (lc);
lcd.setCursor(7, 1);
lcd.print (ld);
lcd.setCursor(8, 1);
lcd.print (le);
lcd.setCursor(9, 1);
lcd.print (lf);
lcd.setCursor(10, 1);
lcd.print(lg);
lcd.setCursor(11, 1);
lcd.print(lh);
lcd.setCursor(11, 1);
lcd.blink();

}
void loop() {

// read the state of the pushbutton value:
butChange = digitalRead(buttonChange);
//butDigit = digitalRead(buttonDigit);
//butEnter = digitalRead(buttonEnter);
//butPause = digitalRead(buttonPause);

if (butChange != butChangeLBS) {

if (butChange == HIGH)

delay(50);
lcd.blink();
delay(50);

{
(butChangeCounter--);
lcd.setCursor(butChangeCounter , 1);

if (butChangeCounter <= 3) {

butChangeCounter = 11;

}

lcd.blink();
delay(50);
}

butChangeLBS = butChange;
}
}

// moves cursor on lower display to input a number at a desired location:
// Change digit value on position 11 lower display
if (butChangeCounter != butChangeCounterLBS) {
if ((butChangeCounter = 11) && (butDigit == HIGH)) {

(butDigit = lh + 1);
if (lh > 9)
lh = 0;
}

lcd.blink();
delay(50);

}

// Change digit value on position 10 lower display

// moves cursor on lower display to input a number at a desired location:

// Change digit value on position 10 lower display
if (butDigit != butDigitLBS) {

if ((butChangeCounter = 10) && (butDigit == HIGH))
(butDigit = lg++);

//if (butDigit == HIGH){

if (lg > 9)
lg = 0;
delay(50);

lcd.blink();
delay(50);
}

// Change digit value on position 9 lower display

if ((butChangeCounter = 9) && (butDigit == HIGH)) (butDigit = lf++); {
// if (butDigit == HIGH){

if (lf > 9)
lf = 0;
delay(50);

lcd.blink();
delay(50);
}

// Change digit value on position 8 lower display

if ((butChangeCounter = 8) && (butDigit == HIGH)) {
// if (butdigit == HIGH){
(butDigit = le++);
if (le > 9)
le = 0;
delay(50);

lcd.blink();
delay(50);
}
// Change digit value on position 7 lower display

if ((butChangeCounter = 7) && (butDigit == HIGH)) {
// if (butDigit == HIGH){

(butDigit = ld++);
if (ld > 9)
ld = 0;
delay(50);

lcd.blink();
delay(50);

}

// Change digit value on position 6 lower display

if ((butChangeCounter = 6) && (butDigit == HIGH)) {
// if (buttonState2 == HIGH){

(butDigit = lc++);
if (lc > 9)
lc = 0;
delay(50);

lcd.blink();
delay(50);

}

// Change digit value on position 5 lower display

if ((butChangeCounter = 5) && (butDigit == HIGH)) (butDigit = lb++); {
// if (butDigit == HIGH){

if (lb > 9)
lb = 0;
delay(50);

lcd.blink();
delay(50);

}

// Change digit value on position 4 lower display

if ((butChangeCounter = 4) && (butDigit == HIGH)) {
//if (buttonState2 == HIGH){

(butDigit = la++);
if (la > 9)
la = 0;
delay(50);

lcd.blink();
delay(50);

}
}
}

Hi,

Do yourself a favor and get acquainted with arrays.

Jacques

  if ((butChangeCounter = 11) && (butDigit == HIGH)) {

One right; one wrong. Same mistake lots of places.

to answer the questions above if butChange is high it should move a blinking cursor 1 space to the left (but its going twice)
and lines of code will be executed whatever the state of butChange will ask for another button input to change that character

Actually I don't think that you did answer the question. To me

    if (butChange == HIGH)
      delay(50);
    lcd.blink();
    delay(50);

says "if butChange is HIGH delay() for 50 milliseconds then blink the LCD and delay() for another 50 milliseconds. If butChange is LOWdelay() then blink the LCD and delay() for 50 milliseconds."

So, in the section of code above all that taking butChange HIGH does is to add a delay() of 50 milliseconds. Is that what you intended ?

This section of code

  if (butChange != butChangeLBS)
  {
    if (butChange == HIGH)
      delay(50);
    lcd.blink();
    delay(50);
    {
      (butChangeCounter--);
      lcd.setCursor(butChangeCounter , 1);
      if (butChangeCounter <= 3)
      {
        butChangeCounter = 11;
      }
      lcd.blink();
    }
    delay(50);
  }

will be executed when the change button is pressed and when it is released, which sounds like it could be the cause of your double cursor move.

Hi Bob thanks for the advice removing the delays did cure the problem and i know i have a long way to go.

AndySC:
Hi Bob thanks for the advice removing the delays did cure the problem and i know i have a long way to go.

I don't see how removing the delay()s would cure it. I would be interested in seeing your revised, working code.

This is the revised code

// read the state of the pushbutton value:
butChange = digitalRead(buttonChange);
//butDigit = digitalRead(buttonDigit);
//butEnter = digitalRead(buttonEnter);
//butPause = digitalRead(buttonPause);

if (butChange != butChangeLBS) {
if (butChange == HIGH)
{
(butChangeCounter--);
lcd.setCursor(butChangeCounter , 1);
lcd.blink();
}
if (butChangeCounter <= 4)

butChangeCounter = 12;
lcd.blink();
{
butChangeLBS = butChange;

}
}
}

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]