Calling Upon Functions inside a loop

I have a count up and down timer that will turn on a fan and buzzer when the timer hits 60 seconds and back down to zero. I am using a remote control to control the entire circuit. The timer is trying to count up but it seems my void function names are not in the right place or something. So far I have gotten to where the timer is trying to count but won't leave zero. Also, if someone could help me try to interface my buzzer with the timer and remote that would be helpful too. And I can give the specs for the motor once the timer is working if someone wishes to help me with the motor and buzzer as well. Thank you.

#include <ir_Lego_PF_BitStreamEncoder.h>
#include <boarddefs.h>
#include <IRremoteInt.h>
#include <IRremote.h>

int pinCount = 2; //number of pins in the array

//Define Sensor Pin
const int RECV_PIN =22;

//Define LED Constants
const int bluePin =26;
const int greenPin =28;
const int redPin =24;

//Define IR Reciever and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

//Button Definition
boolean Power=false;
boolean Play=false;

//Define Integer to Remember Toggle State
int togglestate = 0;

unsigned long time;
unsigned long time2;
int counter;
bool upCounter = false;
bool downCounter = false;
bool mainCounter = false;

//Define Pins of 4 digit 7 segment Display
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;

//Define Digital Pins of Display
int d4 = 12;
int d3 = 11;
int d2 = 10;
int d1 = 9;

//Math
long n = 0; //n represents value displayed on display
long y = 60000; //y represents start time of count down
int x = 100;
int del = 5; //value is the degree of fine tuning 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

//Timer
int timer = 500;
int value;

void setup() {

Serial.begin(9600);
irrecv.enableIRIn();

//Set LEDs as Outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

//Set LED Display Pins as Outputs
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);
time = millis();
}

void loop() {

time2 = millis();
if( time2- time >= 1000){
time = time2;
}
if(mainCounter == true){

if(time2- time >= 1000){
if( upCounter == true){
counter++;
if(counter >= 60){
upCounter = false;
downCounter = true;
}

}
else if(downCounter == true){
counter--;
if(counter <=0){
upCounter =true;
downCounter = false;
mainCounter = false;
}
}
time = time2;
}

}

if (irrecv.decode(&results)){
int value = results.value;
Serial.println(value);
switch(value){
case 0xFFA25D:
//Power ON/OFF
Power= !Power;
if(Power){
digitalWrite(bluePin, 0xFF);
digitalWrite(redPin, 0x00);
digitalWrite(greenPin, 0x00);
digitalWrite(d1, LOW);
digitalWrite(d2, LOW);
digitalWrite(d3, LOW);
digitalWrite(d4, LOW);
}
if(!Power){
digitalWrite(bluePin, LOW);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(d1, HIGH);
digitalWrite(d2, HIGH);
digitalWrite(d3, HIGH);
digitalWrite(d4, HIGH);
}
delay(1500);
break;
}
switch(value){
case 0xFF22DD:
//Play/Pause
Play= !Play;
if(Play){
mainCounter =true;
digitalWrite(bluePin, 0x00);
digitalWrite(redPin, 0xFF);
digitalWrite(greenPin, 0x00);
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(2);//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(3);//Light up 7-segment display d4
pickNumber(n%10);//Get the value of single digit
delay(del);//delay 5ms
}
if(!Play){ //Pause
mainCounter =false;
digitalWrite(bluePin, 0xFF);
digitalWrite(redPin, 0x00);
digitalWrite(greenPin, 0x00);
}
delay(1500);
break;
}
irrecv.resume();
}

}
/**************************************/
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);
}

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);
}/code]

Have a quick read of the forum 'how to' guide at the beginning of this section. Use code tags, you can edit your post. This way you are more likely to get a useful answer

pmagowan:
Have a quick read of the forum 'how to' guide at the beginning of this section. Use code tags, you can edit your post. This way you are more likely to get a useful answer

thank you, I fixed it so people can get the code

Please learn to properly indent your code so it is readable. What you've posted just makes my head hurt....

Regards,
Ray L.

Good Karma!

Now try and break down your question into logical steps.
do you want to turn your fan on when timer hits 60? when do you want to turn it off? How are you controling the circuit with the remote? What do you mean by 'the timer is trying to count up'?

