counter to stop motor

I want to stop a motor after it passes between two optointerrupters five times. This works fine without the counter but stops at the first sensor with this code. Would you please where the error is? Thanks:

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

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

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

void loop() {
  
 digitalWrite(enable,HIGH);
 val1 = digitalRead(opto1);
 val2 = digitalRead(opto2);
 
 if(val1 == HIGH){
   digitalWrite(motor1,HIGH);
  digitalWrite(motor2,LOW);
 counter = counter +1;
  
 }
 
  if(val2 == HIGH){
   digitalWrite(motor1,LOW);
  digitalWrite(motor2,HIGH);
  counter = counter +1;
  
 }
 
if(counter >5){
  
   digitalWrite(motor1,LOW);
   digitalWrite(motor2,LOW);
   
}
Serial.println(counter);
 }

While you increment the count when you see it go high you keep on incrementing while it remains high Which will happen as the loop() occours very quickly.
You have to only look for the next high once it has gone low.

I understand what you are suggesting but don't have a clue how write code that will increment with each high value after a low. I assumed that what I have written adds 1 to count every time it passes the interrupter. Obviously it doesn't.

You need to think about the states that the program/sensors can be in. The opto sensors can be HIGH or LOW. You should only increment counter when the sensor is HIGH and the sensor was LOW last time.

So, you need to have a couple of variables to hold the sensor state(s).

int previousVal1 = LOW;
int previousVal2 = LOW;

Then, in loop, react only when val1 != previousVal1:

if(val1 == HIGH [glow]&& previousVal1 == LOW[/glow])
{
   digitalWrite(motor1,HIGH);
   digitalWrite(motor2,LOW);
   counter = counter +1;
}
[glow]previousVal1 = val1;[/glow]

Thank you! I have spent the entire day on the wrong track.

Even with your assistance, I have somehow managed to corrupt the "count". The serial monitor buzzes the results; consecutive numbers without stopping at 5. I have juggled these variables in all but the correct way.

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);
}
void loop() {
 counter += 1;

What's this doing here?

Should be counter = 1; but now counter is coming up with all "0".

Then counter will be reset to 1 every time the loop executes. You don't want that either.

No, it shouldn't. The counter variable should be initialized once (in setup). It gets incremented inside if blocks, in the loop method.

The loop method is called in an infinite loop. It might be called dozens, hundreds, or thousands of times before your if blocks are ever executed. If you muck with counter every time it is called, you will never get counter to 5.

Thanks. Counter is counting now and if manually passed through the optointerupters, the sketch will stop the motor, but the sensors are being ignored; not forcing the motor in the opposite direction for another count. Why is the sketch ignoring the interrupter signal?

I'm not quite sure what you mean by "interrupter signal". Interrupts are something very specific and you aren't using them.

Why don't you try inserting some additional serial.print statements into your code so you can see what it's actually doing.

Counter is counting now and if manually passed through the optointerupters, the sketch will stop the motor

If what is passed through the optointerupters?

Here's another question. You use digitalWrite to set the enable pin HIGH, before reading from pins opto1 and opto2. Is it not necessary to set the enable pin LOW again, after reading?

I should have written "optoinerruptors". Two are mounted on each end of the printer carriage. These are working when tested without the count. The carriage zooms back and forth. Something in the sketch is disabling the sensors even though they are being counted when the carriage is forced by hand to pass through them. I have mounted a tab that passes through the slot of each interruptor.

Sorry, I said two optointerrupters are mounted on each end, when only one is on each end.

I think it's time to post your code, again.

Here is the latest:

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(motor1,HIGH);
Serial.begin(9600);
    
}

void loop() {
 
  
 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,HIGH);
   digitalWrite(motor2,LOW);
   counter = counter +1;
}
previousVal2 = val2;
  
 //}
 
if(counter >5){
  
   digitalWrite(motor1,LOW);
   digitalWrite(motor2,LOW);
   
}
Serial.println(counter);
 }

What causes the carriage to change direction? There appear to be two motors, but only motor1 ever gets turned on.

Does the enable pin need to be set LOW ever?

The carriage goes in the opposite direction when the tab passes the opto switch. There is only one motor set in the original printer frame. The switch works fine without the counter but I need to stop it after five passes.

The enable pin doesn't seem to affect the process one way or the other.