Tactile Switch not activating after Press

Hi there,

I am using an Arduino to display some parameters from a solar inverter via modbus. I do not have the solar system set up to practise that so I am trying to sort out the rest of my Arduino interface things before hand. I am fairly new to Arduino coding but not struggling too much, I have this code that I procured and have edited to fit what I need:

#include <TimerOne.h>
//the pins of 4-digit 7-segment display attach to pin2-13 respectively 
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;
long n = 50;// n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999. 
int x = 100;
int del = 5;//Set del as 5; the value is the degree of fine tuning for the clock
int count = 0;//Set count=0. Here count is a count value that increases by 1 every 0.1 second, which means 1 second is counted when the value is 10

const int buttonPin = 32;
int buttonState = 0;
void setup()
{
  //set all the pins of the LED display as output
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  //Timer1.attachInterrupt( add ); // attach the service routine here
}
/***************************************/ 
void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    Serial.println(88);
  }
  else{
  SOC();
  }
}

void SOC()
{
  for(int l = 0; l<100;++l){
  clearLEDs();//clear the 7-segment display screen
  pickDigit(0);//Light up 7-segment display d1
  S();// State
  delay(del);//delay 5ms

  clearLEDs();//clear the 7-segment display screen
  pickDigit(1);//Light up 7-segment display d2
  O();// of
  delay(del);//delay 5ms

  clearLEDs();//clear the 7-segment display screen
  pickDigit(2);//Light up 7-segment display d3
  C();//Charge
  delay(del);//delay 5ms
  }
  clearLEDs();
  delay(500);
  for (int l = 0; l<500;++l){
  // clearLEDs();//clear the 7-segment display screen
  // pickDigit(0);//Light up 7-segment display d1
  // pickNumber((n/1000));// get the value of thousand
  // delay(del);//delay 5ms

  // clearLEDs();//clear the 7-segment display screen
  // pickDigit(1);//Light up 7-segment display d2
  // pickNumber((n%1000)/100);// get the value of hundred
  // delay(del);//delay 5ms

  clearLEDs();//clear the 7-segment display screen
  pickDigit(0);//Light up 7-segment display d3
  pickNumber(n%100/10);//get the value of ten
  delay(del);//delay 5ms

  clearLEDs();//clear the 7-segment display screen
  pickDigit(1);//Light up 7-segment display d4
  pickNumber(n%10);//Get the value of single digit
  delay(del);//delay 5ms

  // clearLEDs();//clear the 7-segment display screen
  // pickDigit(3);//Light up 7-segment display d3
  // P();//Charge
  // delay(del);//delay 5ms
  }
  clearLEDs();
  delay(250);
}

void pickDigit(int x) //light up a 7-segment display
{
  //The 7-segment LED display is a common-cathode one. So also use digitalWrite to  set d1 as high and the LED will go out
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);

  switch(x)
  {
    case 0: 
    digitalWrite(d1, LOW);//Light d1 up 
    break;
    case 1: 
    digitalWrite(d2, LOW); //Light d2 up 
    break;
    case 2: 
    digitalWrite(d3, LOW); //Light d3 up 
    break;
    default: 
    digitalWrite(d4, LOW); //Light d4 up 
    break;
  }
}