Generally it is best to look at one task at a time, get it working and then move on the the next. Then you can think about how you integrate them

P.S you can auto format your code in the arduino ide

pmagowan:
Good Karma!

Now try and break down your question into logical steps.
do you want to turn your fan on when timer hits 60? when do you want to turn it off? How are you controling the circuit with the remote? What do you mean by 'the timer is trying to count up'?

Generally it is best to look at one task at a time, get it working and then move on the the next. Then you can think about how you integrate them

P.S you can auto format your code in the arduino ide

So, I have the IR remote working to control the entire circuit. The two buttons, I'm pressing are Power and Play/Pause. When I press play, the counter is going to count up to 60 and when the counter hits 60, a buzzer will sound and then the DC motor and fan will come on and stay on as the counter is counting down. And at anytime I press pause the timer will pause and the fan should pause/stop as well. And if I hit play, everything will resume. But when I press play, the LED kinda glitches like its trying to start counting but it stays at zero.

It is hard for me to determine what you are trying to accomplish by looking at the code. Can you be more specific on what the code is SUPPOSED to do?

General comments:

  1. Develop your code in increments. Get the most basic logic working without hardware interfaces just using serial print statements.
  2. You can clean up a lot of your code by using arrays for pins and LED states.
  3. Single character variable names are discouraged. They are hard to search for and not descriptive.
  4. I'm sure the use of multiple flags could be eliminated if your program requirements were clear.

It sounds like you are treating it like a conscious being which is trying to comply with your instructions. I suspect it is doing exactly what it is coded to do. The remote is not controlling the circuit. I suspect the remote is sending a signal to the arduino through a sensor and the Arduino is controlling the circuit. You need to break everything down into logical steps. The arduino does not have intuition.

eg

press power on remote
signal received by sensor x
signal read by arduino through pin x
signal triggers power on function
etc etc

then you can troubleshoot each part
so, does the remote button work?
is the signal received?
does the arduino read signal
does the arduino run function powerOn?
etc etc

you can use Serial.print on the arduino to show you what it is doing.

so write

Serial.print ("working at this point");

and stick it in your code to see if you have got stuck at various points. obviously adjust this as required to debug

This code just resets the 1 second timer and does noting useful...

 time2 = millis();
  if ( time2 - time >= 1000) {
    time = time2;
  }

After it completes the condition time2 - time >= 1000 cannot possibly still be true, since if it was then you set time = time2 and consequently time2 - time must now be equal 0 by definition.

Therefore the timer test in this code (regardless of the value of mainCounter)...

 if (mainCounter == true) {

    if (time2 - time >= 1000) {
      if ( upCounter == true) {
        counter++;
        if (counter >= 60) {
          upCounter = false;
          downCounter = true;
        }
      }
      else if (downCounter == true) {
        counter--;
        if (counter <= 0) {
          upCounter = true;
          downCounter = false;
          mainCounter = false;
        }
      }
      time = time2;
    }

  }

Cannot possibly do anything useful either.

So all you are left with is your IR Receiver code.

An example of how to build in increments is shown below. I took everything out except for the flags and counters and quickly debugged using the following code:

unsigned long time;
unsigned long time2;
int counter;
bool upCounter = true;
bool downCounter = false;
bool mainCounter = true;

void setup() {

  Serial.begin(9600);
  time = millis();
}

void loop() {

  time2 = millis();
//  if ( time2 - time >= 1000) {
//    time = time2;
//  }
  if (mainCounter == true) {
    if (time2 - time >= 100) {
      if ( upCounter == true) {
        counter++;
        if (counter >= 60) {
          upCounter = false;
          downCounter = true;
        }
      }
      else if (downCounter == true) {
        counter--;
        if (counter <= 0) {
          upCounter = true;
          downCounter = false;
          mainCounter = false;
        }
      }
      Serial.print("Count = ");
      Serial.println(counter);
      time = time2;
    }
  }
}

I quickly commented out the lines that caused the counters not to work and now they are working. Now you can proceed to debug another part of the code OR figure out how to do this more efficiently.

So far I have gotten to where the timer is trying to count but won't leave zero.

