Comparing Long Integers I've Created

My Code:

if(p1reactiontime > p2reactiontime)
{
digitalWrite(p1yellow1,HIGH);
}
else if(p1reactiontime < p2reactiontime)
{
digitalWrite(p2yellow1,HIGH);
}

Could some explain to me why this type of comparison will not work for me?

I’ve been stumped on this for a while now and I really could use some help.

My full code:

int p1yellow1 = 22;
int p1yellow2 = 24;
int p1yellow3 = 26;
int p1green = 28;
int p1red = 30;
int p2yellow1 = 23;
int p2yellow2 = 25;
int p2yellow3 = 27;
int p2green = 29;
int p2red = 31;
const int p1 = 34;
const int p2 = 36;
int start = 32;
int p1redlighted=0;
int p2redlighted=0;
int w1=0;
int y1=0;
int z1=0;
int p11=0;
int p22=0;
long starttime;
long p1pushtime;
long p2pushtime;
long p1reactiontime;
long p2reactiontime;

void setup()
{
Serial.begin(115200);
pinMode(p1yellow1, OUTPUT);
pinMode(p1yellow2, OUTPUT);
pinMode(p1yellow3, OUTPUT);
pinMode(p1green, OUTPUT);
pinMode(p1red, OUTPUT);
pinMode(p2yellow1, OUTPUT);
pinMode(p2yellow2, OUTPUT);
pinMode(p2yellow3, OUTPUT);
pinMode(p2green, OUTPUT);
pinMode(p2red, OUTPUT);
pinMode(start,INPUT);
pinMode(p1, INPUT);
pinMode(p2, INPUT);
}
void loop()
{
p11 = digitalRead(p1);
p22 = digitalRead(p2);
digitalWrite(p1yellow1,LOW);
digitalWrite(p2yellow1,LOW);
digitalWrite(p1yellow2,LOW);
digitalWrite(p2yellow2,LOW);
digitalWrite(p1yellow3,LOW);
digitalWrite(p2yellow3,LOW);

if (digitalRead (start)==HIGH)
{
w1=1;
while (w1==1)
{
y1=0;
p1redlighted=0;
p2redlighted=0;
digitalWrite(p1green,LOW);
digitalWrite(p2green,LOW);
delay(2000);
digitalWrite(p1yellow1,HIGH);
digitalWrite(p2yellow1,HIGH);
delay(250);
digitalWrite(p1yellow1,LOW);
digitalWrite(p2yellow1,LOW);
delay(250);
digitalWrite(p1yellow2,HIGH);
digitalWrite(p2yellow2,HIGH);
delay(250);
digitalWrite(p1yellow2,LOW);
digitalWrite(p2yellow2,LOW);
delay(250);
digitalWrite(p1yellow3,HIGH);
digitalWrite(p2yellow3,HIGH);
delay(250);
digitalWrite(p1yellow3,LOW);
digitalWrite(p2yellow3,LOW);
delay(250);
digitalWrite(p1green,HIGH);
digitalWrite(p2green,HIGH);
starttime=micros();
w1=0;
}
}
if(p1redlighted==1)
{
digitalWrite(p1red,HIGH);
}
if((p11==HIGH&&p22==HIGH)&&y1==0&&w1==0)
{
p1pushtime=micros();
p2pushtime=micros();
digitalWrite(p1green,LOW);
digitalWrite(p2green,LOW);
p1reactiontime=(p1pushtime-starttime);
p2reactiontime=(p2pushtime-starttime);
y1=1;
z1=1;
if(z1==1)
{
Serial.println (“Player 1”);
Serial.println(p1reactiontime);
Serial.println("");
Serial.println (“Player 2”);
Serial.println(p2reactiontime);
Serial.println("");
z1=0;
}
z1=0;
y1=1;
}
if(p1reactiontime > p2reactiontime)
{
digitalWrite(p1yellow1,HIGH);
}
else if(p1reactiontime < p2reactiontime)
{
digitalWrite(p2yellow1,HIGH);
}
}

What do you mean by "not work"?

Post your code.

It seems to me you're forgetting the == condition. Why even have an "else if", just use "else", and adjust the first if statement to include (or not) the =.

Does anything go super nova if the two reaction times equal each other?

Post your full code

...R

I posted my full code.

And what happens to those pins about 3 microseconds after you set them HIGH?

It would've saved a load of everyone's time if you'd posted all your code to begin with.

Two things - please don't make amendments to earlier Posts that make subsequent comments look stupid please use code tags for code

so it looks like this and is easy to read and easy to copy

You should define variables associated with micros() and millis() as unsigned long.

The way your code is now I imagine that starttime; p1pushtime; and p2pushtime; all have the same value.

What are you trying to measure?

...R

Robin2: Two things - please don't make amendments to earlier Posts that make subsequent comments look stupid please use code tags for code

so it looks like this and is easy to read and easy to copy

You should define variables associated with micros() and millis() as unsigned long.

The way your code is now I imagine that starttime; p1pushtime; and p2pushtime; all have the same value.

What are you trying to measure?

...R

It appears to be a reaction game for two players who compete against each other. The first one to push the button after the LEDs do their thing wins. You are correct, though. Pushtimes will be the same (or differ by 4 us) which, if my assumption about the game is correct, is not a good way to determine the winner.