Counter for LDR with sevseg

Hey,

I have a setup were I want to display a specific number on a seven-segment display, a number each time for two rounds, when a Light Decreasing Resistance sensor gets activated.

Below is the code I'm using which works great except that it loops in the display for ever:

#include "SevSeg.h"
SevSeg sevseg; 
int ldr = A0; 
int val ; 
int segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};
void setup(){
  byte numDigits = 4;
  byte digitPins[] = {10, 11, 12, 13};
  byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};

  bool resistorsOnSegments = true; 
  bool updateWithDelaysIn = true;
  byte hardwareConfig = COMMON_CATHODE; 
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(90);
}




void loop(){
int x = 0;
    val = analogRead(ldr);
    if(val>500){
x=1;}
if(x==1){
    sevseg.setNumber(1234);
    sevseg.refreshDisplay();
}
else(x==0);{
    sevseg.refreshDisplay(); 
    delay(1000);
    }
    return(0);
}
 else(x == 0);

Is that the right syntax for an else clause? (hint: no)

int segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};
void setup(){
  byte numDigits = 4;
  byte digitPins[] = {10, 11, 12, 13};
  byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};

  bool resistorsOnSegments = true;
  bool updateWithDelaysIn = true;

Why declare the segment pins twice?

Do you know that all of those variables that are declared in setup() will no longer exist once setup() is finished. They go out of scope.

Thanks for fast reply. and excuse me for I am a noob.

could you please help me with the correct code? I'd appreciate it a lot.

What happens if you change

else(x==0);{

to

else {

I have no way to test the code, but here it is with the corrections as best as I can do. I move the variable declarations from setup().

#include "SevSeg.h"
SevSeg sevseg;
int ldr = A0;
int val ;
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};
bool resistorsOnSegments = true;
bool updateWithDelaysIn = true;
byte hardwareConfig = COMMON_CATHODE;

void setup()
{
   sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
   sevseg.setBrightness(90);
}

void loop()
{
   int x = 0;
   val = analogRead(ldr);
   if (val > 500)
   {
      x = 1;
   }
   else  // ****** must reset x if below 500
   {
      x = 0;
   }
   if (x == 1)
   {
      sevseg.setNumber(1234);
      sevseg.refreshDisplay();
   }
   else
   {
      sevseg.refreshDisplay();
      delay(1000);
   }
   //return (0);   ****** this is useless
}