in the following, your reset "time" to "time2" in the first test so that it can never be true in the 2nd test after mainCounter. why not just increment the counter in the first test?

void loop() {
    time2 = millis();
    if( time2- time >= 1000){
        time = time2;
    }

    if(mainCounter == true){
        if(time2- time >= 1000){
            if( upCounter == true){
                counter++;

indented code (too long, had to prune). you could use an array of which pins to set for each digit and have a single function

#include <ir_Lego_PF_BitStreamEncoder.h>
#include <boarddefs.h>
#include <IRremoteInt.h>
#include <IRremote.h>
int pinCount = 2; //number of pins in the array
//Define Sensor Pin
const int RECV_PIN =22;
//Define LED Constants
const int bluePin =26;
const int greenPin =28;
const int redPin =24;
//Define IR Reciever and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;
//Button Definition
boolean Power=false;
boolean Play=false;
//Define Integer to Remember Toggle State
int togglestate = 0;
unsigned long time;
unsigned long time2;
int counter;
bool upCounter = false;
bool downCounter = false;
bool mainCounter = false;
//Define Pins of 4 digit 7 segment Display
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
//Define Digital Pins of Display
int d4 = 12;
int d3 = 11;
int d2 = 10;
int d1 = 9;
//Math
long n = 0; //n represents value displayed on display
long y = 60000; //y represents start time of count down
int x = 100;
int del = 5; //value is the degree of fine tuning 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
//Timer
int timer = 500;
int value;

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(9600);
    irrecv.enableIRIn();
    //Set LEDs as Outputs
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
    //Set LED Display Pins as Outputs
    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);
    time = millis();
}

// -----------------------------------------------------------------------------
void loop() {
    time2 = millis();
    if( time2- time >= 1000){
        time = time2;
    }

    if(mainCounter == true){
        if(time2- time >= 1000){
            if( upCounter == true){
                counter++;
                if(counter >= 60){
                    upCounter = false;
                    downCounter = true;
                }

            }

            else if(downCounter == true){
                counter--;
                if(counter <=0){
                    upCounter =true;
                    downCounter = false;
                    mainCounter = false;
                }

            }

            time = time2;
        }

    }

    if (irrecv.decode(&results)){
        int value = results.value;
        Serial.println(value);
        switch(value){
        case 0xFFA25D:
            //Power ON/OFF
            Power= !Power;
            if(Power){
                digitalWrite(bluePin, 0xFF);
                digitalWrite(redPin, 0x00);
                digitalWrite(greenPin, 0x00);
                digitalWrite(d1, LOW);
                digitalWrite(d2, LOW);
                digitalWrite(d3, LOW);
                digitalWrite(d4, LOW);
            }

            if(!Power){
                digitalWrite(bluePin, LOW);
                digitalWrite(redPin, LOW);
                digitalWrite(greenPin, LOW);
                digitalWrite(d1, HIGH);
                digitalWrite(d2, HIGH);
                digitalWrite(d3, HIGH);
                digitalWrite(d4, HIGH);
            }

            delay(1500);
            break;
        }

        switch(value){
        case 0xFF22DD:
            //Play/Pause
            Play= !Play;
            if(Play){
                mainCounter =true;
                digitalWrite(bluePin, 0x00);
                digitalWrite(redPin, 0xFF);
                digitalWrite(greenPin, 0x00);
                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(2);//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(3);//Light up 7-segment display d4
                pickNumber(n%10);//Get the value of single digit
                delay(del);//delay 5ms
            }

            if(!Play){ //Pause
                mainCounter =false;
                digitalWrite(bluePin, 0xFF);
                digitalWrite(redPin, 0x00);
                digitalWrite(greenPin, 0x00);
            }

            delay(1500);
            break;
        }

        irrecv.resume();
    }

}

/**************************************/
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);
}

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);
}

     ...
}/code]

It probably is running how its coded to do. However, the remote is working along with its receiver. I tried to attach a picture of what its looking like right now, but it wouldn't post like that. The receiver is working. And the LED is turning on and off like I want it to. I have the function names outside of the void loop for the loop to call upon those functions, but it's not exactly calling upon them.