//The function is to control the 7-segment LED display to display numbers. Here x is the number to be displayed. It is an integer from 0 to 9 
void pickNumber(int x)
{
  switch(x)
  {
    default: 
    zero(); 
    break;
    case 1: 
    one(); 
    break;
    case 2: 
    two(); 
    break;
    case 3: 
    three(); 
    break;
    case 4: 
    four(); 
    break;
    case 5: 
    five(); 
    break;
    case 6: 
    six(); 
    break;
    case 7: 
    seven(); 
    break;
    case 8: 
    eight(); 
    break;
    case 9: 
    nine(); 
    break;
  }
} 
void clearLEDs() //clear the 7-segment display screen
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void zero() //the 7-segment led display 0
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void one() //the 7-segment led display 1
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void two() //the 7-segment led display 2
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void three() //the 7-segment led display 3
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void four() //the 7-segment led display 4
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void five() //the 7-segment led display 5
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void six() //the 7-segment led display 6
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void seven() //the 7-segment led display 7
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void eight() //the 7-segment led display 8
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void nine() //the 7-segment led display 9
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void L(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void S(){
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void O(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void C(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void P(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
/*******************************************/
// void add()
// {
//   // Toggle LED
//   count ++;
//   if(count == 10)
//   {
//     count = 0;
//     n++;
//     if(n == 10000)
//     {
//       n = 0;
//     }
//   }
// }

So I know why my button isn't doing what I need, because there is a call to a looping function inside the main loop and whilst it is doing that it isn't checking my button state, I verified the button is working and it enters that routine when I hold it down, I just want to know what changes I need to make to be able to constantly check the button state or when it is pressed in to save that information and to run the routine the next time it returns to the main loop, ideally staying in that state until the button is pressed again but first I just want it to change when I press.
It's just a basic tactile switch (unsure part number) I am using a Mega 2560 Board and I have a 5641 as 4 digit display.

This is one of the most common questions asked in this forum.

So the answer is also one of the most common answers given on the forum. You have to get rid of those delay() calls. You can't just remove them. You have to rewrite the code in a way that delay() is not needed.

Terms to search for:

  • Blink without delay
  • Doing more than one thing at once
  • State machine

I must warn you. This involves taking your coding skills to the next level. Reviewing the code you posted, it's clear that your coding skills are at a basic level.

3 Likes

Do you have a pull up or pull down resistor on your button? the line " pinMode(buttonPin, INPUT);" does not enable the internal pullup.

2 Likes

the line " pinMode(buttonPin, INPUT);" does not enable the internal pullup.

I am using a 10k pull up resistor. Why does that not work?

You have to get rid of those delay() calls.

I figured it was something like that I will try to cut them where I can

You CAN cut them all out! You will create individual timers for each of your LED operations. When that timer expires you can then do the code for that LED and restore that timer to it's original value.
Then your loop() will run at full speed, all the time, and your tactile switch will be recognized immediately.

1 Like

Is your circuit wired as is S2 below?

Yep that's essentially what I have expect only the pullup path. The issue is the delays which stop my pushes being registered.

I like that enthusiasm! I am currently working on getting rid of them, got it down to 2 which are the slight blink I want after some information is displayed before the next, which I assume I can use a timer for also.

That was quick!

I have the button responding instantly when I click it now thank you people! I have another issue that I believe I know the cause of but unsure on the fix. Now when I click the button it goes to "stateChanger" but I continuously have the button pressed in so the state keeps changing. Would a delay actually be beneficial here?

#include <TimerOne.h>
//the pins of 4-digit 7-segment display attach to pin2-13 respectively 
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;
long n = 50;// n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999. 
int x = 100;
int del = 5;//Set del as 5; the value is the degree of fine tuning for the clock
int count = 0;//Set count=0. Here count is a count value that increases by 1 every 0.1 second, which means 1 second is counted when the value is 10

const int buttonPin = 32;
int buttonState = 0;

enum state {
  charge,
  solar
};
state currentState;
unsigned long time;
void setup()
{
  //set all the pins of the LED display as output
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  //Timer1.attachInterrupt( add ); // attach the service routine here
  currentState = charge;
}
/***************************************/ 
void loop()
{
  clearLEDs();
  switch(currentState) {
    case charge:
      SOC();  
      break;
    case solar:
      SOL();
      break;
  }
}

void SOC()
{
  time = millis();
  while (millis() - time < 5000){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
    }
    clearLEDs();//clear the 7-segment display screen
    pickDigit(0);//Light up 7-segment display d1
    S();// State

    clearLEDs();//clear the 7-segment display screen
    pickDigit(1);//Light up 7-segment display d2
    O();// of

    clearLEDs();//clear the 7-segment display screen
    pickDigit(2);//Light up 7-segment display d3
    C();//Charge
  }
  blinkTimer();
  time = millis();
  while (millis() - time < 5000){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
    }

    clearLEDs();//clear the 7-segment display screen
    pickDigit(0);//Digit 1
    pickNumber(n%100/10);//get the value of ten

    clearLEDs();//clear the 7-segment display screen
    pickDigit(1);// Digit 2
    pickNumber(n%10);//Get the value of single digit

  }
  blinkTimer();
}


void SOL(){
  time = millis();
  while (millis() - time < 5000){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
    }
    clearLEDs();//clear the 7-segment display screen
    pickDigit(0);//Light up 7-segment display d1
    S();// State
    // delay(del);//delay 5ms

    clearLEDs();//clear the 7-segment display screen
    pickDigit(1);//Light up 7-segment display d2
    O();// of
    // delay(del);//delay 5ms

    clearLEDs();//clear the 7-segment display screen
    pickDigit(2);//Light up 7-segment display d3
    L();//
    // delay(del);//delay 5ms
  }

}

void stateChanger(){
  clearLEDs();
  
  if(currentState == charge){
    Serial.println(1234);
    currentState = solar;
  }

  if(currentState == solar){
    currentState = charge;
  }

}


void blinkTimer(){
  time = millis();
  clearLEDs();
  while (millis() - time < 250){
  
  }
}

void pickDigit(int x) //light up a 7-segment display
{
  //The 7-segment LED display is a common-cathode one. So also use digitalWrite to  set d1 as high and the LED will go out
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);

  switch(x)
  {
    case 0: 
    digitalWrite(d1, LOW);//Light d1 up 
    break;
    case 1: 
    digitalWrite(d2, LOW); //Light d2 up 
    break;
    case 2: 
    digitalWrite(d3, LOW); //Light d3 up 
    break;
    default: 
    digitalWrite(d4, LOW); //Light d4 up 
    break;
  }
}

//The function is to control the 7-segment LED display to display numbers. Here x is the number to be displayed. It is an integer from 0 to 9 
void pickNumber(int x)
{
  switch(x)
  {
    default: 
    zero(); 
    break;
    case 1: 
    one(); 
    break;
    case 2: 
    two(); 
    break;
    case 3: 
    three(); 
    break;
    case 4: 
    four(); 
    break;
    case 5: 
    five(); 
    break;
    case 6: 
    six(); 
    break;
    case 7: 
    seven(); 
    break;
    case 8: 
    eight(); 
    break;
    case 9: 
    nine(); 
    break;
  }
} 
void clearLEDs() //clear the 7-segment display screen
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void zero() //the 7-segment led display 0
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void one() //the 7-segment led display 1
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void two() //the 7-segment led display 2
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void three() //the 7-segment led display 3
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void four() //the 7-segment led display 4
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void five() //the 7-segment led display 5
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void six() //the 7-segment led display 6
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void seven() //the 7-segment led display 7
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void eight() //the 7-segment led display 8
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void nine() //the 7-segment led display 9
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void L(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void S(){
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void O(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void C(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void P(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
/*******************************************/
// void add()
// {
//   // Toggle LED
//   count ++;
//   if(count == 10)
//   {
//     count = 0;
//     n++;
//     if(n == 10000)
//     {
//       n = 0;
//     }
//   }
// }

No! You have to code so you know and remember that the switch CHANGED, not that is currently is pressed.

1 Like

I agree with Paul, however you should be aware on how to handle these types of situations without using delay.

There is a millis() counter that is initiated by the IDE that increments every millisecond. You can use that to in effect cause some contition to not be executed for some number of milliseconds.

In the IDE lLook at File/examples/02.Digital/ blink without delay for a simple example.

1 Like

Ok, thank you everyone for your help I have it doing what I want (for now!) Thank you everyone that helped me out, it's definitely not a masterpiece but it seems to work for me! Definitely need to comment it up properly too.

My next step will be working with MODBUS to pull the values in which I am hoping won't be too bad.

#include <TimerOne.h>
//the pins of 4-digit 7-segment display attach to pin2-13 respectively 
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;
long n = 50;// n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999.
int y = 45; 
int x = 100;
int del = 5;//Set del as 5; the value is the degree of fine tuning for the clock
int count = 0;//Set count=0. Here count is a count value that increases by 1 every 0.1 second, which means 1 second is counted when the value is 10

const int buttonPin = 32;
int buttonState = 0;
int break_tag = 0;
int timer_threshold = 3000; //milliseconds

enum state {
  charge,
  solar
};
state currentState;
unsigned long time;
unsigned long prevInput = 0;
void setup()
{
  //set all the pins of the LED display as output
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  //Timer1.attachInterrupt( add ); // attach the service routine here
  currentState = charge;
}
/***************************************/ 
void loop()
{
  clearLEDs();
  switch(currentState) {
    case charge:
      SOC();  
      break_tag = 0;
      break;
    case solar:
      SOL();
      break_tag = 0;
      break;
  }
}

void SOC()
{
  time = millis();
  while (millis() - time < timer_threshold){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
      break;
    }

    if (break_tag == 1){
      break;
    }
    clearLEDs();//clear the 7-segment display screen
    pickDigit(0);//Light up 7-segment display d1
    S();// State

    clearLEDs();//clear the 7-segment display screen
    pickDigit(1);//Light up 7-segment display d2
    O();// of

    clearLEDs();//clear the 7-segment display screen
    pickDigit(2);//Light up 7-segment display d3
    C();//Charge
  }
  blinkTimer();
  time = millis();
  while (millis() - time < timer_threshold){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
      break;
    }

    if (break_tag == 1){
      break;
    }

    clearLEDs();//clear the 7-segment display screen
    pickDigit(0);//Digit 1
    pickNumber(n%100/10);//get the value of ten

    clearLEDs();//clear the 7-segment display screen
    pickDigit(1);// Digit 2
    pickNumber(n%10);//Get the value of single digit

  }
  blinkTimer();
}

void SOL(){
  clearLEDs();
  time = millis();
  while (millis() - time < timer_threshold){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
    }
    if (break_tag == 1){
      break;
    }
    clearLEDs();//clear the 7-segment display screen
    pickDigit(0);//Light up 7-segment display d1
    P();// Photo
    //delay(del);//delay 5ms

    clearLEDs();//clear the 7-segment display screen
    pickDigit(1);//Light up 7-segment display d2
    V();// Voltaic
    //delay(del);//delay 5ms
    
    }
  blinkTimer();
  time = millis();
  while (millis() - time < timer_threshold){
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
      break;
    }

    if (break_tag == 1){
      break;
    }
     clearLEDs();//clear the 7-segment display screen
    pickDigit(0);//Light up 7-segment display d1
    pickNumber((y/1000));// get the value of thousand

    clearLEDs();//clear the 7-segment display screen
    pickDigit(1);//Light up 7-segment display d2
    pickNumber((y%1000)/100);// get the value of hundred

    clearLEDs();//clear the 7-segment display screen
    pickDigit(2);//Light up 7-segment display d3
    pickNumber(y%100/10);//get the value of ten

    clearLEDs();//clear the 7-segment display screen
    pickDigit(3);//Light up 7-segment display d4
    pickNumber(y%10);//Get the value of single digit
  }
  blinkTimer();
}

void stateChanger(){
  clearLEDs();
  break_tag = 1;
  unsigned long currentInput = millis();
  if (currentInput - prevInput >= 2000){
    prevInput = currentInput;
  
    switch(currentState) {
      case solar:
        while (buttonState == HIGH){
          buttonState = LOW;
        }
        
        currentState = charge;
      break;
        
      case charge:
        while (buttonState == HIGH){
          buttonState = LOW;
        }
        
        currentState = solar;
      break;
    }
  }
}


void blinkTimer(){
  time = millis();
  clearLEDs();
  while (millis() - time < 250){
  
  }
}

void pickDigit(int x) //light up a 7-segment display
{
  //The 7-segment LED display is a common-cathode one. So also use digitalWrite to  set d1 as high and the LED will go out
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);

  switch(x)
  {
    case 0: 
    digitalWrite(d1, LOW);//Light d1 up 
    break;
    case 1: 
    digitalWrite(d2, LOW); //Light d2 up 
    break;
    case 2: 
    digitalWrite(d3, LOW); //Light d3 up 
    break;
    default: 
    digitalWrite(d4, LOW); //Light d4 up 
    break;
  }
}

//The function is to control the 7-segment LED display to display numbers. Here x is the number to be displayed. It is an integer from 0 to 9 
void pickNumber(int x)
{
  switch(x)
  {
    default: 
    zero(); 
    break;
    case 1: 
    one(); 
    break;
    case 2: 
    two(); 
    break;
    case 3: 
    three(); 
    break;
    case 4: 
    four(); 
    break;
    case 5: 
    five(); 
    break;
    case 6: 
    six(); 
    break;
    case 7: 
    seven(); 
    break;
    case 8: 
    eight(); 
    break;
    case 9: 
    nine(); 
    break;
  }
} 
void clearLEDs() //clear the 7-segment display screen
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void zero() //the 7-segment led display 0
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void one() //the 7-segment led display 1
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void two() //the 7-segment led display 2
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void three() //the 7-segment led display 3
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void four() //the 7-segment led display 4
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void five() //the 7-segment led display 5
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void six() //the 7-segment led display 6
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void seven() //the 7-segment led display 7
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void eight() //the 7-segment led display 8
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void nine() //the 7-segment led display 9
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void L(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void S(){
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void O(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void C(){
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void P(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void V(){
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}
/*******************************************/
// void add()
// {
//   // Toggle LED
//   count ++;
//   if(count == 10)
//   {
//     count = 0;
//     n++;
//     if(n == 10000)
//     {
//       n = 0;
//     }
//   }
// }

It is extremely long and repetitive! Most of the long repetitive parts relate to the 7 segment display.

Have you considered using the SevSeg library? That would greatly reduce the length of your code and remove a lot of repetition.

I believe I did see it on a few of the sketches I read. I will have a look at it soon I have just been getting my head around how it all works so far. What are the actual issues with having a lot of repetitive code, does it slow my device down or is it just bad practice?

Please read

And consider if you have done this correctly.

This is because you gave yourself the credit for solving the problem, but you did not solve the problem. You only verified that something someone told you to try worked. It is that person who deserves to be credited with the solution.

  • Running out of memory space, flash (program) and/or RAM (dynamic) memory.
  • Reducing the chances of completing your project successfully. There are always bugs in new code. More code == more bugs and they are more difficult to find.
  • Fixing those bugs or making other changes to the code later, when you want to add more functions or change the code in some way. Some changes may have to be made multiple times in all the repetitions of the (effectively) same code, instead of making the change once.
1 Like

Sorry yes I will change that

Update for anyone interested/for future here is the code reformatted with the sevseg library and commented a bit better

#include <TimerOne.h>
#include "SevSeg.h"
SevSeg sevseg;  //Instantiate a seven segment object
//the pins of 4-digit 7-segment display attach to pin2-13 respectively

long n = random(100);  // n represents the value displayed on the LED display (SOC). For example, when n=0, 0000 is displayed. The maximum value is 9999.
int y = random(9999);  //placeholder for solar generation

const int buttonPin = 32;    //Pin button is connected to
int buttonState = 0;         //starts LOW (unpressed)
int break_tag = 0;           //default for breaking out of loop upon button press
int text_timer = 1500;       //Explicitly for text displays
int timer_threshold = 2500;  //milliseconds for numbers

int blink = 250;  //Timer for blinks between display changes (milliseconds)

//List of states
enum state {
  charge,
  solar,
  load,
  daily_kwh
};
state currentState;  //Value for checking current state

unsigned long time;           //variable for saving how much time has elapsed (for timers)
unsigned long prevInput = 0;  //Timer for when the previous input was made (default 0)
void setup() {
  byte numDigits = 4;                               //Number of digits on 7 seg
  byte digitPins[] = { 13, 12, 11, 10 };            //pins connected to digits
  byte segmentPins[] = { 2, 3, 4, 5, 6, 7, 8, 9 };  //pins connected to segments
  bool resistorsOnSegments = true;                  // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_CATHODE;             // 5641as is common cathode seven segment
  bool updateWithDelays = false;                    // Default 'false' is Recommended
  bool leadingZeros = false;                        // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = false;                     // Use 'true' if your decimal point doesn't exist or isn't connected. Then, you only need to specify 7 segmentPins[]

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  currentState = charge;  // sets current state to state in SOC
}
/***************************************/
//Main loop holds state switching and continuosly calls them
void loop() {
  sevseg.blank();
  switch (currentState) {
    case charge:
      SOC();
      break_tag = 0;

      break;
    case solar:
      SOL();
      break_tag = 0;
      break;
  }
}

//State of charge function
//Displays SOC then the value
void SOC() {
  time = millis();
  while (millis() - time < text_timer) {
    sevseg.refreshDisplay();
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
      break;
    }

    if (break_tag == 1) {
      break;
    }
    sevseg.setChars("Soc");
  }
  sevseg.blank();
  blinkTimer();
  time = millis();
  while (millis() - time < timer_threshold) {
    sevseg.refreshDisplay();
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
      break;
    }

    if (break_tag == 1) {
      break;
    }
    sevseg.setNumber(n, 0);
  }
  sevseg.blank();
  blinkTimer();
}

//Solar function for displaying solar production
void SOL() {
  time = millis();
  while (millis() - time < text_timer) {
    sevseg.refreshDisplay();
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
      break;
    }

    if (break_tag == 1) {
      break;
    }
    sevseg.setChars("PV");
  }
  sevseg.blank();
  blinkTimer();
  time = millis();
  while (millis() - time < timer_threshold) {
    sevseg.refreshDisplay();
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      stateChanger();
      break;
    }

    if (break_tag == 1) {
      break;
    }
    sevseg.setNumber(y, 0);
  }
  sevseg.blank();
  blinkTimer();
}

//Upon button press will be called to move onto next function
void stateChanger() {
  sevseg.blank();
  break_tag = 1;
  unsigned long currentInput = millis();
  if (currentInput - prevInput >= 500) {
    prevInput = currentInput;

    switch (currentState) {
      case solar:
        while (buttonState == HIGH) {
          buttonState = LOW;
        }
        n = random(100);
        currentState = charge;
        break;

      case charge:
        while (buttonState == HIGH) {
          buttonState = LOW;
        }
        y = random(9999);
        currentState = solar;
        break;
    }
  }
}

//Timer for blanks betweens changing display
void blinkTimer() {
  time = millis();
  sevseg.blank();
  while (millis() - time < blink) {
  }
}