Using buttons to set values within a counter

HI folks. Wondering if someone might have some insight into a problem I am facing.

I have a counter that reads inputs on one pin (from a magnet on a wheel like on a bike speedometer). The wheel is lifting an object up and down so the counter allows me to see the height from Zero point (set using the reset button at the start of operation.

I have an alphanumeric display with 5 digits. Four for the footage count and one for the half foot value.
So far I am able to read the input, convert it to feet and calculate the half foot value and display those on my display. Using a second switch I am able to run the counter backwards.

I also have an alarm connected. I have the alarm set to start beeping at a set distance at each end of travel and after a further set distance the alarm sounds continuously and the display shows the word “STOP”.

With the reset button used for giving the zero start point and by pre-programming the end distance all works fine.

What I really need to do though is be able to use some more buttons to set parameters once the system is running. So I would like to have one push button programmed to set the top distance value once the device reaches the desired height and then to set that value as the top limit for the alarm (later I will be adding kill switches at the top and bottom limits).

If I can achieve that successfully then it would be great if I can also add two more buttons to set interim top and bottom limits within the maximum range (these would be for guidance and not have kill switches).

So I guess my question is - How do you get the button trigger to read the current distance value and then set that as a new value which doesn’t change until the button is pressed again. So far I kind of have it working but it doesn’t save the new limit value just keeps incrementing it.

My very scrappy code is here - pieced together from all sorts of sources as I am a complete newbie to all this and this is my first of several rather complex projects I need to solve.

#include <AlphaNumeric_Driver.h>  // Include AlphaNumeric Display Driver Library
#define NUMBER_OF_DISPLAYS 5  // This value is currently only used by the library for the clear() command.


int buttonPressCount;

const int  buttonPin = 4;    
const int  alarmPin = 7;
const int  directionPin = 3;
const int topLimitPin = 5; // to set top limit of shot
const int bottomLimitPin = 6; // to set bottom limit of shot


int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button
int distanceCounter = 0;
int directionState = 0;   //counter for the number of button presses
int lastdirectionState = 0;  

int fractional = 0; // gets remainder for increments less than a foot
int decimal = 0; // converts remainder to zero value or 0.5
//int val = 0;
//int previousVal = 0;
int topLimit = 300; //max height to run up to
int topWarning =300; // height below max for warning to start - suggest 10ft in real world
//int topLimitState = 0;
//int lastTopLimitState = 0;

int shotHigh = 0;
int shotLow = 0;

int SDIpin = 11;
int CLKpin = 13;
int LEpin = 10;
int OEpin = 9;

alphaNumeric myDisplay(SDIpin, CLKpin, LEpin, OEpin, NUMBER_OF_DISPLAYS);  // Create an instance of Alpha Numeric Displays

//char displayString[] = ("STOP");  // Static string to scroll on displays

void setup()  {
  pinMode(buttonPin, INPUT);  
  digitalWrite(alarmPin, LOW);
  pinMode(alarmPin, OUTPUT);
  digitalWrite(alarmPin, LOW);
  
  pinMode(directionPin, INPUT);
  digitalWrite(directionPin, LOW);
  
  pinMode(topLimitPin, INPUT);
  digitalWrite(topLimitPin, LOW);
  
   pinMode(bottomLimitPin, INPUT);
  digitalWrite(bottomLimitPin, LOW);
  
  Serial.begin(9600);  //initialize serial communication

  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
}

void loop(){
 
  while (digitalRead(topLimitPin) == HIGH) {
  SetTopLimit();
  

  
  if (distanceCounter >= 13 && distanceCounter <=17 || distanceCounter <= -1 && distanceCounter >=-2 || distanceCounter >= topWarning && distanceCounter <= topLimit){
  digitalWrite(alarmPin, HIGH);
  delay(1);
  digitalWrite(alarmPin, LOW);
      }      
 else if (distanceCounter >17 || distanceCounter <-2){
  
  digitalWrite(alarmPin, HIGH);
 }
 else digitalWrite(alarmPin, LOW);{
 }
  
      
      
      
      
      
      
      
      
      directionState = digitalRead(directionPin);
      //if (directionPin, LOW) {
 buttonState = digitalRead(buttonPin);  //read the pushbutton input pin
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH){
     if (directionState == LOW) {
       //if (buttonState == HIGH) {
            // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter--;
       }
      else 
      //if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    //if (buttonState == HIGH){
    // if (directionState == HIGH) {
      buttonPushCounter++;
      
      
      distanceCounter = buttonPushCounter / 6;
   
      
      Serial.print("number of button pushes:  ");
      //Serial.println(buttonPushCounter, DEC);
      Serial.println(distanceCounter, DEC);
      
      Serial.print("top limit:  ");
      //Serial.println(buttonPushCounter, DEC);
      Serial.println(topLimit, DEC);
      
      Serial.print("top warning:  ");
      //Serial.println(buttonPushCounter, DEC);
      Serial.println(topWarning, DEC);
      //updateDisplay(buttonPushCounter);  //function to update the display 'requires button press count'
      updateDisplay(distanceCounter);
      //updateDisplay(decimal);
      
    } 

  }
 
  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop
//lastdirectionState = directionState;
  

{
        topWarning = topLimit - 2;
  }
     }
     }
