Programming ( IR sensor counts to 12 then triggers a solenoid)

Hey Guys/Gals

I search everywhere on youtube but can't find any program examples of IR sensor counting to 12 then trigger a solenoid.

Can anyone help me?

thank you!!!

Did you look for "IR SENSOR counting to 13 " ?

Why on earth do you need that ?
Which part are you unclear about ?
A- IR sensor detection circuit
B- the counting code
C- All of it. I have no clue and need someone
to do my homework for me.

oh sorry I should have been more clear.
I understand the detection circuit already, also the relay to the solenoid.
I just need some help with the coding aspect.
Specifically the counting to a specific number, than trigger a pin(solenoid).

Here is the code.

int sol = 8; //Tell the Arduino that the positive end of the solenoid is
//plugged into pin 8

int sen = 2; //Tell the Arduino that the positive end of the Sensor is
//plugged into pin 2

void setup() { //this is included in every code we will be
//dealing with. This part runs once.

pinMode(sol, OUTPUT); //These two lines tell the Arduino if the
pinMode(sen, INPUT); // connections are INPUTS or OUTPUTS
}

void loop() { // this will run continuously after the Void setup has
// run once

int val = digitalRead(sen); // Instate a new variable, that is reading
// whether the sensor is sensing
//movement or not. (High or Low)

if(val == HIGH){ //If statutes ask the Arduino if the
//sensor value is reading HIGH (movement)

digitalWrite(sol, HIGH); // if it is high we will send power
// to the solenoid
delay(1000);

}else{ // if it is high we will send power
// to the solenoid
digitalWrite(sol, LOW);
delay(1000);

}

You need to do a bit more definition on your question. approximately how fast are the counts? How long to leave the trigger on? What to do after that? Then put it in a flow chart and you will probably be ready to code it. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

raschemmel:
Why on earth do you need that ?

Perhaps counting a dozen items into a box then changing the chute to the other side for the next box...

OP you need to peruse the state change detect tutorial which will show you how to look for the sensor to change from high to low or low to high. Your current code is just interested in the sensor being high or low, but to count items you need to count changes in the sensor's state.

Then when you get to 12, turn the solenoid on.

But what turns it off?

azianstunn:
//...whether the sensor is sensing movement or not. (High or Low)

Is it a pir sensor? A "normal" ir sensor won't sense movement, just presence. That's why I said you need to check for state changes... a change in an ir sensor's state implies there was movement, but the sensor itself won't measure movement per se.

Hi 12Stepper

You exactly right, counting to a dozen, then changing the chute.

To turn it off, I was just thinking..

delay(1000);
digitalWrite(sol,LOW);

I am reading your recommend "state change detect" right now.
And will learn about counting codes.

thank you

azianstunn:
You exactly right, counting to a dozen, then changing the chute.

Haha, lucky guess!

azianstunn:
delay(1000);
digitalWrite(sol,LOW);

Nah, you don't want delay()s. You need to embrace the idea of using millis() as used in blink without delay.

But won't the next 12 need the chute to go back?- Can't you just off it then and swing the chute from side to side like that?

It is an IR sensor, with a relay module,12 v power supply and a solenoid to a cylinder,

Here's a picture.

IR sensor counts to 12 when object pass by, then triggers the solenoid.

How does the chute swing back? When the solenoid goes low does the chute spring back to the other side or what?

(Which is why I asked what happens after the 24th item....)

Once again thank you 12stepper for your help, it's greatly appreciated.
For now, I am just experimenting with the process.

As of right now, the IR sensor HIGH, activates the solenoid which triggers the cylinder. And when the IR is LOW, is also triggers the solenoid(LOW) which pulls back the cylinder.

I am looking to only trigger the solenoid when the IR sensor is counted at 12.

I believe my program needs the right coding.(counting)

Seems to me that you basically want it to toggle the position every 12 items, so you don't need any timing. Solenoid starts low, chute to one side. Count 12, solenoid high, chute moves to the other side. Count 12 more, solenoid low, chute goes back to first position.

I would use a boolean so-called "flag" and every 12 count just set the flag to the opposite of what it is, and use its value as the high or low of the solenoid pin.

If you look at the state change example from earlier, you'll see it does something every 4 count: easy to make that a 12.

Ok thankxx 12stepper.

I will learn that process now.

I've just written a quick sketch to do it. If you want it I'll post it, or you might want to experiment further your self.

It just changes the solenoid from low to high or high to low every 12 items. (Actually I put the 12 in a variable so I can test it quicker with small values :wink: )

By the way does your sensor show high or low when obstructed? Most I've seen are low when obstructed, so the arrival of an item is a high -> low transition, which is the way I coded it.

Yes sure. That would be greatly appreciated!!
The sensor shows HIGH if obstructed.

Please post it. I can really use any help.

The sensor will be HIGH when obstructed.

azianstunn:
The sensor shows HIGH if obstructed.

Ok let me just rewire and test it that way...

Here you go....

Tested with a button wired to 5V from the pin, and a pull down resistor to hold it low until pressed.

My "solenoid" is just an led: I assume you've taken care of the current etc associated with a solenoid.

Set the itemsPerBox variable near the top.

This is pretty much just the state change variable example: just toggles the solenoid state every itemsPerBox .

Run with serial monitor open and you should get this (itemsPerBox =4):

Setup done... let's count
 
1 2 3 4 , solenoid going 1
1 2 3 4 , solenoid going 0
1 2 3 4 , solenoid going 1
1 2 3 4 , solenoid going 0
1 2 3 4 , solenoid going 1
//  https://forum.arduino.cc/index.php?topic=650891.0
//  toggle solenoid every "x" items

/*
  BASED ON State change detection (edge detection) changed for INPUT PULLUP
  https://www.arduino.cc/en/Tutorial/StateChangeDetection
*/

// this constant won't change:
const int  sen = 2;
const int sol = 8;

// Variables will change:
bool senState;         // current state of the sensor
bool lastSenState;     // previous state of the sensor
byte itemCount;
byte itemsPerBox = 4; // just as a test it's quicker ;)
bool chuteState = false;

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);

  pinMode(sen, INPUT);
  //sensor is high when obstructed
  //my button is wired from pin to 5v
  //and pin has pulldown resistor to force it low if button not pressed
  pinMode(sol, OUTPUT);

  //initialize button states
  senState = digitalRead(sen);
  lastSenState = senState;

  //turn bulitin led off
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  Serial.println(" ");
  Serial.println("Setup done... let's count");
  Serial.println(" ");
}

void loop()
{
  // read the sensor:
  senState = digitalRead(sen);

  // compare the senState to its previous state
  if (senState != lastSenState) // != means not equal, so it changed one way or the other
  {
    if (senState == HIGH) //... and if it's now high, this was a low to high transition
    //                          ie a new object arrived
    {
      itemCount++;
      Serial.print(itemCount);
      Serial.print(" ");
      if (itemCount == itemsPerBox)
      {
        Serial.print(", solenoid going ");
        chuteState = !chuteState; // means "not" so this sets the state to its opposite
        digitalWrite(sol, chuteState);
        Serial.println(chuteState);
        itemCount = 0;
      }
    }

    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastSenState = senState;

} //loop

Oh my God!! You are a genius!!!! It WORKS!!!
Now I just to to change from 4 to 12. And LOW the solenoid 1 second after HIGH.

Thank you soooooo much.

You must have all the equipment that I have right?