PWR ON:
RGB=GREEN
LED= ON showing 0000

PWR OFF:
RGB= OFF
LED= OFF

Play/Pause (PLAY):
RGB=RED
LED= counting up from 0-60seconds

Timer@60 seconds:
Fan=ON
Buzzer=ON for 2-4seconds

Timer Count Down from 60-0:
Fan=ON

Timer reaches 0 from countdown:
Fan=OFF
Buzzer= ON for 2-4 seconds
Timer= 0 (Doesn't count again until I hit play again)
RGB=Green (since the timer isn't running anymore)

I want it be like I can hit pause at any interval in the count up or count down sequence and it pause and then resume when I hit play again.

gcjr:
in the following, your reset "time" to "time2" in the first test so that it can never be true in the 2nd test after mainCounter. why not just increment the counter in the first test?

void loop() {

time2 = millis();
   if( time2- time >= 1000){
       time = time2;
   }

if(mainCounter == true){
       if(time2- time >= 1000){
           if( upCounter == true){
               counter++;




indented code (too long, had to prune). you could use an array of which pins to set for each digit and have a single function 



#include <ir_Lego_PF_BitStreamEncoder.h>
#include <boarddefs.h>
#include <IRremoteInt.h>
#include <IRremote.h>
int pinCount = 2; //number of pins in the array
//Define Sensor Pin
const int RECV_PIN =22;
//Define LED Constants
const int bluePin =26;
const int greenPin =28;
const int redPin =24;
//Define IR Reciever and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;
//Button Definition
boolean Power=false;
boolean Play=false;
//Define Integer to Remember Toggle State
int togglestate = 0;
unsigned long time;
unsigned long time2;
int counter;
bool upCounter = false;
bool downCounter = false;
bool mainCounter = false;
//Define Pins of 4 digit 7 segment Display
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
//Define Digital Pins of Display
int d4 = 12;
int d3 = 11;
int d2 = 10;
int d1 = 9;
//Math
long n = 0; //n represents value displayed on display
long y = 60000; //y represents start time of count down
int x = 100;
int del = 5; //value is the degree of fine tuning 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
//Timer
int timer = 500;
int value;

// -----------------------------------------------------------------------------
void setup() {
   Serial.begin(9600);
   irrecv.enableIRIn();
   //Set LEDs as Outputs
   pinMode(redPin, OUTPUT);
   pinMode(greenPin, OUTPUT);
   pinMode(bluePin, OUTPUT);
   //Set LED Display Pins as Outputs
   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);
   time = millis();
}

// -----------------------------------------------------------------------------
void loop() {
   time2 = millis();
   if( time2- time >= 1000){
       time = time2;
   }

if(mainCounter == true){
       if(time2- time >= 1000){
           if( upCounter == true){
               counter++;
               if(counter >= 60){
                   upCounter = false;
                   downCounter = true;
               }

}

else if(downCounter == true){
               counter--;
               if(counter <=0){
                   upCounter =true;
                   downCounter = false;
                   mainCounter = false;
               }

}

time = time2;
       }

}

if (irrecv.decode(&results)){
       int value = results.value;
       Serial.println(value);
       switch(value){
       case 0xFFA25D:
           //Power ON/OFF
           Power= !Power;
           if(Power){
               digitalWrite(bluePin, 0xFF);
               digitalWrite(redPin, 0x00);
               digitalWrite(greenPin, 0x00);
               digitalWrite(d1, LOW);
               digitalWrite(d2, LOW);
               digitalWrite(d3, LOW);
               digitalWrite(d4, LOW);
           }

if(!Power){
               digitalWrite(bluePin, LOW);
               digitalWrite(redPin, LOW);
               digitalWrite(greenPin, LOW);
               digitalWrite(d1, HIGH);
               digitalWrite(d2, HIGH);
               digitalWrite(d3, HIGH);
               digitalWrite(d4, HIGH);
           }

delay(1500);
           break;
       }

switch(value){
       case 0xFF22DD:
           //Play/Pause
           Play= !Play;
           if(Play){
               mainCounter =true;
               digitalWrite(bluePin, 0x00);
               digitalWrite(redPin, 0xFF);
               digitalWrite(greenPin, 0x00);
               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(2);//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(3);//Light up 7-segment display d4
               pickNumber(n%10);//Get the value of single digit
               delay(del);//delay 5ms
           }

if(!Play){ //Pause
               mainCounter =false;
               digitalWrite(bluePin, 0xFF);
               digitalWrite(redPin, 0x00);
               digitalWrite(greenPin, 0x00);
           }

delay(1500);
           break;
       }

irrecv.resume();
   }

}

/**************************************/
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);
}

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);
}

...
}/code]

