Newcommer coding issue

I’m playing around with the 4 digit 7 segment display, and I have a pot hooked up to speed up time. I had no trouble coding that. my troubles come when I add a if statement to the code. the timer dont move, and I’ve tried moving the code. Please help

Author: SMRAZA KEEN
Date:2016/6/29
IDE V1.6.9
Email:TechnicSmraza@outlook.com
Function:
*/
int pot = A0;
int timeSwitch = 19;
int ASeg = 9;
int BSeg = 13;
int CSeg = 4;
int DSeg = 6;
int ESeg = 7;
int FSeg = 10;
int GSeg = 3;

int a1 = 8;
int a2 = 11;
int a3 = 12;
int a4 = 2;
// set variable
long n = 0;
int x = 100;

int del = 54; // fine adjustment for clock

void setup()
{
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(a3, OUTPUT);
pinMode(a4, OUTPUT);
pinMode(ASeg, OUTPUT);
pinMode(BSeg, OUTPUT);
pinMode(CSeg, OUTPUT);
pinMode(DSeg, OUTPUT);
pinMode(ESeg, OUTPUT);
pinMode(FSeg, OUTPUT);
pinMode(GSeg, OUTPUT);
pinMode(timeSwitch, INPUT);
}

void loop()
{

int val = digitalRead(timeSwitch);
if (val == LOW)
{
n = n + 3;
}
else
{
n = n - 2;
}

del = analogRead(pot) * 3;
clearLEDS(); // Eliminating ghost
pickDigit(1); // Display position 1
pickNumber((n / x / 1000) % 10); // Display number
delayMicroseconds(del); // Time adjustment

clearLEDS();
pickDigit(2);
pickNumber((n / x / 100) % 10);
delayMicroseconds(del);

clearLEDS();
pickDigit(3);
pickNumber((n / x / 10) % 10);
delayMicroseconds(del);

clearLEDS();
pickDigit(4);
pickNumber(n / x % 10);
delayMicroseconds(del);
n++;
/*
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
}
}
Standard*/

}
// Select display position
void pickDigit(int x)
{
digitalWrite(a1, LOW);
digitalWrite(a2, LOW);
digitalWrite(a3, LOW);
digitalWrite(a4, LOW);

Hi,

Bheschel:
I’m playing around with the 4 digit 7 segment display, and I have a pot hooked up to speed up time. I had no trouble coding that. my troubles come when I add a if statement to the code.

Can you post the whole code please (using code tags… click the </> in the message editor).
and then describe exactly what “if statement” you are trying to add and what you expect it to do.

Yours,
TonyWilk

void loop()
{

int val = digitalRead(timeSwitch);
if (val == LOW)
{
n = n + 3;
}
else
{
n = n - 2;
}

that's the part of the code i've written, the if statement at the bottom is the example I was using. when I comment out the if statement the n++ at the bottom of the main loop works just fine. I'm trying to make the counter go backwards with the switch. I have a uno, and read that the analog pins can be used as digital 14-19?

/*
   Author: SMRAZA KEEN
   Date:2016/6/29
   IDE V1.6.9
   Email:TechnicSmraza@outlook.com
   Function:
*/
int pot = A0;
int timeSwitch = 19;
int ASeg = 9;
int BSeg = 13;
int CSeg = 4;
int DSeg = 6;
int ESeg = 7;
int FSeg = 10;
int GSeg = 3;

int a1 = 8;
int a2 = 11;
int a3 = 12;
int a4 = 2;
// set variable
long n = 0;
int x = 100;

int del = 54; // fine adjustment for clock

void setup()
{
  pinMode(a1, OUTPUT);
  pinMode(a2, OUTPUT);
  pinMode(a3, OUTPUT);
  pinMode(a4, OUTPUT);
  pinMode(ASeg, OUTPUT);
  pinMode(BSeg, OUTPUT);
  pinMode(CSeg, OUTPUT);
  pinMode(DSeg, OUTPUT);
  pinMode(ESeg, OUTPUT);
  pinMode(FSeg, OUTPUT);
  pinMode(GSeg, OUTPUT);
  pinMode(timeSwitch, INPUT);
}

void loop()
{

  int val = digitalRead(timeSwitch);
  if (val == LOW)
  {
    n = n + 3;
  }
  else
  {
    n = n - 2;
  }
  
  
  del = analogRead(pot) * 3;
  clearLEDS();  // Eliminating ghost
  pickDigit(1);  // Display position 1
  pickNumber((n / x / 1000) % 10); // Display number
  delayMicroseconds(del);  // Time adjustment

  clearLEDS();
  pickDigit(2);
  pickNumber((n / x / 100) % 10);
  delayMicroseconds(del);

  clearLEDS();
  pickDigit(3);
  pickNumber((n / x / 10) % 10);
  delayMicroseconds(del);

  clearLEDS();
  pickDigit(4);
  pickNumber(n / x % 10);
  delayMicroseconds(del);
    n++;
/*
    int val = digitalRead(inputPin); // read input value
    if (val == HIGH) // check if the input is HIGH
    {
    digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
    }
    else
    {
    digitalWrite(ledPin, LOW); // turn LED off
    }
    }
    Standard*/

}
// Select display position
void pickDigit(int x)
{
  digitalWrite(a1, LOW);
  digitalWrite(a2, LOW);
  digitalWrite(a3, LOW);
  digitalWrite(a4, LOW);

  switch (x)
  {
    case 1:
      digitalWrite(a1, HIGH);
      break;
    case 2:
      digitalWrite(a2, HIGH);
      break;
    case 3:
      digitalWrite(a3, HIGH);
      break;
    case 4:
      digitalWrite(a4, HIGH);
      break;
  }
}
// select display number
void pickNumber(int x)
{
  switch (x) {

    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;
    default: zero(); break;
  }
}

void clearLEDS()  // clear the screen
{
  digitalWrite(ASeg, HIGH);
  digitalWrite(BSeg, HIGH);
  digitalWrite(CSeg, HIGH);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, HIGH);
}

//Display function ‘0-9’
void zero() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, HIGH);
}

