# Loop question

Hi, I am building a scoreboard for a foosball table. So basically I have a sensor that every time a ball passes it value changes and the numbers changes on the 7 segment LED. I have created all the functions for the numbers. I just need to know what kind of loop will work here. So if the ball passed by the sensor the number changes. Thanks of all the help This is what I have got so far.

``````int a = 12;
int b = 11;
int c = 10;
int d = 9;
int e = 8;
int f = 7;
int g = 6;
int dp = 5;
int ls1 = 1;
int ls2 = 2;
int ls3 = 3;
int ls4 = 4;
int inputPin = 13;
int val = 0;

void digital_0(void)
{
unsigned char j;
digitalWrite(dp,LOW);
digitalWrite(g,LOW);
for(j=7;j<=12;j++)
digitalWrite(j,HIGH);
}

void digital_1(void)
{
unsigned char j;
digitalWrite(a,LOW);
for(j=5;j<=9;j++)
digitalWrite(j,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}

void digital_2(void)
{
unsigned char j;
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(dp,LOW);
for(j=11;j<=12;j++)
digitalWrite(j,HIGH);
for(j=8;j<=9;j++)
digitalWrite(j,HIGH);
digitalWrite(g,HIGH);
}

void digital_3(void)
{
unsigned char j;
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(dp,LOW);
for(j=9;j<=13;j++)
digitalWrite(j,HIGH);
digitalWrite(g,HIGH);
}

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

void digital_5(void)
{
unsigned char j;
digitalWrite(b,LOW);
digitalWrite(e,LOW);
digitalWrite(dp,LOW);

digitalWrite(a,HIGH);
for(j=6;j<=7;j++)
digitalWrite(j,HIGH);
for(j=9;j<=10;j++)
digitalWrite(j,HIGH);
}

void digital_6(void)
{
unsigned char j;
digitalWrite(b,LOW);
digitalWrite(dp,LOW);

digitalWrite(a,HIGH);
for(j=6;j<=10;j++)
digitalWrite(j,HIGH);
}

void digital_7(void)
{
unsigned char j;
for(j=5;j<=9;j++)
digitalWrite(j,LOW);
for(j=10;j<=12;j++)
digitalWrite(j,HIGH);
}

void digital_8(void)
{
unsigned char j;
digitalWrite(dp,LOW);
for(j=6;j<=12;j++)
digitalWrite(j,HIGH);
}

void digital_9(void)
{
unsigned char j;
digitalWrite(e,LOW);
digitalWrite(dp,LOW);

for(j=9;j<=12;j++)
digitalWrite(j,HIGH);
for(j=6;j<=7;j++)
digitalWrite(j,HIGH);
}

void setup()
{
int i;
for(i=1;i<=12;i++)
pinMode(i,OUTPUT);
pinMode(inputPin,INPUT);

}
void loop()
{

if(val == HIGH)
digital_0();

}
``````

I just need to know what kind of loop will work here.

Where is "here"? Do you want to output 0, then 1, then 2, etc. whenever the sensor returns HIGH?

You should be aware of two things. First, the sensor may return HIGH for several iterations of loop. Presumably, you only want to increment the score once whenever the sensor goes HIGH, until it goes LOW again.

Second, there may be some bouncing, depending on the type of sensor being read. You need to deal with that, too.

So, the first thing to do is to create a variable to hold the score (the number of times that the sensor has transitioned to HIGH.

The second thing to do is to determine when a transition occurs. This happens when the sensor returns a value that is not the same as last time. This implies, of course, that you need to keep track of what the sensor returned last time.

The third thing you need to do is to decide whether debouncing is necessary. If so, you need to do that.

Finally, it is the score that you want to show. So, can the score be greater than 10? If not, a switch statement, on the score, is easy to use, with cases for 0 through 9.

Thanks for helping me with this. I am still confused about how to do this…sorry very new at programming and electronics. So basically I create a switch statement in the void loop() ? But when I create anything in the void loop area it goes through it all the way. I don’t know how to make it stop and wait for the sensor to go HIGH?. Thanks again for the help!

Also I created a variable but how can I store the past score in that variable? Thanks!

Thanks... Would you be able to post the part of the while loop?...thanks

The loop function already loops. So you hardly need another loop.

As PaulS was trying to tell you, what you really want is the transition from high to low (or low to high), and only get one transition. Otherwise as the ball passes the sensor, the score will go up like crazy.

Assuming high means a ball is there, something like this:

``````int score;
byte oldval;

void loop()
{

// see if sensor has just gone high
if (val == HIGH && oldval == LOW)
{
score++;  // new score

switch (score)
{
case 0: digital_0(); break;
case 1: digital_1(); break;
case 2: digital_2(); break;
case 3: digital_3(); break;
case 4: digital_4(); break;
case 5: digital_5(); break;
case 6: digital_6(); break;
case 7: digital_7(); break;
case 8: digital_8(); break;
case 9: digital_9(); break;

}  // end switch

}  // end if

oldval = val;

} // end loop
``````

You still have a few issues, like if the score is greater than 9, and if the switch bounces. You also have to show the initial zero, and have a way of resetting for a new game.

Thank a lot for your help!...I have tried uploading the sketch and nothing shows up on the Digit LED if something passes in front of the sensor. For now I will only have the score up to 9. I will probably have a switch to reset the game.

I have tried uploading the sketch and nothing shows up on the Digit LED if something passes in front of the sensor.

You are not enabling the internal pullup resistor. So, you must have an external pullup or pulldown resistor wired with the switch. The test for HIGH implies a pulldown resistor. How is your switch wired?

Thanks Paul. Right now I don't have a switch connected just the IR sensor and if the write digital_0(); outside the switch case loop the 0 digit shows up on LED. Do I need to have a physical switch to change the digits?

I don't have a switch connected just the IR sensor

Which you want to act like a switch, right?

What kind of IR sensor is it? Is it a digital sensor?

Have you tried a simple sketch, that just prints Pass or Fail based on whether the sensor is reporting HIGH or LOW, and are you certain that you are getting the correct data from the sensor under all circumstances?

Still seems like a floating pin condition to me.

Thanks Paul, yes you are right it is a switch :).

This is a digital sensor below is the info on the sensor

Model: E18-D80NK

Features : - Adjustable Infrared Sensor Switch is a set of transmitter and receiver in one of the photoelectric switch sensor. The detection distance can be adjusted according to the demand. The sensor has a detection range of 3cm-80cm. The switching signal output differs in accordance to the obstacles. It remains high when no obstacles and remains low when there are obstacles. - Useful for robot, interactive media, industrial assembly line, etc. Specification - Voltage:DC 5V -Current: 100mA - Sensing Distance :3 - 50 CM -NPN Normal Open Type - Sensor diameter: 18mm -Sensor Length: 43mm -Lead Length: 45mm -Pin Definition: red (power "+" ) and green(power "-" ), yellow (signal date).

I haven't tried the pass and fail yet. Let me put together a quick one see if that works. Thanks

Hey Paul so I did a quick test with the below

``````void loop()
{

if (val == HIGH)
{
digital_1();}
else {
digital_0();
}
}
``````

and when I upload the sketch it shows 1 on the LED right away and when I pass my hand in the front of the sensor it changes to 0. Shouldn't that be other way around?

when I upload the sketch it shows 1 on the LED right away and when I pass my hand in the front of the sensor it changes to 0. Shouldn't that be other way around?

The sensor reads HIGH (as in HIGH light level) when it gets plenty of light, and LOW (man, it's dark in here) when it doesn't get any light. So, the behavior seems reasonable.

You can, of course, change the behavior, by displaying 1 when LOW and 0 when HIGH.

oh I see...so that code should work right?

so that code should work right?

You already said that it did work...

Sorry Paul the below code still doesn't work. When I upload the sketch nothing shows up on the LED screens. It looks like the case loop is not working.

``````int score;
byte oldval;

void loop()
{

// see if sensor has just gone high
if (val == HIGH && oldval == LOW)
{
score++;  // new score

switch (score)
{
case 0: digital_0(); break;
case 1: digital_1(); break;
case 2: digital_2(); break;
case 3: digital_3(); break;
case 4: digital_4(); break;
case 5: digital_5(); break;
case 6: digital_6(); break;
case 7: digital_7(); break;
case 8: digital_8(); break;
case 9: digital_9(); break;

}  // end switch

}  // end if

oldval = val;

} // end loop
``````

That doesn't even compile.

sorry here is the whole code

``````int a = 12;
int b = 11;
int c = 10;
int d = 9;
int e = 8;
int f = 7;
int g = 6;
int dp = 5;
int ls1 = 1;
int ls2 = 2;
int ls3 = 3;
int ls4 = 4;
int inputPin = 13;
int val = 0;
int score;
byte oldval;

void digital_0(void)
{
unsigned char j;
digitalWrite(dp,LOW);
digitalWrite(g,LOW);
for(j=7;j<=12;j++)
digitalWrite(j,HIGH);
}

void digital_1(void)
{
unsigned char j;
digitalWrite(a,LOW);
for(j=5;j<=9;j++)
digitalWrite(j,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}

void digital_2(void)
{
unsigned char j;
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(dp,LOW);
for(j=11;j<=12;j++)
digitalWrite(j,HIGH);
for(j=8;j<=9;j++)
digitalWrite(j,HIGH);
digitalWrite(g,HIGH);
}

void digital_3(void)
{
unsigned char j;
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(dp,LOW);
for(j=9;j<=13;j++)
digitalWrite(j,HIGH);
digitalWrite(g,HIGH);
}

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

void digital_5(void)
{
unsigned char j;
digitalWrite(b,LOW);
digitalWrite(e,LOW);
digitalWrite(dp,LOW);

digitalWrite(a,HIGH);
for(j=6;j<=7;j++)
digitalWrite(j,HIGH);
for(j=9;j<=10;j++)
digitalWrite(j,HIGH);
}

void digital_6(void)
{
unsigned char j;
digitalWrite(b,LOW);
digitalWrite(dp,LOW);

digitalWrite(a,HIGH);
for(j=6;j<=10;j++)
digitalWrite(j,HIGH);
}

void digital_7(void)
{
unsigned char j;
for(j=5;j<=9;j++)
digitalWrite(j,LOW);
for(j=10;j<=12;j++)
digitalWrite(j,HIGH);
}

void digital_8(void)
{
unsigned char j;
digitalWrite(dp,LOW);
for(j=6;j<=12;j++)
digitalWrite(j,HIGH);
}

void digital_9(void)
{
unsigned char j;
digitalWrite(e,LOW);
digitalWrite(dp,LOW);

for(j=9;j<=12;j++)
digitalWrite(j,HIGH);
for(j=6;j<=7;j++)
digitalWrite(j,HIGH);
}

void digital_10(void)
{

digitalWrite(e,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(b,LOW);
digitalWrite(dp,LOW);

digitalWrite(a,HIGH);
delay(500);
digitalWrite(g,HIGH);
delay(500);
digitalWrite(d,HIGH);
delay(500);
}

void setup()
{
int i;
for(i=1;i<=12;i++)
pinMode(i,OUTPUT);
pinMode(inputPin,INPUT);
}

void loop()
{

//digital_0(); //show inital 0

// see if sensor has just gone high
if (val == HIGH  && oldval == LOW)
{
score++;  // new score

switch (score)
{
case 0: digital_0(); break;
case 1: digital_1(); break;
case 2: digital_2(); break;
case 3: digital_3(); break;
case 4: digital_4(); break;
case 5: digital_5(); break;
case 6: digital_6(); break;
case 7: digital_7(); break;
case 8: digital_8(); break;
case 9: digital_9(); break;

}  // end switch

}  // end if

oldval = val;

} // end loop
``````

Have you not been introduced to the use of Serial.begin(), Serial.print(), Serial.println() and the Serial Monitor for debugging? It is not necessary to do everything blind.

Hey Paul never used the monitor before. I will try playing around with that. Also the Serial command are for the analog sensor or will it work for digital as well? Thanks