using an array for each digit is smart. But how do I do that if the pins have different values per digit? I just started coding with Arduino maybe 2 months ago, so I'm still trying to learn the short cuts. So I've been doing everything the long way as I learn how to do it.

Here is a cleaned up version of the count up and down logic:

unsigned long time;
unsigned long time2;
int counter = 0;
int counterDir = 1;  //count up intially
bool mainCounter = true;

void setup() {

  Serial.begin(9600);
  time = millis();
}

void loop() {

  time2 = millis();
  if (mainCounter == true && time2 - time >= 100) {
    counter += counterDir;
    if (counter >= 60) counterDir = -1;
    if (counter <= 0) {
      counterDir = 1;
      mainCounter = false;
    }
    Serial.print("counter = ");
    Serial.println(counter);
    time = time2;
  }
}

Also, where you check the remote you have 2 switch statements, each with one case. You should have 1 switch statement with 2 cases.

ToddL1962:
An example of how to build in increments is shown below. I took everything out except for the flags and counters and quickly debugged using the following code:

unsigned long time;

unsigned long time2;
int counter;
bool upCounter = true;
bool downCounter = false;
bool mainCounter = true;

void setup() {

Serial.begin(9600);
 time = millis();
}

void loop() {

time2 = millis();
//  if ( time2 - time >= 1000) {
//    time = time2;
//  }
 if (mainCounter == true) {
   if (time2 - time >= 100) {
     if ( upCounter == true) {
       counter++;
       if (counter >= 60) {
         upCounter = false;
         downCounter = true;
       }
     }
     else if (downCounter == true) {
       counter--;
       if (counter <= 0) {
         upCounter = true;
         downCounter = false;
         mainCounter = false;
       }
     }
     Serial.print("Count = ");
     Serial.println(counter);
     time = time2;
   }
 }
}




I quickly commented out the lines that caused the counters not to work and now they are working. Now you can proceed to debug another part of the code OR figure out how to do this more efficiently.

That looks more efficient than what I have on my code. Thank you, I tried to simplify what I want the code to do in a previous comment. And that's what I had attempted to do the first time I tried using the two, and I changed it lol.

akeller5:
That looks more efficient than what I have on my code. Thank you, I tried to simplify what I want the code to do in a previous comment. And that's what I had attempted to do the first time I tried using the two, and I changed it lol.

ToddL1962:
Here is a cleaned up version of the count up and down logic:

unsigned long time;

unsigned long time2;
int counter = 0;
int counterDir = 1;  //count up intially
bool mainCounter = true;

void setup() {

Serial.begin(9600);
  time = millis();
}

void loop() {

time2 = millis();
  if (mainCounter == true && time2 - time >= 100) {
    counter += counterDir;
    if (counter >= 60) counterDir = -1;
    if (counter <= 0) {
      counterDir = 1;
      mainCounter = false;
    }
    Serial.print("counter = ");
    Serial.println(counter);
    time = time2;
  }
}




Also, where you check the remote you have 2 switch statements, each with one case. You should have 1 switch statement with 2 cases.

ToddL1962:
Here is a cleaned up version of the count up and down logic:

unsigned long time;

unsigned long time2;
int counter = 0;
int counterDir = 1;  //count up intially
bool mainCounter = true;

void setup() {

Serial.begin(9600);
  time = millis();
}

void loop() {

time2 = millis();
  if (mainCounter == true && time2 - time >= 100) {
    counter += counterDir;
    if (counter >= 60) counterDir = -1;
    if (counter <= 0) {
      counterDir = 1;
      mainCounter = false;
    }
    Serial.print("counter = ");
    Serial.println(counter);
    time = time2;
  }
}




