Reset 7 segment

I have written a program that uses a PIR sensor, a servo, a relay (output 13) and a 7-segment timer. The 7-segment counts up every time the PIR-sensor detects motion. I would like to be able to re-set and re-start the 7-segment at will. I have tried using a simple key code but that failed. I am including my code below. Any help would appreciated.

Many thanks,
Thomas.

arduino_piesths.ino (5.84 KB)

I can't see your code on my device and many others with phones, etc. can't either. If you post your code as directed in the how to use this forum-please read stickies, everyone can see it.

Because of all those delay() calls your processor is spending most of the time doing absolutely nothing. The beginner's guide to millis() shows how to do non blocking timing.

Read this to see how to post code. Use autoformat to make your code easier to follow.

#define segA 2//connecting segment A to PIN2

#define segB 3// connecting segment B to PIN3

#define segC 4// connecting segment C to PIN4

#define segD 5// connecting segment D to PIN5

#define segE 6// connecting segment E to PIN6

#define segF 7// connecting segment F to PIN7

#define segG 8// connecting segment G to PIN8
#include <Servo.h>
Servo MyServo;
const int SERVO_PIN = 10;
const int PIR_PIN = 12;
const int PIR_POS_PIN = 13;

 

                int COUNT=1;//count integer for 0-9 increment

void setup()

{
Serial.begin(9600);
  MyServo.attach(SERVO_PIN);    //
  MyServo.write(0);
  pinMode(PIR_PIN, INPUT);
  pinMode(PIR_POS_PIN, OUTPUT);
  digitalWrite(PIR_POS_PIN, HIGH);
  Serial.println("SENSOR ACTIVE");
                  for (int i=2;i<9;i++)

                  {
pinMode(13, OUTPUT);
                   pinMode(i, OUTPUT);// taking all pins from 2-8 as output
                 
                  }

}

 

void loop()

{
   if (digitalRead(PIR_PIN) == HIGH) {
    Serial.println("---");
    Serial.print("motion detected at ");
    Serial.print(millis() / 1000);
    Serial.println("sec");
    digitalWrite(13, HIGH);
  delay(6000); // Wait for 1000 millisecond(s) 
    MyServo.write(60);
    delay(2000);
    MyServo.write(0);
    delay(3000); 
    digitalWrite(13, LOW); 
  

switch (COUNT)
 

                {
  

 

                case 0://when count value is zero show”0” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, LOW);

                digitalWrite(segG, HIGH);

                break;

 

                case 1:// when count value is 1 show”1” on disp

                digitalWrite(segA, HIGH);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, HIGH);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, HIGH);

                break;

 

                case 2:// when count value is 2 show”2” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, HIGH);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, LOW);

                break;

 

                case 3:// when count value is 3 show”3” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, LOW);

                break;

 

                case 4:// when count value is 4 show”4” on disp

                digitalWrite(segA, HIGH);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, HIGH);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 5:// when count value is 5 show”5” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, HIGH);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 6:// when count value is 6 show”6” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, HIGH);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 7:// when count value is 7 show”7” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, HIGH);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, HIGH);

                break;

 

                case 8:// when count value is 8 show”8” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 9:// when count value is 9 show”9” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                break;

                }

                if (COUNT<10)

                {
  
COUNT++;
                                delay(1000);///increment count integer for every second

                }
   
                if (COUNT==10)
     {

                                COUNT=0;// if count integer value is equal to 10, reset it to zero.

                                delay(1000);

                }
  }
}

The answer is most likely in reply #2. But you should show your failed attempt; I don't see how you try to reset the counter in your code.

Further I advise to use tools->auto format in the IDE to properly indent your code. Also, empty lines are useful to separate sections of code, but don't over-do it.

Personally I consider the below more readable.

#define segA 2//connecting segment A to PIN2
#define segB 3// connecting segment B to PIN3
#define segC 4// connecting segment C to PIN4
#define segD 5// connecting segment D to PIN5
#define segE 6// connecting segment E to PIN6
#define segF 7// connecting segment F to PIN7
#define segG 8// connecting segment G to PIN8