void one() {
  digitalWrite(ASeg, HIGH);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, HIGH);
}

void two() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, HIGH);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, LOW);
}

void three() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, LOW);
}
void four() {
  digitalWrite(ASeg, HIGH);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}
void five() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, HIGH);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}
void six() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, HIGH);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}

void seven() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, HIGH);
}

void eight() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}

void nine() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}

Before doing anything else I would tidy up the writing of the segment data. You do not need separate functions for each digit value, but I can see why you did it that way.

You could have a single function to turn the required segments on and pass it the value of the digit to be displayed. Put the segment on/off data in a 2 dimensional array, one row per digit and one column per segment. When you call the function use the digit (0 to 9) as the index to the array row (the first dimension) and iterate through the columns (second dimension) of that row to turn the segments on or off based on the data held. This would also remove the need for the switch/case and you program would be much smaller and tidier.

An obvious problem with your code is that you are detecting when a button is pressed rather than when it becomes pressed so currently the button press will be detected multiple times as loop() repeats which is probably not what you want. Look at the StateChangeDetection example in the IDE to see how to detect when a button becomes pressed.

Try setting:

  pinMode(timeSwitch, INPUT_PULLUP);

and comment out the n++, then it should continuously count up or down.

Notice that if you count down the value will go to zero and then wrap around to quite a big number.

Yours,
TonyWilk

so I tried pinMode(timeSwitch, INPUT_PULLUP) and my counter still said 0000. i tried removing the PULLUP and switching it to a analogRead and that didnt help.

/*
   Author: SMRAZA KEEN
   Date:2016/6/29
   IDE V1.6.9
   Email:TechnicSmraza@outlook.com
   Function:
*/
int pot = A0;
int timeSwitch = 19;
int ASeg = 9;
int BSeg = 13;
int CSeg = 4;
int DSeg = 6;
int ESeg = 7;
int FSeg = 10;
int GSeg = 3;

int a1 = 8;
int a2 = 11;
int a3 = 12;
int a4 = 2;
// set variable
long n = 0;
int x = 100;

int del = 54; // fine adjustment for clock

