Code behaves in unexpected ways

Hello I’m a newbie and this is my first post.
I’m having trouble with a chunk of code.
This code is suppose to detect a single pulse, two pulses 270 ms apart or two pulses 730 ms apart. Then briefly turn on the appropriate LED.
The really frustrating part is: it works about 1/3 of the time. I’d be grateful for any help. Here’s the code:

const int green = 13;
const int yellow1 = 10;
const int yellow2 = 9;
const int remote = 8;

unsigned long remoteTimer;
int remoteCycle;

void setup() {
pinMode(green, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(remote, INPUT);
reset();
}

void reset() {
remoteTimer = 0;
digitalWrite(yellow1, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(green, LOW);
}

void loop() {

unsigned long a = 300;
unsigned long b = 240;
unsigned long c = 760;
unsigned long d = 700;

if(remoteCycle == LOW && digitalRead(remote) == HIGH) {
if(remoteTimer == 0) {
remoteTimer = millis();
}
else {
if((millis() - remoteTimer) < a && (millis() - remoteTimer) > b) {
digitalWrite(yellow1, HIGH);
}
else if((millis() - remoteTimer) < c && (millis() - remoteTimer) > d) {
digitalWrite(yellow2, HIGH);
}
else {
digitalWrite(green, HIGH);
}
}
}

if((millis() - remoteTimer) > 1500 || (millis() - remoteTimer) < 0) {
reset();
}
remoteCycle = digitalRead(remote);
}

////////////////
Thanks in advance!

(millis() - remoteTimer) < 0)

The above can never be true as you are using unsigned longs

Mark

I’m sorry, I got lost in the conditional statements.
You could add some comments.

Please place your code between [code][/code] tags.

const int green = 13;         // LED turns on for a single pulse. 
const int yellow1 = 10;       // LED turns on for a two pulses seperated by 270ms.
const int yellow2 = 9;        // LED turns on for a two pulses seperated by 730ms. 
const int remote = 8;         // Pulse input.

unsigned long remoteTimer;    // Used to remember time of first pulse.
int remoteCycle;              // Used by loop to remember state of digitalRead(remote) for the next loop.

void setup() {
  pinMode(green, OUTPUT);
  pinMode(yellow1, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(remote, INPUT);
  
  // Set remoteTimer to 0 and turn off all LEDs.
  reset();
}

/* 
 * Set remoteTimer to 0 and turn off all LEDs.
 */
void reset() {
  remoteTimer = 0;
  digitalWrite(yellow1, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(green, LOW);
}

/* 
 * The green LED turns on for a single pulse.
 * The first yellow LED turns on for a two pulses seperated by 270ms. 
 * The second yellow LED turns on for a two pulses seperated by 730ms. 
 */
void loop() {
  // If and only if the digitalRead(remote) was LOW for the last loop and HIGH for this loop
  if(remoteCycle == LOW && digitalRead(remote) == HIGH) {
    
     // If this is the first LOW to HIGH change (pulse).
     if(remoteTimer == 0) {
        remoteTimer =  millis();
     }
     
     // If this is the second LOW to HIGH change (pulse).
     else {
        // If the second pulse was inbetween 240 to 300 ms.
        if((millis() - remoteTimer) < 300 && (millis() - remoteTimer) > 240) {
           digitalWrite(yellow1, HIGH);
        }
        
        //If the second pulse was inbetween 700 to 760 ms.
        else if((millis() - remoteTimer) < 760 && (millis() - remoteTimer) > 700) {
           digitalWrite(yellow2, HIGH);
        }
        
        // If there is no second pulse.
        else {
           digitalWrite(green, HIGH);
        }
     }
  }
  
  // If 1500 ms has pasted since the first pulse set remoteTimer back to 0 and turn off all LEDs.
  if((millis() - remoteTimer) > 1500) {
     reset();
  }
  
  // Store the current digitalRead(remote) state for comparison in the next loop.
  remoteCycle = digitalRead(remote);
}

What do your Serial.print()s tell you? Or, rather, what would they if you bothered having some?