#include <Servo.h>
Servo MyServo;
const int SERVO_PIN = 10;
const int PIR_PIN = 12;
const int PIR_POS_PIN = 13;



int COUNT = 1; //count integer for 0-9 increment

void setup()

{
  Serial.begin(9600);

  MyServo.attach(SERVO_PIN);    //
  MyServo.write(0);

  pinMode(PIR_PIN, INPUT);
  pinMode(PIR_POS_PIN, OUTPUT);
  digitalWrite(PIR_POS_PIN, HIGH);
  Serial.println("SENSOR ACTIVE");
  for (int i = 2; i < 9; i++)
  {
    pinMode(13, OUTPUT);
    pinMode(i, OUTPUT);// taking all pins from 2-8 as output
  }
}



void loop()
{
  if (digitalRead(PIR_PIN) == HIGH)
  {
    Serial.println("---");
    Serial.print("motion detected at ");
    Serial.print(millis() / 1000);
    Serial.println("sec");
    digitalWrite(13, HIGH);

    delay(6000); // Wait for 1000 millisecond(s)
    MyServo.write(60);
    delay(2000);
    MyServo.write(0);
    delay(3000);
    digitalWrite(13, LOW);

    switch (COUNT)
    {
      case 0://when count value is zero show"0" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, LOW);
        digitalWrite(segC, LOW);
        digitalWrite(segD, LOW);
        digitalWrite(segE, LOW);
        digitalWrite(segF, LOW);
        digitalWrite(segG, HIGH);
        break;
      case 1:// when count value is 1 show"1" on disp
        digitalWrite(segA, HIGH);
        digitalWrite(segB, LOW);
        digitalWrite(segC, LOW);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 2:// when count value is 2 show"2" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, LOW);
        digitalWrite(segC, HIGH);
        digitalWrite(segD, LOW);
        digitalWrite(segE, LOW);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, LOW);
        break;
      case 3:// when count value is 3 show"3" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, LOW);
        digitalWrite(segC, LOW);
        digitalWrite(segD, LOW);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, LOW);
        break;
      case 4:// when count value is 4 show"4" on disp
        digitalWrite(segA, HIGH);
        digitalWrite(segB, LOW);
        digitalWrite(segC, LOW);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, LOW);
        digitalWrite(segG, LOW);
        break;
      case 5:// when count value is 5 show"5" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, LOW);
        digitalWrite(segD, LOW);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, LOW);
        digitalWrite(segG, LOW);
        break;
      case 6:// when count value is 6 show"6" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, HIGH);
        digitalWrite(segC, LOW);
        digitalWrite(segD, LOW);
        digitalWrite(segE, LOW);
        digitalWrite(segF, LOW);
        digitalWrite(segG, LOW);
        break;
      case 7:// when count value is 7 show"7" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, LOW);
        digitalWrite(segC, LOW);
        digitalWrite(segD, HIGH);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, HIGH);
        digitalWrite(segG, HIGH);
        break;
      case 8:// when count value is 8 show"8" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, LOW);
        digitalWrite(segC, LOW);
        digitalWrite(segD, LOW);
        digitalWrite(segE, LOW);
        digitalWrite(segF, LOW);
        digitalWrite(segG, LOW);
        break;
      case 9:// when count value is 9 show"9" on disp
        digitalWrite(segA, LOW);
        digitalWrite(segB, LOW);
        digitalWrite(segC, LOW);
        digitalWrite(segD, LOW);
        digitalWrite(segE, HIGH);
        digitalWrite(segF, LOW);
        digitalWrite(segG, LOW);
        break;
    }

    if (COUNT < 10)
    {
      COUNT++;
      delay(1000);///increment count integer for every second
    }

    if (COUNT == 10)
    {
      COUNT = 0; // if count integer value is equal to 10, reset it to zero.
      delay(1000);
    }
  }
}

Thank you for the comment.

This my attempt to add a re-set button. Any advice would be appreciated....

