Counter help

Hi all,
I found this code:

/*
Project Title: Switch Counter using LCD and Increment and Decrement Buttons
Link: www.newtechandme.com
 
The circuit:
 * LCD RS pin to digital pin 13
 * LCD Enable pin to digital pin 12
 * LCD D4 pin to digital pin 11
 * LCD D5 pin to digital pin 10
 * LCD D6 pin to digital pin 9
 * LCD D7 pin to digital pin 8
 * LCD R/W pin to ground
 * Increment button to digital pin 2
 * Decrement button to digital pin 3
*/
// include the library code:
#include <LiquidCrystal.h>
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
 
//Set buttons pin numbers
byte incrementButton = 2;
byte decrementButton = 3;
 
//Global variables
int incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
int decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
int counter = 0; //variable that will store the count
int lastIncrementState = 0;
int lastDecrementState = 0;
 
void setup()
{
   pinMode(incrementButton, INPUT); //set incrementButton as INPUT
   pinMode(decrementButton, INPUT); //set decrementButton as INPUT
   lcd.begin(16, 2); //set up the LCD's number of columns and rows:
   lcd.print("NewTechandMe.com"); //print a message on the LCD
}//end of setup
 
void loop()
{
   lcd.setCursor(0, 1);
   incrementState = digitalRead(incrementButton); //read the increment button state
   decrementState = digitalRead(decrementButton); //read the decrement button state
   if(incrementState != lastIncrementState) //compare increment button state to its last state
   {
      if(incrementState == LOW)//increment button is pressed
      {
         counter = counter + 1; //increment the counter
         lcd.print(counter); //print it on serial monitor
         lcd.print("                "); //16 blank spaces
         delay(20); //debounce delay
      }
   }
   lastIncrementState = incrementState;
 
   if(decrementState != lastDecrementState)//compare decrement state to its lastState
   {
      if(decrementState == LOW)//decrement button is pressed
      {
         counter = counter - 1; //decrement the counter
         lcd.print(counter); //print it on serial monitor
         lcd.print("                "); //16 blank spaces
         delay(20); //debounce delay
      }
   }
   lastDecrementState = decrementState;
}//end of loop

It seems to work 8), but i would like it to do a few different things:
I would like it to count up to 3 then reset back to 0, (0-1-2-3) use only 1 button (known as filter)
and print to lcd 1Khz,1.5Khz,2.0Khz,2.5Khz overwriting the 1st.
Also have 4 outputs going high one at a time, to drive a relay driver, prob a transistor to 12v out.

Not to good at coding, but trying to learn, so need some pointers.

Howard

I would like it to count up to 3 then reset back to 0, (0-1-2-3) use only 1 button (known as filter)

All you need to do is add a test for counter going over 3. If that happens, set counter to 0.

and print to lcd 1Khz,1.5Khz,2.0Khz,2.5Khz overwriting the 1st.

Equally trivial with a switch statement and 4 cases.

Also have 4 outputs going high one at a time, to drive a relay driver, prob a transistor to 12v out.

You can do that in the same switch statement's cases.

Hi all,
Well i found this sketch and modded it
and it works ok, but on power up the display is blank
and i wanted it to show the first case :astonished:

//ssb audio filter button

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

// set up a constant for the switchPin
const int switchPin = 2;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

int buttonPushCounter = -1;   // counter for the number of button presses

void setup() {
  // set up the number of columns and rows on the LCD 
  lcd.begin(16, 2);

  // set up the switch pin as an input
  pinMode(switchPin,INPUT);

  lcd.setCursor(0, 0);

  //Setup Filter Outputs
  pinMode(3, OUTPUT);  //Initiates 1Khz pin 
  pinMode(4, OUTPUT);  //Initiates 1.5Khz pin 
  pinMode(5, OUTPUT);  //Initiates 2.0Khz pin
  pinMode(6, OUTPUT);  //Initiates 2.5Khz pin
}