void setup()
{
  pinMode(a1, OUTPUT);
  pinMode(a2, OUTPUT);
  pinMode(a3, OUTPUT);
  pinMode(a4, OUTPUT);
  pinMode(ASeg, OUTPUT);
  pinMode(BSeg, OUTPUT);
  pinMode(CSeg, OUTPUT);
  pinMode(DSeg, OUTPUT);
  pinMode(ESeg, OUTPUT);
  pinMode(FSeg, OUTPUT);
  pinMode(GSeg, OUTPUT);
  pinMode(timeSwitch, INPUT);
}

void loop()
{

  int val = analogRead(timeSwitch);
  if (val < 400)
  {
    n = n + 3;
  }
  else
  {
    n = n - 2;
  }
  
  
  del = analogRead(pot) * 3;
  clearLEDS();  // Eliminating ghost
  pickDigit(1);  // Display position 1
  pickNumber((n / x / 1000) % 10); // Display number
  delayMicroseconds(del);  // Time adjustment

  clearLEDS();
  pickDigit(2);
  pickNumber((n / x / 100) % 10);
  delayMicroseconds(del);

  clearLEDS();
  pickDigit(3);
  pickNumber((n / x / 10) % 10);
  delayMicroseconds(del);

  clearLEDS();
  pickDigit(4);
  pickNumber(n / x % 10);
  delayMicroseconds(del);
//    n++;
/*
    int val = digitalRead(inputPin); // read input value
    if (val == HIGH) // check if the input is HIGH
    {
    digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
    }
    else
    {
    digitalWrite(ledPin, LOW); // turn LED off
    }
    }
    Standard*/

}
// Select display position
void pickDigit(int x)
{
  digitalWrite(a1, LOW);
  digitalWrite(a2, LOW);
  digitalWrite(a3, LOW);
  digitalWrite(a4, LOW);

  switch (x)
  {
    case 1:
      digitalWrite(a1, HIGH);
      break;
    case 2:
      digitalWrite(a2, HIGH);
      break;
    case 3:
      digitalWrite(a3, HIGH);
      break;
    case 4:
      digitalWrite(a4, HIGH);
      break;
  }
}
// select display number
void pickNumber(int x)
{
  switch (x) {

    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;
    default: zero(); break;
  }
}

void clearLEDS()  // clear the screen
{
  digitalWrite(ASeg, HIGH);
  digitalWrite(BSeg, HIGH);
  digitalWrite(CSeg, HIGH);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, HIGH);
}

//Display function ‘0-9’
void zero() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, HIGH);
}

void one() {
  digitalWrite(ASeg, HIGH);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, HIGH);
}

void two() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, HIGH);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, LOW);
}

void three() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, LOW);
}
void four() {
  digitalWrite(ASeg, HIGH);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}
void five() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, HIGH);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}
void six() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, HIGH);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}

void seven() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, HIGH);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, HIGH);
  digitalWrite(GSeg, HIGH);
}

void eight() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, LOW);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}

void nine() {
  digitalWrite(ASeg, LOW);
  digitalWrite(BSeg, LOW);
  digitalWrite(CSeg, LOW);
  digitalWrite(DSeg, LOW);
  digitalWrite(ESeg, HIGH);
  digitalWrite(FSeg, LOW);
  digitalWrite(GSeg, LOW);
}

my counter still said 0000

Is it the counter or the display that is not working ?

What do you see if you print n to the Serial monitor ?

the counter works and counts up when I hide the if stament and unhide the n++.
when I unhide the if statement the counter just sits at 0000

Bheschel:
the counter works and counts up when I hide the if stament and unhide the n++.
when I unhide the if statement the counter just sits at 0000

Try this:

  if (val < 400)
  {
    if( n > 0 )
      n = n - 1;
  }
  else
  {
    if( n < 9999 )
       n = n + 1;
  }

I’ve swapped it around just for testing, if the input is high it will count up.
If the input is low, it will count down - but stop at zero.

Previously the number would go negative, so you would only show 0000
If you havn’t got the right pin, it would never go positive.

Now it should at least count up.
If you have the correct pin and switch it to ground, it should count down.

Yours,
TonyWilk