#define segA 2//connecting segment A to PIN2

#define segB 3// connecting segment B to PIN3

#define segC 4// connecting segment C to PIN4

#define segD 5// connecting segment D to PIN5

#define segE 6// connecting segment E to PIN6

#define segF 7// connecting segment F to PIN7

#define segG 8// connecting segment G to PIN8
#include <Servo.h>
Servo MyServo;
const int SERVO_PIN = 10;
const int PIR_PIN = 12;
const int PIR_POS_PIN = 13;

int BUTTON = 9;
 

                int COUNT=1;//count integer for 0-9 increment

void setup()

{
Serial.begin(9600);
  MyServo.attach(SERVO_PIN);    //
  MyServo.write(0);
  pinMode(PIR_PIN, INPUT);
  pinMode(PIR_POS_PIN, OUTPUT);
  digitalWrite(PIR_POS_PIN, HIGH);
  Serial.println("SENSOR ACTIVE");
                  for (int i=2;i<9;i++)

                  {
pinMode(13, OUTPUT);
                   pinMode(i, OUTPUT);// taking all pins from 2-8 as output
                 pinMode(BUTTON,INPUT);
                    
                  }

}

 

void loop()
{
 
 
  
    
     if(digitalRead(BUTTON) == HIGH){
    COUNT=0;
     }
  
  if(digitalRead(PIR_PIN) == HIGH){
  
    Serial.println("---");
    Serial.print("motion detected at ");
    Serial.print(millis() / 1000);
    Serial.println("sec");
    digitalWrite(13, HIGH);
   
  delay(6000); // Wait for 1000 millisecond(s) 
    MyServo.write(60);
    delay(2000);
    MyServo.write(0);
    delay(3000); 
    digitalWrite(13, LOW);
  
  
     
    
  

switch (COUNT)
 

                {
  

 

                case 0://when count value is zero show”0” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, LOW);

                digitalWrite(segG, HIGH);

                break;

 

                case 1:// when count value is 1 show”1” on disp

                digitalWrite(segA, HIGH);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, HIGH);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, HIGH);

                break;

 

                case 2:// when count value is 2 show”2” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, HIGH);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, LOW);

                break;

 

                case 3:// when count value is 3 show”3” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, LOW);

                break;

 

                case 4:// when count value is 4 show”4” on disp

                digitalWrite(segA, HIGH);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, HIGH);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 5:// when count value is 5 show”5” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, HIGH);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 6:// when count value is 6 show”6” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, HIGH);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 7:// when count value is 7 show”7” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, HIGH);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, HIGH);

                digitalWrite(segG, HIGH);

                break;

 

                case 8:// when count value is 8 show”8” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, LOW);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

                break;

 

                case 9:// when count value is 9 show”9” on disp

                digitalWrite(segA, LOW);

                digitalWrite(segB, LOW);

                digitalWrite(segC, LOW);

                digitalWrite(segD, LOW);

                digitalWrite(segE, HIGH);

                digitalWrite(segF, LOW);

                digitalWrite(segG, LOW);

  
                break;

 
         

                break;


     
  if (COUNT<10)

                {
  
COUNT++;
                                delay(1000);///increment count integer for every second

                }
   
                if (COUNT==10)
     {

                                COUNT=0;// if count integer value is equal to 10, reset it to zero.

                                delay(1000);
    
    }
  }
  }
  }

The formatting (or lack thereof) makes the code very hard to read. Please heed the advice of sterretje.

Further I advise to use tools->auto format in the IDE to properly indent your code. Also, empty lines are useful to separate sections of code, but don't over-do it.

What does the code do? How does that differ from what you want? How is the switch wired?

Further I advise to use tools->auto format in the IDE to properly indent your code. Also, empty lines are useful to separate sections of code, but don't over-do it.

Any advice would be appreciated....

Apparently not.

Quote

Any advice would be appreciated....

Apparently not.

"Advice appreciated" and "take action on advice" are independent concepts to the OP.

...