Also, where you check the remote you have 2 switch statements, each with one case. You should have 1 switch statement with 2 cases.

I tried implementing with what you put in the coding and it says the my clearLEDs wasn't declared in the scope but it was before??

akeller5:
I tried implementing with what you put in the coding and it says the my clearLEDs wasn't declared in the scope but it was before??

Post your latest code. Probably something simple.

#include <ir_Lego_PF_BitStreamEncoder.h>
#include <boarddefs.h>
#include <IRremoteInt.h>
#include <IRremote.h>


//Define Sensor Pin
const int RECV_PIN =22;

//Define LED Constants
const int bluePin =26;
const int greenPin =28;
const int redPin =24;

//Define IR Reciever and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

//Button Definition
boolean Power=false;
boolean Play=false;

//Count UP/DOWN Logic
unsigned long time;
unsigned long time2;
int counter = 0;
int counterDir = 1; //count up intially
bool mainCounter = true;


//Define Pins of 4 digit 7 segment Display
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;

//Define Digital Pins of Display
int d4 = 12;
int d3 = 11;
int d2 = 10;
int d1 = 9;

//Math
long n = 0; //n represents value displayed on display
long y = 60000; //y represents start time of count down
int x = 100;
int del = 5; //value is the degree of fine tuning 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

//Timer
int timer = 500;
int value;

void setup() {

Serial.begin(9600);
irrecv.enableIRIn();

//Set LEDs as Outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

//Set LED Display Pins as Outputs
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);
time = millis();
}

void loop() {
 time2 = millis();
 if (mainCounter == true && time2 - time >= 100){
    counter += counterDir;
    if (mainCounter >= 60) counterDir = -1;
    if (counter <= 0){
      counterDir = 1;
      mainCounter = false;
    }
    Serial.print("counter = ");
    Serial.println(counter);
    time = time2;

 if (irrecv.decode(&results)){
 int value = results.value;
 Serial.println(value);
 switch(value){
  case 0xFFA25D:
  //Power ON/OFF
  Power= !Power;
  if(Power){
    digitalWrite(bluePin, 0xFF);
    digitalWrite(redPin, 0x00);
    digitalWrite(greenPin, 0x00);
    digitalWrite(d1, LOW);
    digitalWrite(d2, LOW);
    digitalWrite(d3, LOW);
    digitalWrite(d4, LOW);
    }
  if(!Power){
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(d1, HIGH);
    digitalWrite(d2, HIGH);
    digitalWrite(d3, HIGH);
    digitalWrite(d4, HIGH);
    }
    delay(1500);
    break;
  }
  switch(value){
    case 0xFF22DD:
      //Play/Pause
    Play= !Play;
    if(Play){
      mainCounter =true;
        digitalWrite(bluePin, 0x00);
        digitalWrite(redPin, 0xFF);
        digitalWrite(greenPin, 0x00);
        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(2);//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(3);//Light up 7-segment display d4
        pickNumber(n%10);//Get the value of single digit
        delay(del);//delay 5ms
        }
    if(!Play){ //Pause
        mainCounter =false;
        digitalWrite(bluePin, 0xFF);
        digitalWrite(redPin, 0x00);
        digitalWrite(greenPin, 0x00);
        }
        delay(1500);
        break;
      }
    irrecv.resume();
  }

}
/**************************************/
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);
}

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);
}/code]

akeller5:
using an array for each digit is smart. But how do I do that if the pins have different values per digit? I just started coding with Arduino maybe 2 months ago, so I'm still trying to learn the short cuts. So I've been doing everything the long way as I learn how to do it.

consider the following. don't have your hardware. simulated on my laptop

#include <stdio.h>

// -----------------------------------------------------------------------------
// arduino stubs

enum { LOW, HIGH };
enum { INPUT, INPUT_PULLUP, OUTPUT };

typedef unsigned char byte;

void digitalWrite (
    byte pin,
    int  state )
{
    printf ("%s: %2d, %d\n", __func__, pin, state);
}

void pinMode (
    byte pin,
    int  cfg )
{
    printf ("%s: %2d, %d\n", __func__, pin, cfg);
}

