Hi I have a foosball table that I want the scoring to be electronic. I've figured out the schematic and some of the programing.
I used these 2 sites as references and followed their schematics:
http://www.thebox.myzen.co.uk/Hardware/Sneak_Thief.html
My code
int x=0; //used for for loopss
const int irPin = A0;
const int irPin2 = A1;
int value = 0; //the variables I use for counting
int value2 = 0;
int counter = 0;
int counter2 = 0;
int lastState = 0;
int lastState2 = 0;
void setup()
{
DDRD=0xff; // all pins 0-7 OUTPUTs
DDRB=0xff; // all pins 8-13 OUTPUTs
PORTD=0x00; // make pins 0-7 LOWs
PORTB=0x00; // make pins 8-13 LOWs
Serial.begin(9600);
}
void loop()
{
value = analogRead(irPin);
value2 = analogRead(irPin2);
if(counter < 1){
turnOnSquare(3);
displayDigit(0);
delay(2);
turnOnSquare(5);
displayDigit(10);
delay(2);
turnOnSquare(2);
displayDigit(0);
delay(2);
if(counter2 < 1){
turnOnSquare(3);
displayDigit(0);
delay(2);
turnOnSquare(5);
displayDigit(10);
delay(2);
turnOnSquare(2);
displayDigit(0);
delay(2);
}
if(value != lastState && value != lastState + 1) {
if (value < 127 || value > 133) {
counter++;
Serial.println("on");
Serial.print("points: ");
Serial.println(counter);
Serial.println(value);
turnOnSquare(3);
displayDigit(counter);
delay(50);
if(counter >= 10){
while(1){
turnOnSquare(3);
displayDigit(1);
delay(2);
turnOnSquare(4);
displayDigit(0);
delay(2);
turnOnSquare(5);
displayDigit(10);
}
}
else {
Serial.println("off");
Serial.println(value);
}
}
if(value2 != lastState2 && value2 != lastState2 + 1) {
if (value2 < 127 || value2 > 133) {
counter2++;
Serial.println("on");
Serial.print("points: ");
Serial.println(counter2);
Serial.println(value2);
turnOnSquare(2);
displayDigit(counter2);
delay(50);
if(counter2 >= 10){
while(1){
turnOnSquare(2);
displayDigit(1);
delay(2);
turnOnSquare(1);
displayDigit(0);
delay(2);
turnOnSquare(5);
displayDigit(10);
}
}
else {
Serial.println("off");
Serial.println(value);
}
}
lastState = value;
lastState2 = value2;
}
}
}
}
void turnOnSquare(int num)
{
int d1=9,d2=10,d3=11,d4=12;
switch(num)
{
case 1:
digitalWrite(d2,LOW);
digitalWrite(d3,LOW);
digitalWrite(d4,LOW);
digitalWrite(d1,HIGH);
break;
case 2:
digitalWrite(d1,LOW);
digitalWrite(d3,LOW);
digitalWrite(d4,LOW);
digitalWrite(d2,HIGH);
break;
case 3:
digitalWrite(d1,LOW);
digitalWrite(d2,LOW);
digitalWrite(d4,LOW);
digitalWrite(d3,HIGH);
break;
case 4:
digitalWrite(d1,LOW);
digitalWrite(d2,LOW);
digitalWrite(d3,LOW);
digitalWrite(d4,HIGH);
break;
case 5:
digitalWrite(d1,LOW);
digitalWrite(d2,LOW);
digitalWrite(d3,LOW);
digitalWrite(d4,LOW); // this is so I can cleanly turn off the display
break;
}
}
void displayDigit(int num)
{
switch(num)
{
case 0:
PORTD=B00000011; // pins 2-7 on
digitalWrite(8,HIGH); // turn off pin 8
break;
case 1:
PORTD=B11100111; // only pins 2 and 3 are on
digitalWrite(8,HIGH); // turn off pin 8
break;
case 2:
PORTD=B10010011; // only pins 2,3,5 and 6 on
digitalWrite(8,LOW); // segment g on
break;
case 3:
PORTD=B11000011; // only pins 2,3,4 and 5 on
digitalWrite(8,LOW); // segment g on
break;
case 4:
PORTD=B01100111; // only pins 3,4 and 7 on
digitalWrite(8,LOW); // segment g on
break;
case 5:
PORTD=B01001011; //B10110100; // only pins 2,4,5 and 7 on
digitalWrite(8,LOW); // segment g on
break;
case 6:
PORTD=B00001011; //B11110100; // only pins 2,4,5,6 and 7 on
digitalWrite(8,LOW); // segment g on
break;
case 7:
PORTD=B11100011; // only pins 2,3 and 4 on
digitalWrite(8,HIGH); // segment g off
break;
case 8:
PORTD=B00000011; // pins 2-7 on
digitalWrite(8,LOW); // turn on pin 8
break;
case 9:
PORTD=B01000011; // only pins 2,3, 4 and 5 on
digitalWrite(8,LOW); // segment g on
break;
case 10:
PORTD=B11111111;
digitalWrite(8, HIGH);
break; //tunr off every thing
}
}
Ok so what happens when I turn it on is it displays 0:0 then is I trip the first ir sensor it reads :1 I want it to read 0:1 and it wont keep counting from there. Also I cant get the first digit to count like the second. Oh and value2 is for the first number. The ir sensor for value2 works fine and so does the display so its a software problem. Please help.