void loop() {
  // check the status of the switch
  switchState = digitalRead(switchPin);

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) {

    if (switchState == LOW) {

      buttonPushCounter ++;
      buttonPushCounter %= 4;
      // clean up the screen before printing a new reply
      lcd.clear();

      switch (buttonPushCounter)
      {

      case 0:
        lcd.print("1Khz");
        //delay(3000);
        digitalWrite(3, HIGH);  //Output on
        digitalWrite(4, LOW);   //Output off
        //delay(1000);
        digitalWrite(5, LOW);   //Output off
        digitalWrite(6, LOW);   //Output off

        break;

      case 1:
        lcd.print("1.5Khz");
        //delay(3000);
        digitalWrite(3, LOW);   //Output off
        digitalWrite(4, HIGH);  //Output on
        // delay(1000);
        digitalWrite(5, LOW);   //Output off
        digitalWrite(6, LOW);   //Output off

        break;

      case 2:
        lcd.print("2.0Khz");
        //delay(3000);
        digitalWrite(3, LOW);  //Output off
        digitalWrite(4, LOW);  //Output on
        // delay(1000);
        digitalWrite(5, HIGH);  //Output off
        digitalWrite(6, LOW);   //Output off

        break;

      case 3:
        lcd.print("2.5Khz");
        //delay(3000);
        digitalWrite(3, LOW);  //Output off
        digitalWrite(4, LOW);  //Output on
        // delay(1000);
        digitalWrite(5, LOW);  //Output off
        digitalWrite(6, HIGH);   //Output off

        break;

      }
    }
  }
  // save the current switch state as the last state 
  prevSwitchState = switchState;
}

Howard

So, make it display the default state.

Help with that ??

Howard

Put it in setup

Put it in setup

That's one way. The other is to move the switch statement outside of the if() block that checks for a switch transition (to pressed).

That’s one way. The other is to move the switch statement outside of the if() block that checks for a switch transition (to pressed).

Is that line 37 of the code below ?, if so where do i place it ?

//ssb audio filter button

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

const int switchPin = 2;  // set up a constant for the switchPin

int switchState = 0;  // variable to hold the value of the switchPin

int prevSwitchState = 0;  // variable to hold previous value of the switchpin

int buttonPushCounter = -1;   // counter for the number of button presses

void setup() {
  lcd.begin(16, 2);  // set up the number of columns and rows on the LCD

  pinMode(switchPin,INPUT);  // set up the switch pin as an input

  lcd.setCursor(0, 0);

  //Setup Filter Outputs
  pinMode(4, OUTPUT);  //Initiates off pin 
  pinMode(5, OUTPUT);  //Initiates 1.5Khz pin 
  pinMode(6, OUTPUT);  //Initiates 2.0Khz pin
  pinMode(7, OUTPUT);  //Initiates 2.5Khz pin
}

void loop() {
  // check the status of the switch
  switchState = digitalRead(switchPin);

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) {

    if (switchState == LOW) {

      buttonPushCounter ++;
      buttonPushCounter %= 4;

      lcd.clear();  // clean up the screen before printing

      switch (buttonPushCounter)
      {

      case 0:
        lcd.print("Off");
        //delay(3000);
        digitalWrite(4, HIGH);  //Output on
        digitalWrite(5, LOW);   //Output off
        //delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, LOW);   //Output off

        break;

      case 1:
        lcd.print("1.5Khz");
        //delay(3000);
        digitalWrite(4, LOW);   //Output off
        digitalWrite(5, HIGH);  //Output on
        // delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, LOW);   //Output off

        break;

      case 2:
        lcd.print("2.0Khz");
        //delay(3000);
        digitalWrite(4, LOW);  //Output off
        digitalWrite(5, LOW);  //Output on
        // delay(1000);
        digitalWrite(6, HIGH);  //Output off
        digitalWrite(7, LOW);   //Output off

        break;

      case 3:
        lcd.print("2.5Khz");
        //delay(3000);
        digitalWrite(4, LOW);  //Output off
        digitalWrite(5, LOW);  //Output on
        // delay(1000);
        digitalWrite(6, LOW);  //Output off
        digitalWrite(7, HIGH);   //Output off

        break;

      }
    }
  }
  prevSwitchState = switchState;  // save the current switch state as the last state
}

Howard

Hi Can someone please help me, i'am really struggling with this :0

Howard