// -----------------------------------------------------------------------------
byte pinSegs [] = { 8, 7, 6, 5, 4, 3, 2 };   // g - a

#define N_SEQ   sizeof(pinSegs)

byte segCodes [] = {
    0x7e, // zero()
    0x30, // one()
    0x6d, // two()
    0x79, // three()
    0x33, // four()
    0x5b, // five()
    0x5f, // six()
    0x70, // seven()
    0x7f, // eight()
    0x7b, // nine()
};

#define N_SEQ_CODE  sizeof(segCodes)


// -----------------------------------------------------------------------------
void disp7seg (
    int  val )
{
    if (sizeof(segCodes) <= val)
        return;

    printf ("\n%s: %d\n", __func__, val);
    byte seg = segCodes [val];
    for (unsigned n = 0; n < N_SEQ; n++, val >>= 1)
        digitalWrite (pinSegs [n], val & 1 ? HIGH : LOW);
}

// -----------------------------------------------------------------------------
void setup (void)
{
    printf ("%s:\n", __func__);
    for (unsigned n = 0; n < N_SEQ; n++)
        pinMode (pinSegs [n], OUTPUT);

    for (unsigned n = 0; n <= 9; n++)
        disp7seg (n);
}

// -----------------------------------------------------------------------------
void lop (void) { }


// -----------------------------------------------------------------------------
int
main (void)
{
    setup ();
    return 0;
}

output

setup:
pinMode:  8, 2
pinMode:  7, 2
pinMode:  6, 2
pinMode:  5, 2
pinMode:  4, 2
pinMode:  3, 2
pinMode:  2, 2

disp7seg: 0
digitalWrite:  8, 0
digitalWrite:  7, 0
digitalWrite:  6, 0
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 1
digitalWrite:  8, 1
digitalWrite:  7, 0
digitalWrite:  6, 0
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 2
digitalWrite:  8, 0
digitalWrite:  7, 1
digitalWrite:  6, 0
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 3
digitalWrite:  8, 1
digitalWrite:  7, 1
digitalWrite:  6, 0
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 4
digitalWrite:  8, 0
digitalWrite:  7, 0
digitalWrite:  6, 1
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 5
digitalWrite:  8, 1
digitalWrite:  7, 0
digitalWrite:  6, 1
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 6
digitalWrite:  8, 0
digitalWrite:  7, 1
digitalWrite:  6, 1
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 7
digitalWrite:  8, 1
digitalWrite:  7, 1
digitalWrite:  6, 1
digitalWrite:  5, 0
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 8
digitalWrite:  8, 0
digitalWrite:  7, 0
digitalWrite:  6, 0
digitalWrite:  5, 1
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

disp7seg: 9
digitalWrite:  8, 1
digitalWrite:  7, 0
digitalWrite:  6, 0
digitalWrite:  5, 1
digitalWrite:  4, 0
digitalWrite:  3, 0
digitalWrite:  2, 0

u

Here is an alternative way to use arrays as well. gcjr's is just as valid also and, indeed, uses less memory.

#include <ir_Lego_PF_BitStreamEncoder.h>
#include <boarddefs.h>
#include <IRremoteInt.h>
#include <IRremote.h>

int pinCount = 2; //number of pins in the array

//Define Sensor Pin
const int RECV_PIN = 22;

//Define LED Constants
const int bluePin = 26;
const int greenPin = 28;
const int redPin = 24;

//Define IR Reciever and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;

//Button Definition
boolean Power = false;
boolean Play = false;

//Define Integer to Remember Toggle State
int togglestate = 0;

unsigned long time;
unsigned long time2;
int counter = 0;
int counterDir = 1;
bool mainCounter = false;

//Define Pins of 4 digit 7 segment Display
int SevensegPins[7] = {2,3,4,5,6,7,8};

