counting and sending results

I am trying to add a feature to a machine, I am trying to add an air cylinder to it with a piece of metal to stop some bottles as they pass.

I plan to use a photo electric sensor to count the bottles I just need to count 2 bottles at a time then send signal to a relay powering a solenoid valve to activate the air cylinder.

So the aim is to close the air cylinder every time the sensor counts 2 bottles then it should wait a while then retract the cylinder to let two (2) more bottles pass, I wrote a code can anyone tell me what they think of it.

int sensorPin = 3;
int relayPin = 5;
int bottleCount = 0;

void setup() {
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);

}

void loop() {

  for(bottleCount<=2; bottleCount=bottleCount+1;) {
  digitalRead(sensorPin);
  }
  delay(1000);

  digitalWrite(relayPin, HIGH);
  delay(3000);

}

I plan to use a photo electric sensor to count the bottles

The first thing that I notice is that the code does not count the bottles. It will do exactly the same thing whether there are any bottles passing the sensor or not.

You need to read the sensor output, add one to the count when a bottle is detected then act when the count reaches two. That is not what the code does at the moment.

enzyb:
I wrote a code can anyone tell me what they think of it.

When you write an Arduino program the first thing you should do is test it. That way, if it does what you want you don't need to ask questions. And if it does not work as you want then telling us what it actually does is a big help in diagnosing the problem.

The Arduino system is great for learning-by-doing

...R

The machine I want to test it on is at work so I couldnt have tested it before, I dnt have the equipement at home to do so, so I was just putting it out there to see where I went wrong.

enzyb:
The machine I want to test it on is at work so I couldnt have tested it before, I dnt have the equipement at home to do so, so I was just putting it out there to see where I went wrong.

All you need to test it is an Arduino board and a jumper wire.

Use the built in LED as the relay output so that you can see it turning on and off.
Use a jumper wire between the input pin to GND or 5V (whichever is appropriate) to simulate detection of a bottle. For added information output the value of pertinent variables to the Serial monitor.

updated code

int sensorPin = 3;
int relayPin = 5;
int bottleCount = 0;

void setup() {
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);

}

void loop() {

  if(digitalRead(sensorPin) == HIGH); {
    bottleCount++
  }

  if(bottleCount == 2); {
    digitalWrite(realyPin, HIGH);
    
  }
}

Better, but this line is wrong

  if(digitalRead(sensorPin) == HIGH);

The semicolon should not be there, because with it in place, when the sensor pin is HIGH the only code that will be conditionally executed is the semicolon and the code in the following code block will always be executed irrespective of the state of the sensor pin.

The same goes for the second if test in your code.

Soon we will be talking about how to wait a while when 2 bottles have been detected. Can you please explain exactly what should happen after the relay has been activated.

UKHeliBob:
Can you please explain exactly what should happen after the relay has been activated.

When the relay is activated it should send voltage to a solenoid coil to activated the air cylinder to stop bottles from passing.

Well I made a test but the led doesnt turn off I tried bridging from pin 3 to gnd or pin to 5v no effect, the serial monitor just keeps printing 1,3. The next thing is how I would get it to stop counting above 2 because printed bottle count also and it was just going up.

int sensorPin = 3;
int relayPin = 13;
int bottleCount = 0;

void setup() {
pinMode(sensorPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}

void loop() {

  if(digitalRead(sensorPin) == LOW) {
    bottleCount++;
  
  }

  if(bottleCount == 2); {
    digitalWrite(relayPin, HIGH);
     Serial.print(relayPin);
  }
}

if(bottleCount == 2); Take another look at reply 6

This Flow Chart describes what you have wanted in your original post as to the actions to be taken by your program. You may work yourself to convert this Flow Chart into Arduino/C Programming codes.