The display of anything is conditional upon you changing the switch state. So, to display the default state, you have to remove that dependency, so either the end of setup, or the top of loop.

Sorry ,but i dont know which part you are talking about I'am a newbe when it comes to coding

Howard now starting to go BALD!!!

Look at the code inside "loop". Anything that ends up on the LCD can only be put there because switchState is LOW, and you only do that test because the current state is different to the previous state. So, if you want anything to appear on the LCD without having to press buttons, move the code that displays stuff outside those conditions.

I'm posting from my phone, so I won't be cutting and,pasting any code for you.

Sorry its not getting through to me, i have moved a few things from within the "loop" but i get compile errors, so i must be doing things wrong.

Howard

G7LQY: i get compile errors, so i must be doing things wrong.

Perhaps if you posted your code somebody might be able to advise how to correct the errors in it.

Heres my code:

//ssb audio filter button

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

const int switchPin = 2;  // set up a constant for the switchPin

int switchState = 0;  // variable to hold the value of the switchPin

int prevSwitchState = 0;  // variable to hold previous value of the switchpin

int buttonPushCounter = -1;   // counter for the number of button presses

void setup() {
  lcd.begin(16, 2);  // set up the number of columns and rows on the LCD

  pinMode(switchPin,INPUT);  // set up the switch pin as an input

  //Setup Filter Outputs
  pinMode(4, OUTPUT);  //Initiates off pin 
  pinMode(5, OUTPUT);  //Initiates 1.5Khz pin 
  pinMode(6, OUTPUT);  //Initiates 2.0Khz pin
  pinMode(7, OUTPUT);  //Initiates 2.5Khz pin
  
}
void loop() {
  
  // check the status of the switch
  switchState = digitalRead(switchPin);

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) {

    if (switchState == LOW) {

      buttonPushCounter ++;
      buttonPushCounter %= 4;
      
      lcd.clear();  // clean up the screen before printing
      lcd.setCursor(0, 0);
      switch (buttonPushCounter)
      {
      
      case 0:
        lcd.print("Off");
        //delay(3000);
        digitalWrite(4, HIGH);  //Output on
        digitalWrite(5, LOW);   //Output off
        //delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, LOW);   //Output off

        break;

      case 1:
        lcd.print("1.5Khz");
        //delay(3000);
        digitalWrite(4, LOW);   //Output off
        digitalWrite(5, HIGH);  //Output on
        // delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, LOW);   //Output off

        break;

      case 2:
        lcd.print("2.0Khz");
        //delay(3000);
        digitalWrite(4, LOW);  //Output off
        digitalWrite(5, LOW);  //Output off
        // delay(1000);
        digitalWrite(6, HIGH);  //Output on
        digitalWrite(7, LOW);   //Output off

        break;

      case 3:
        lcd.print("2.5Khz");
        //delay(3000);
        digitalWrite(4, LOW);   //Output off
        digitalWrite(5, LOW);   //Output off
        // delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, HIGH);  //Output on

        break;

      }
    }
  }
  prevSwitchState = switchState;  // save the current switch state as the last state
}

The thing is i don’t know what needs to be moved to where

