count doesn't count

I am trying to control a printer motor with an Arduino and two optointerrupters. “Counter” does not count; the serial monitor buzzes through consecutive numbers but does not stop at 5. Please advise. Thanks.

int opto1 = 4;
int opto2 = 5;
int motor1 = 9;
int motor2 = 6;
int enable = 8;
int val1;
int val2;
int counter;
int previousVal1 = LOW;
int previousVal2 = LOW; 

void setup() {
 //counter = 0; 
 pinMode(opto1,INPUT);
 pinMode(opto2,INPUT);
 pinMode(motor1,OUTPUT);
 pinMode(motor2,OUTPUT);

pinMode(enable,OUTPUT);
digitalWrite(motor2,HIGH);
Serial.begin(9600);
    
}

void loop() {
 counter += 1;
  
 digitalWrite(enable,HIGH);
 val1 = digitalRead(opto1);
 val2 = digitalRead(opto2);
 
if(val1 == HIGH && previousVal1 == LOW)
{
   digitalWrite(motor1,HIGH);
   digitalWrite(motor2,LOW);
   counter = counter +1;
}
previousVal1 = val1;
  
// }
 
if(val2 == HIGH && previousVal2 == LOW)
{
   digitalWrite(motor1,LOW);
   digitalWrite(motor2,HIGH);
   counter = counter +1;
   
}

Serial.println(counter);
}

I see nothing in there that would indicate the counter should stop at 5, or that you’re even checking if it’s 5.

macegr:
I see nothing in there that would indicate the counter should stop at 5, or that you’re even checking if it’s 5.

Nor is it marked as being volatile.

Mike