Great info thanks econjack. Crossroads, the array thing made me want to run away to my mummy... riding a pink vespa...
I put in my new knowledge about changing variables... it seems to sort of do it...
Here's the code. I have some kind of craziness going on where it will lose 1 life on the first hit but then two or three on the next successive one's
#define RCVR 2 //IR RCVR Signal pin
#define RPOS 5 //IR RCVR positive pin
#define EMIT 3 //IR LED pin
#define TS 13 //Team Switch Common
#define TA 12 // Team Switch is A
#define TB 7 // Team Switch is B
#define TRIG 4 //Trigger pin
#define lONE 9 //One life left
#define lTWO 10 //Two lives left
#define lTHREE 11 //Three lives left
int life; // Life Counter, Get three on reset
int pLen; // To be used to measure Pulse Length
int lenCount; //Counts the number of incoming pulses
int aHit; //Gives number of A Pulses that equals a hit
int bHit; //Gives number of B Pulses that equals a hit
int sigGEN; //Makes the A and B Signals
void setup()
{
pinMode (RCVR, INPUT);
pinMode (RPOS, OUTPUT);
pinMode (EMIT, OUTPUT);
pinMode (TS, OUTPUT);
pinMode (TA, INPUT);
pinMode (TB, INPUT);
pinMode (TRIG, INPUT);
pinMode (lONE, OUTPUT);
pinMode (lTWO, OUTPUT);
pinMode (lTHREE, OUTPUT);
if(life == 0) //There are 0 lives at start
{life++; //add first
life++; //add second
life++; //add third
}
Serial.begin(9600); // open a serial connection to your computer
}
void loop()
{
if(life == 3)
{digitalWrite(lONE, HIGH); //
digitalWrite(lTWO, HIGH);
digitalWrite(lTHREE, HIGH);
}
if(life == 2)
{digitalWrite(lONE, HIGH); //
digitalWrite(lTWO, HIGH);
digitalWrite(lTHREE, LOW);
}
if(life == 1)
{digitalWrite(lONE, HIGH); //
digitalWrite(lTWO, LOW);
digitalWrite(lTHREE, LOW);
}
if(life == 0)
{digitalWrite(lONE, LOW); //
digitalWrite(lTWO, LOW);
digitalWrite(lTHREE, LOW);
}
if(life < 0)
{digitalWrite(lONE, LOW); //
digitalWrite(lTWO, LOW);
digitalWrite(lTHREE, LOW);
}
digitalWrite(RPOS, HIGH); //Turn on the Receiver
if (digitalRead(RCVR)== 0) // If the RCVR pin has started to receive
{for (int lenCount=0; lenCount <8; lenCount++) // Do the following 10x
{pLen = pulseIn(RCVR, LOW); //Measure the length of the received pulse
if (pLen > 8000) //If it is more than 8000
{if(pLen < 12000) //and less than 12000
{aHit++; //Count one pulse for A
}
}
if (pLen > 27000) //If it is more than 8000
{if(pLen < 31000) //and less than 12000
{ bHit++; //Count one pulse for B
}
}
} //Move on if count gets to <8
Serial.print(life);
if(aHit > 5) //If there are more than 5 A pulses
{digitalWrite(TS,HIGH); //Make the Team Switch live
if(digitalRead(TA) == HIGH) //If Team Switch is A (Friendly Fire)
{life-=2; //Lose 2 lives
}
if(digitalRead(TB) == HIGH) //If Team Switch is B (Enemy Fire)
{life--; //Lose 1 life
}
}
if(bHit > 5) //If there are more than 5 A pulses
{digitalWrite(TS,HIGH); //Make the Team Switch live
if(digitalRead(TB) == HIGH) //If Team Switch is B (Friendly Fire)
{life-=2; //Lose 2 lives
}
if(digitalRead(TA) == HIGH) //If Team Switch is A (Enemy Fire)
{life--; //Lose 1 life
}
}
Serial.print(life);
Serial.println();
}
aHit = 0; //Reset aHit
bHit = 0; //Reset bHit
}
Here's the serial after a few shots.. 3 being the lives before, 2 after.
32
20
0-3
-3-6
-6-9
-9-12
-12-14
-14-16
-16-19
-19-21
-21-24