Howard, now have no hair left!!

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) {

So, this is the start of the code that detects whether a switch has changed state. If you put the { on a new line, and used Tools + Auto Format, the { might move around. But, there will be one later in the code that lines up with it, and marks the end of the "if a switch state change occurred" block.

Inside that block, you have some code that does something useful when buttonPushCounter is 0, 1, 2, etc.

Move that code (assuming that you can recognize it) AFTER the end of the "if a switch state change occurred" block.

The code to do something useful when buttonPushCounter is 0, 1, 2, etc. should not rely on the switch becoming pressed. The only thing that should rely on the switch becoming pressed is incrementing buttonPushCounter.

Hi
To find the { at the end of the loop, i put mouse cursor at the { just below if (switchState != prevSwitchState), and found a { that was highlighted,
is that the end of the “if a switch state change occurred” block??

I don’t think that it is the case code ?
having probs recognising it.

Thanks
Howard

is that the end of the "if a switch state change occurred" block??

Yes.

The switch statement, and all the cases, goes AFTER that }.

Hi So from if (switchState == LOW) {

if (switchState == LOW) {
      buttonPushCounter ++;
      buttonPushCounter %= 4;
      lcd.clear();  // clean up the screen before printing
      lcd.setCursor(0, 0);
      switch (buttonPushCounter)
      {
      case 0:
        lcd.print("Off");
        //delay(3000);
        digitalWrite(4, HIGH);  //Output on
        digitalWrite(5, LOW);   //Output off
        //delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, LOW);   //Output off
        break;
      case 1:
        lcd.print("1.5Khz");
        //delay(3000);
        digitalWrite(4, LOW);   //Output off
        digitalWrite(5, HIGH);  //Output on
        // delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, LOW);   //Output off
        break;
      case 2:
        lcd.print("2.0Khz");
        //delay(3000);
        digitalWrite(4, LOW);  //Output off
        digitalWrite(5, LOW);  //Output off
        // delay(1000);
        digitalWrite(6, HIGH);  //Output on
        digitalWrite(7, LOW);   //Output off
        break;
      case 3:
        lcd.print("2.5Khz");
        //delay(3000);
        digitalWrite(4, LOW);   //Output off
        digitalWrite(5, LOW);   //Output off
        // delay(1000);
        digitalWrite(6, LOW);   //Output off
        digitalWrite(7, HIGH);  //Output on
        break;

to here last breakbreak;

go here

      }
    }
  }// this is the end of the "if a switch state change occurred" block??
  //go here ??
  prevSwitchState = switchState;  // save the current switch state as the last state
}

Thanks Howard

Heres the modded code:

//ssb audio filter button

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

const int switchPin = 2;  // set up a constant for the switchPin

int switchState = 0;  // variable to hold the value of the switchPin

int prevSwitchState = 0;  // variable to hold previous value of the switchpin

int buttonPushCounter = -1;   // counter for the number of button presses

void setup() {
  lcd.begin(16, 2);  // set up the number of columns and rows on the LCD

  pinMode(switchPin,INPUT);  // set up the switch pin as an input

  //Setup Filter Outputs
  pinMode(4, OUTPUT);  //Initiates off pin 
  pinMode(5, OUTPUT);  //Initiates 1.5Khz pin 
  pinMode(6, OUTPUT);  //Initiates 2.0Khz pin
  pinMode(7, OUTPUT);  //Initiates 2.5Khz pin
}

void loop() {
  // check the status of the switch
  switchState = digitalRead(switchPin);

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) 
  {

    if (switchState == LOW) {


    }
  }
}
buttonPushCounter ++;  //go here ??
buttonPushCounter %= 4;

lcd.clear();  // clean up the screen before printing
lcd.setCursor(0, 0);
switch (buttonPushCounter)
{
case 0:
  lcd.print("Off");
  //delay(3000);
  digitalWrite(4, HIGH);  //Output on
  digitalWrite(5, LOW);   //Output off
  //delay(1000);
  digitalWrite(6, LOW);   //Output off
  digitalWrite(7, LOW);   //Output off
  break;
case 1:
  lcd.print("1.5Khz");
  //delay(3000);
  digitalWrite(4, LOW);   //Output off
  digitalWrite(5, HIGH);  //Output on
  // delay(1000);
  digitalWrite(6, LOW);   //Output off
  digitalWrite(7, LOW);   //Output off
  break;
case 2:
  lcd.print("2.0Khz");
  //delay(3000);
  digitalWrite(4, LOW);  //Output off
  digitalWrite(5, LOW);  //Output off
  // delay(1000);
  digitalWrite(6, HIGH);  //Output on
  digitalWrite(7, LOW);   //Output off
  break;
case 3:
  lcd.print("2.5Khz");
  //delay(3000);
  digitalWrite(4, LOW);   //Output off
  digitalWrite(5, LOW);   //Output off
  // delay(1000);
  digitalWrite(6, LOW);   //Output off
  digitalWrite(7, HIGH);  //Output on
  break;//go here ??
  prevSwitchState = switchState;  // save the current switch state as the last state
}

But it has errors!!
(how do i copy\paste the error codes from the bottom window of the sketch to here ?)

Thanks
Howard