Counter is counting too quick

I am making a conveyor system for a college project. For this project I am using a DC motor and an ultrasonic sensor to detect the presence of 3 passing items and count it.. The motor should continue to turn until the sensor has detected the required amount of items and once it reaches this count it should stop, delay for 10 seconds and then restart again. I also am using a switch which changes the required item count to 6 items and the motor should function in the same way.
I have my sensor counting items present and it will stop the motor once it counts 3 items however when I reset the counter (pushing reset button on controller) and change the count to 6 items, the ultrasonic sensor will count to 6 items immediately.
I am also having trouble getting the counter to reset in the program and delay the motor.
Any help would be greatly appreciated!

const int switchPin = 7; // the number of the switch pin
const int motorPin =  10; // the number of the motor pin
int switchState = 0;  // variable for reading the switch's state

int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin

int count = 0;
int count2 = 0;



boolean detected = false;
boolean detected2 = false;

float duration_us, distance_cm;

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  4;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // begin serial port
  Serial.begin (9600);

  // initialize the motor pin as an output:
  pinMode(motorPin, OUTPUT);
  // initialize the switch pin as an input:
  pinMode(switchPin, INPUT);

  // configure the trigger pin to output mode
  pinMode(trigPin, OUTPUT);
  // configure the echo pin to input mode
  pinMode(echoPin, INPUT);

   pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {


  // check if the switch is pressed.
  if (switchState == HIGH && count <= 3) {
    // turn motor on:
    digitalWrite(motorPin, HIGH); 
    } else if 
      (switchState == HIGH && buttonState == HIGH && count > 3 && count < 6){
     digitalWrite(motorPin, HIGH); 
  } else {
    // turn motor off:
    digitalWrite(motorPin, LOW);
    delay(1000);
  } 

    


    
  // generate 100-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);

   // read the state of the switch value:
  switchState = digitalRead(switchPin);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;


  if (distance_cm < 10) {
    detected = true;
      count++;

  } else { (distance_cm < 10 && buttonState == HIGH) {
        detected2 = true;
        count2++;
      
     Serial.print("Count = "); Serial.println(count); //print count to serial monitor
      Serial.println("        ");
      Serial.print("Count2 = "); Serial.println(count2);
    }
 

  buttonState = digitalRead(buttonPin);
  // Show the state of pushbutton on serial monitor
 
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
         

  // print the value to Serial Monitor
  
  
  
  delay(500);
}
}

What happens when

void loop() {

Serial.println( count );

is inserted? Can you see the count go from 0, 1, 2, 3, 4, 5, 6, 0, 1 or what?

Why are are buttonPin, ledPin, switchPin and motorPin constants, but not echoPin or trigPin?

Shouldn't there be an 'if' in here somewhere? This is not compiling for me and I'm not convinced the code blocks are defined like you think they are. You need to use auto format in the IDE and make sure the code blocks are what you expect.

  } else {
    (distance_cm < 10 && buttonState == HIGH) {
      detected2 = true;
      count2++;

      Serial.print("Count = "); Serial.println(count); //print count to serial monitor
      Serial.println("        ");
      Serial.print("Count2 = "); Serial.println(count2);
    }
  {
    (distance_cm < 10 && buttonState == HIGH)
    {

Did you mean to put an 'if' before that boolean expression?

There is also a '}' missing at the end.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.