byte SevensegDigits[10][7] = 
{
  {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW}, // 0
  {LOW,  HIGH, HIGH, LOW,  LOW,  LOW,  LOW}, // 1
  {HIGH, HIGH, LOW,  HIGH, HIGH, LOW,  LOW}, // 2
  {HIGH, HIGH, HIGH, HIGH, LOW,  LOW,  HIGH},// 3 
  {LOW,  HIGH, HIGH, LOW,  LOW,  HIGH, HIGH},// 4
  {HIGH, LOW,  HIGH, HIGH, LOW,  HIGH, HIGH},// 5
  {HIGH, LOW,  HIGH, HIGH, HIGH, HIGH, HIGH},// 6
  {HIGH, HIGH, HIGH, LOW,  LOW,  LOW,  LOW}, // 7
  {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH},// 8 
  {HIGH, HIGH, HIGH, HIGH, LOW,  HIGH, HIGH} // 9
};

//Define Digital Pins of Display
int d4 = 12;
int d3 = 11;
int d2 = 10;
int d1 = 9;

//Math
long n = 0; //n represents value displayed on display
long y = 60000; //y represents start time of count down
int x = 100;
int del = 5; //value is the degree of fine tuning 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

//Timer
int timer = 500;
int value;

void setup() {

  Serial.begin(9600);
  irrecv.enableIRIn();

  //Set LEDs as Outputs
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  //Set LED Display Pins as Outputs
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  for (int i=0; i < 7; i++) pinMode(SevensegPins[i], OUTPUT);
  time = millis();
}

void loop() {

  time2 = millis();
  if (mainCounter == true && time2 - time >= 100) {
    counter += counterDir;
    if (counter >= 60) counterDir = -1;
    if (counter <= 0) {
      counterDir = 1;
      mainCounter = false;
    }
    Serial.print("counter = ");
    Serial.println(counter);
    time = time2;
  }

  if (irrecv.decode(&results)) {
    int value = results.value;
    Serial.println(value);
    switch (value) {
      case 0xFFA25D:
        //Power ON/OFF
        Power = !Power;
        if (Power) {
          digitalWrite(bluePin, 0xFF);
          digitalWrite(redPin, 0x00);
          digitalWrite(greenPin, 0x00);
          digitalWrite(d1, LOW);
          digitalWrite(d2, LOW);
          digitalWrite(d3, LOW);
          digitalWrite(d4, LOW);
        }
        if (!Power) {
          digitalWrite(bluePin, LOW);
          digitalWrite(redPin, LOW);
          digitalWrite(greenPin, LOW);
          digitalWrite(d1, HIGH);
          digitalWrite(d2, HIGH);
          digitalWrite(d3, HIGH);
          digitalWrite(d4, HIGH);
        }
        delay(1500);
        break;
    }
    switch (value) {
      case 0xFF22DD:
        //Play/Pause
        Play = !Play;
        if (Play) {
          mainCounter = true;
          digitalWrite(bluePin, 0x00);
          digitalWrite(redPin, 0xFF);
          digitalWrite(greenPin, 0x00);
          clear7segDigit(0);//clear the 7-segment display screen
          set7segDigit(0, (n / 1000)); // get the value of thousand
          delay(del);//delay 5ms

          clear7segDigit(1);//clear the 7-segment display screen
          set7segDigit(1, (n % 1000) / 100); // get the value of hundred
          delay(del);//delay 5ms

          clear7segDigit(2);//clear the 7-segment display screen
          set7segDigit(2, n % 100 / 10); //get the value of ten
          delay(del);//delay 5ms

          clear7segDigit(3);//clear the 7-segment display screen
          set7segDigit(3, n % 10); //Get the value of single digit
          delay(del);//delay 5ms
        }
        if (!Play) { //Pause
          mainCounter = false;
          digitalWrite(bluePin, 0xFF);
          digitalWrite(redPin, 0x00);
          digitalWrite(greenPin, 0x00);
        }
        delay(1500);
        break;
    }
    irrecv.resume();
  }

}

void select7segDigit (int digit)
{ 
  //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 (digit)
  {
    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;
  }
}

void set7segDigit(int digit, int number) //clear the 7-segment display screen
{
  select7segDigit(digit);
  for (int i=0; i < 7; i++) digitalWrite(SevensegPins[i], SevensegDigits[number][i]);
}

void clear7segDigit(int digit) //clear the 7-segment display screen
{
  select7segDigit(digit);
  for (int i=0; i < 7; i++) digitalWrite(SevensegPins[i], LOW);
}