void SetTopLimit () {  
        topLimit = distanceCounter;
           }
     
    


void updateDisplay(int distanceCounter){
  
 
  
      //distanceCounter = buttonPushCounter / 6;
  
  /*digitalRead(topLimitPin);
  if (topLimitPin,HIGH) {
    topLimit = buttonPushCounter;
  } 
    else {
                topLimit = topLimit;
        }  
     
   digitalWrite(topLimitPin, LOW);
  
   
    topWarning = topLimit - 2;
  */
  
  
  fractional = (buttonPushCounter % 6);
             if (fractional < 3 || fractional > 3){
        (decimal = 0);
        }
        else (decimal = 5);
  String decString = String(decimal);  //changes integer to a string
char displayChars[1];  


  if (distanceCounter >17 || distanceCounter <-2){
    myDisplay.print('S');
    myDisplay.print('T');
    myDisplay.print('O');
    myDisplay.print('P');
    myDisplay.print('-');
  }
    else{
String intString = String(distanceCounter);  //changes integer to a string
char displayChars[3];  //create array to hold the four numbers
int stringLength = intString.length();  //get length of the string
//the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
//and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
if (stringLength == 1){
  //myDisplay.print(0);
  myDisplay.print('_');
  myDisplay.print('_');
  myDisplay.print('_');
  //myDisplay.print(0);
  myDisplay.print(intString.charAt(0));
  myDisplay.print(decString.charAt(0));
}else if(stringLength == 2){
  myDisplay.print('_');
  myDisplay.print('_');
  //myDisplay.print(0);
  myDisplay.print(intString.charAt(0));
  myDisplay.print(intString.charAt(1));
  myDisplay.print(decString.charAt(0));
}else if(stringLength == 3){
  myDisplay.print('_');
  myDisplay.print(intString.charAt(0));
  myDisplay.print(intString.charAt(1));
  myDisplay.print(intString.charAt(2));
  myDisplay.print(decString.charAt(0));
}


delay(100); //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'
 

}
        }
  if (distanceCounter >= 13 && distanceCounter <=17 || distanceCounter <= -1 && distanceCounter >=-2 || distanceCounter >= topWarning && distanceCounter <= topLimit){

This needs some parentheses to make it what it is doing. It needs to be split into to lines, for readibility.

  if ((distanceCounter >= 13 && distanceCounter <=17) ||
      (distanceCounter <= -1 && distanceCounter >=-2) ||
      (distanceCounter >= topWarning && distanceCounter <= topLimit))
  {

Getting rid of the useless blank lines, putting all { on separate lines, and using the Tools + Auto Format menu item would greatly improve the readability of your code.

Removing useless curly braces will help, too.
{
topWarning = topLimit - 2;
}
These are doing nothing but saying that you don’t understand where curly braces are needed.
The random indenting makes it nearly impossible to read the code and follow the logic.

Some functions called from loop(), like TestTopLimit(), would also help.

Thanks for the tips.

This is the first arduino project I've started on - driven by need for a solution on a project.

You're right I am learning the scripting and formatting as I go by trial and error so you have to forgive some of the layout errors. Many due to trying to incorporate snippets of code from various different sketches on the web and then doctoring them with my own code to do what I want and relying on the compile errors to fix things pretty much by guess work!

It's all good fun though and a great chllenege.

So all help and tips are appreciated thanks.

OK so I tried the auto format and tidied up the code a bit.

I also tried to put in a function to set the topLimit value to whatever the distance count is at so that at any given point a button press will select the current distance as the topLimit. However I can’t seem to get it to compile. Any suggestions as to the error. The compile error seems to suggest it is something in the void SetTopLimit function as everything else was working before.

Here is the cleaner version of the code. Of course the sections may not be in the best order as I mentioned I merely cut and paste to areas where I could get things to work.

#include <AlphaNumeric_Driver.h>  // Include AlphaNumeric Display Driver Library
#define NUMBER_OF_DISPLAYS 5  // This value is currently only used by the library for the clear() command.

int buttonPressCount;

const int killSwitchPin = 1;
const int killResetPin = 2;
const int directionPin = 3;
const int buttonPin = 4;    
const int topLimitPin = 5; // to set top limit of shot
const int bottomLimitPin = 6; // to set bottom limit of shot
const int alarmPin = 7;

const int OEpin = 9;
const int LEpin = 10;
const int SDIpin = 11;
const int CLKpin = 12;

int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button
int distanceCounter = 0;
int directionState = 0;   //counter for the number of button presses
int lastdirectionState = 0;  
int fractional = 0; // gets remainder for increments less than a foot
int decimal = 0; // converts remainder to zero value or 0.5
//int val = 0;
//int previousVal = 0;
int topLimit = 300; //max height to run up to
int topWarning =298; // height below max for warning to start - suggest 10ft in real world
//int topLimitState = 0;
//int lastTopLimitState = 0;

int shotHigh = 0;
int shotLow = 0;






alphaNumeric myDisplay(SDIpin, CLKpin, LEpin, OEpin, NUMBER_OF_DISPLAYS);  // Create an instance of Alpha Numeric Displays

//char displayString[] = ("STOP");  // Static string to scroll on displays

void setup()  {
  pinMode(buttonPin, INPUT);  
  digitalWrite(alarmPin, LOW);
  
  pinMode(alarmPin, OUTPUT);
  digitalWrite(alarmPin, LOW);
  
  pinMode(directionPin, INPUT);
  digitalWrite(directionPin, LOW);
  
  pinMode(topLimitPin, INPUT);
  digitalWrite(topLimitPin, LOW);
  
  pinMode(bottomLimitPin, INPUT);
  digitalWrite(bottomLimitPin, LOW);
  
  pinMode(killResetPin, INPUT);
  digitalWrite(killResetPin, LOW);
  
  pinMode(killSwitchPin, OUTPUT);
  digitalWrite(killSwitchPin, LOW);
  
  Serial.begin(9600);  //initialize serial communication

  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
}

void loop(){
 
  //while (digitalRead(topLimitPin) == HIGH) {
  //SetTopLimit();
  //}
  
void SetTopLimit();{
   digitalRead(topLimitPin);
   if (topLimitPin,HIGH) {
    topLimit = buttonPushCounter;
  } 
    else {
                topLimit = topLimit;
        }  
     
   digitalWrite(topLimitPin, LOW);
}
   
    topWarning = topLimit - 2;

  if ((distanceCounter <= -1 && distanceCounter >=-2) || 
      (distanceCounter >= topWarning && distanceCounter <topLimit))
  {
  digitalWrite(alarmPin, HIGH);
  delay(1);
  digitalWrite(alarmPin, LOW);
  } 
  
 if ((distanceCounter >=topLimit) ||
     (distanceCounter <-2)){
  
  digitalWrite(alarmPin, HIGH);
 }
  else digitalWrite(alarmPin, LOW);
  }

 directionState = digitalRead(directionPin);
      //if (directionPin, LOW) {
 buttonState = digitalRead(buttonPin);  //read the pushbutton input pin
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH){
     if (directionState == LOW) {
       //if (buttonState == HIGH) {
            // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter--;
       }
      else 
      //if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    //if (buttonState == HIGH){
    // if (directionState == HIGH) {
      buttonPushCounter++;
      
      
      distanceCounter = buttonPushCounter / 6;
   
      
      
      Serial.print("number of button pushes:  ");
      //Serial.println(buttonPushCounter, DEC);
      Serial.println(distanceCounter, DEC);
      
      Serial.print("top limit:  ");
      //Serial.println(buttonPushCounter, DEC);
      Serial.println(topLimit, DEC);
      
      Serial.print("top warning:  ");
      //Serial.println(buttonPushCounter, DEC);
      Serial.println(topWarning, DEC);
      //updateDisplay(buttonPushCounter);  //function to update the display 'requires button press count'
      updateDisplay(distanceCounter);
      //updateDisplay(decimal);
      //SetTopLimit(distanceCounter);
    } 

  }
 
  lastButtonState = buttonState;  // save the current state as the last state, for next time through the loop
//lastdirectionState = directionState;

void updateDisplay(int distanceCounter){
    
  fractional = (buttonPushCounter % 6);
             if (fractional < 3 || fractional > 3){
        (decimal = 0);
        }
        else (decimal = 5);
  String decString = String(decimal);  //changes integer to a string
char displayChars[1];  

  if (distanceCounter >17 || distanceCounter <-2){
    myDisplay.print('S');
    myDisplay.print('T');
    myDisplay.print('O');
    myDisplay.print('P');
    myDisplay.print('-');
  }
    else{
String intString = String(distanceCounter);  //changes integer to a string
char displayChars[3];  //create array to hold the four numbers
int stringLength = intString.length();  //get length of the string
//the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
//and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
if (stringLength == 1){
  //myDisplay.print(0);
  myDisplay.print('_');
  myDisplay.print('_');
  myDisplay.print('_');
  //myDisplay.print(0);
  myDisplay.print(intString.charAt(0));
  myDisplay.print(decString.charAt(0));
}else if(stringLength == 2){
  myDisplay.print('_');
  myDisplay.print('_');
  //myDisplay.print(0);
  myDisplay.print(intString.charAt(0));
  myDisplay.print(intString.charAt(1));
  myDisplay.print(decString.charAt(0));
}else if(stringLength == 3){
  myDisplay.print('_');
  myDisplay.print(intString.charAt(0));
  myDisplay.print(intString.charAt(1));
  myDisplay.print(intString.charAt(2));
  myDisplay.print(decString.charAt(0));
}
delay(100); //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'
}

thanks for any help on this.

Here is the cleaner version of the code.

Doesn't look any better, to me. Still random indents all over the place.

What is this supposed to be? If it is an attempt at a function declaration, why is there is a semicolon on the first line? Why is the function definition inside loop? Functions can not be defined inside another function. Called, yes. Defined, no!

void SetTopLimit();{
   digitalRead(topLimitPin);
   if (topLimitPin,HIGH) {
    topLimit = buttonPushCounter;
  } 
    else {
                topLimit = topLimit;
        }  
     
   digitalWrite(topLimitPin, LOW);
}

if(topLimitPin, HIGH){? What? Randomly splattering keywords around is not how to write code.

Start with some comments. // Read the state of a switch // If the switch is pressed, do this // Otherwise, do this

Then, put some code in that (attempts to) do what the comment says. If you have problems, and post code, at least we can tell what you were trying to do.

OK, Thanks for the pointers. Having never written code before or done any computing in highschool - it was a long time ago before they were invented! - it is a steep learning curve but all help and guidance is greatly appreciated.

So I’ve cleaned up the code some more, though no doubt still not in official formatting. I have also managed to get it all to compile and all to work except for the function reading and changing the topLimit variable when the button is pressed. But the existing variable from start up is working through the code fine so I am guessing it is either the scripting of the function or the placement of the function. I have an LED connected to the button in question so know tat it is wired OK and responding.

Also unable to get the kill switch to fire once only. I am guessing I can make some if / else statements relating to the killreset button to handle that unless there is a better way.

Here’s the first half of the code. I’ll post the second half in a separate post.

#include <AlphaNumeric_Driver.h>  // Include AlphaNumeric Display Driver Library
#define NUMBER_OF_DISPLAYS 5  // This value is currently only used by the library for the clear() command.

int buttonPressCount;

const int killSwitchPin = 8;  //pin to activate relay to kill external system power
const int killResetPin = 2; // buttonto activate killSwitchPin to reactivate power switch
const int directionPin = 3; // switch to register direction change
const int buttonPin = 4;    // sensor input
const int topLimitPin = 5; //  button to set top limit of movement
const int bottomLimitPin = 6; // to set lower limit indicator 
const int alarmPin = 7; // buzzer

int OEpin = 9;  //Alphanumeric display pins
int LEpin = 10;  //Alphanumeric display pins
int SDIpin = 11;  //Alphanumeric display pins
int CLKpin = 12;  //Alphanumeric display pins

int buttonPushCounter = 0;   //counter for the number of button presses
int buttonState = 0;         //current state of the button
int lastButtonState = 0;     //previous state of the button
int distanceCounter = 0;     // counts the distance in feet
int directionState = 0;   //counter for the number of button presses
int lastdirectionState = 0;  
int fractional = 0; // gets remainder from distance counter footage calculation for increments less than a foot
int decimal = 0; // converts remainder to zero value or 0.5
//int val = 0;
//int previousVal = 0;
int topLimit = 18; //max height to run up to
int topWarning =16; // height below max for warning to start - suggest 10ft in real world
int kill = 22;  // height at which kill switch will activate
//int topLimitState = 0;
//int lastTopLimitState = 0;

int shotHigh = 0; //not included yet but would be similar function as topLimit, setting an additional high value that would trigger alarm
int shotLow = 0; // value for low height to trigger alarm

alphaNumeric myDisplay(SDIpin, CLKpin, LEpin, OEpin, NUMBER_OF_DISPLAYS);  // Create an instance of Alpha Numeric Displays


void setup()  {
  
  pinMode(killSwitchPin, OUTPUT);
  digitalWrite(killSwitchPin, HIGH);
  
  pinMode(killResetPin, INPUT);
  digitalWrite(killResetPin, LOW);
  
  pinMode(directionPin, INPUT);
  digitalWrite(directionPin, LOW);
  
  pinMode(buttonPin, INPUT);  
  
  pinMode(topLimitPin, INPUT);
  digitalWrite(topLimitPin, LOW);
  
  pinMode(bottomLimitPin, INPUT);
  digitalWrite(bottomLimitPin, LOW);
  
  pinMode(alarmPin, OUTPUT);
  digitalWrite(alarmPin, LOW);
  
  
    
  Serial.begin(9600);  //initialize serial communication

  myDisplay.print(0); //sets the display to zero initially
  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
  myDisplay.print(0);
}

part 2 of the code from my last post

void loop(){
  
  if ((distanceCounter <= -1 && distanceCounter >=-2) ||                                  // if the ditance counter is between -1 and -2        
      (distanceCounter >= topWarning && distanceCounter <topLimit))                 //using the values set when the toplimit button is pressed
      //(distanceCounter >= topWarning && distanceCounter <topLimit))
  {
  digitalWrite(alarmPin, HIGH);                                                                     // turn the alarm on & then off
  delay(1);
  digitalWrite(alarmPin, LOW);
  } 
  else if (distanceCounter >=topLimit || distanceCounter <-2)                            // if the distance counter is above 15 or below 2 
  {   
  digitalWrite(alarmPin, HIGH);                                                                     // turn the alarm on continuously
  }
  else digitalWrite(alarmPin, LOW);{
  } 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// how do I make this section trigger the relay switch once only and then have to be reset using the killReset button? DO I just have to use an if statement regarding the state of the killReset button or is there a more //elegant way?

  if (distanceCounter >=kill) {                                       // if distance exceeds maximum height kill external device power (not arduino power - arduino power is independent of device)
  digitalWrite(killSwitchPin, LOW);
  delay(10);
  digitalWrite(killSwitchPin, HIGH);
  }
  else 
  digitalWrite(killSwitchPin, HIGH);{
  }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 directionState = digitalRead(directionPin);
                                                                                        //if (directionPin, LOW) {
 buttonState = digitalRead(buttonPin);                                    //read the pushbutton input pin
                                                                                        // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
                                                                                       // if the state has changed, increment the counter
    if (buttonState == HIGH){
     if (directionState == LOW) {
                                                                                       //if (buttonState == HIGH) {
                                                                                       // if the current state is HIGH then the button
                                                                                       // went from off to on:
      buttonPushCounter--;
       }
      else 
                                                                                           //if (buttonState != lastButtonState) {
                                                                                           // if the state has changed, increment the counter
                                                                                            //if (buttonState == HIGH){
                                                                                           // if (directionState == HIGH) {
      buttonPushCounter++;
      
      distanceCounter = buttonPushCounter / 6;                             // converts buttonpushcounter to feet
   
      topWarning = topLimit - 2;                                                 // set the new value for topWarning to the value for topLimit minus 2
      kill = topLimit + 2;                                                              // set the new value for kill to toplimit plus 2
      
      Serial.print("number of button pushes:  ");
                                                                                               //Serial.println(buttonPushCounter, DEC);
      Serial.println(distanceCounter, DEC);
      
      Serial.print("top limit:  ");
                                                                                                 //Serial.println(buttonPushCounter, DEC);
      Serial.println(topLimit, DEC);
      
      Serial.print("top warning:  ");
                                                                                                   //Serial.println(buttonPushCounter, DEC);
      Serial.println(topWarning, DEC);
      //updateDisplay(buttonPushCounter);                                           //function to update the display 'requires button press count'
      updateDisplay(distanceCounter);
      //updateDisplay(decimal);
      //SetTopLimit(distanceCounter);
    } 
}

  lastButtonState = buttonState;                                               // save the current state as the last state, for next time through the loop
                                                 
}
     
void updateDisplay(int distanceCounter){
    
  fractional = (buttonPushCounter % 6);                                                       // finds remainder from counter and calculates value for 0.5 feet
  if (fractional < 3 || fractional > 3){
  (decimal = 0);
  }
  else (decimal = 5);
  
  String decString = String(decimal);                                                       //changes integer to a string
char displayChars[1];  

  if (distanceCounter >topLimit || distanceCounter <-2){                         // displays stop on the display if limits are exceeded
    myDisplay.print('S');
    myDisplay.print('T');
    myDisplay.print('O');
    myDisplay.print('P');
    myDisplay.print('-');
  }
    else{
String intString = String(distanceCounter);                         //changes integer to a string
char displayChars[3];                                                     //create array to hold the four numbers
int stringLength = intString.length();                                //get length of the string
                                                                                 //the following will determine if the button press count variable has 1, 2, 3, or 4 numbers in it
                                                                                 //and will fill the empty spaces with '0'. so if the button press count variable is '29' it will end up being '0029':
if (stringLength == 1){
  myDisplay.print('_');
  myDisplay.print('_');
  myDisplay.print('_');
  myDisplay.print(intString.charAt(0));
  myDisplay.print(decString.charAt(0));
}else if(stringLength == 2){
  myDisplay.print('_');
  myDisplay.print('_');
  myDisplay.print(intString.charAt(0));
  myDisplay.print(intString.charAt(1));
  myDisplay.print(decString.charAt(0));
}else if(stringLength == 3){
  myDisplay.print('_');
  myDisplay.print(intString.charAt(0));
  myDisplay.print(intString.charAt(1));
  myDisplay.print(intString.charAt(2));
  myDisplay.print(decString.charAt(0));
}
delay(100);                                                                    //this will make it so you don't get double counts. you could also use this to avoid someone pressing the button repeatedly 'for fun!'
}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //  so this is the code I want to get to take the distance value at the moment the topLimit button is pressed 
  //   and then use that value for the topLimit and also for calculating the topWarning. 
  //   I don't want this value to change again until the button is pressed again.
  //   Ideally I would have three of these functions (with appropriately named variables) allowing the setting of three distinct point at which alarm would trigger
  //
  //    I have the variables working throughout the codenow so the only thing not happening is the button press resulting in changin the toplimit value.
  //I expect this function is in the wrong place ro do I need to use buttonState instead of just digitalRead?
  
 //while (digitalRead(topLimitPin) == HIGH) {                                       // when the top limit button is pressed run the SetTopLimit function
 //SetTopLimit();
  
void SetTopLimit(){                                                   //When the toplimit button is pressed read the value of the distance counter and set that as the new value for the topLimit.                                                                                                                                                                               
   digitalRead(topLimitPin);                                        //Don't change that value again until button is pressed again 
   if (topLimitPin,HIGH) {
   topLimit = distanceCounter;
   } 
   else {
   topLimit = topLimit;                                                 //if the top limit pin is NOT pressed then do not change the current topLimit value
   }  
   digitalWrite(topLimitPin, LOW);
   }
   
    

  ///////////////////////////////////////////////////////////////////////////////////////////////////////

cheers