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

I just started learning arduino in the last 2 week and this is my first post question on here. Thank you God, you answered my post. Once again thank you for taking the time to help me.

azianstunn:
Thank you soooooo much.

You're welcome. (But I basically just used the example.)

azianstunn:
You must have all the equipment that I have right?

Nope: I used a button as the sensor, and an led as the solenoid.

azianstunn:
And LOW the solenoid 1 second after HIGH.

Why don't you just leave it where it is and let the next 12 count move it back? (That's the way I coded it. If you do move it back with a timer, change chuteState too else it will get out of synch.

Anyway I'm off to bed: 22h45 here and I have a 5k in the morning...

Ohhk. OK you have a goodnite

Or probably better, if you do force it back with a timer, lose the chutestate variable, and on the 12 count do an explicit high to swing it, then as you say, force it to move with a low on the timer.

Then the next 12, and every subsequent 12, will always be a high, and you'll force it low in between each time.

(As it stands, it swings from side to side every 12: I envisaged one box being filled while the previous was being moved out, turn and turn about.)

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

You're going to have trouble if this is literally true. Arduino's processor won't drive heavy loads like a solenoid directly.

dougp:
You're going to have trouble if this is literally true. Arduino's processor won't drive heavy loads like a solenoid directly.

Hehe yeah that's why I gave myself this out:

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

But it does seem from OP's photo in #10 that there's a relay card in the mix: can't see detail though.

Here's adapted code that swings chute on count, returns on time.

I used delay() (naughty me) but if the sketch doesn't do anything other than wiggle the chute, OP can perhaps live with that.

If not we'll change it to a millis() method.

//  https://forum.arduino.cc/index.php?topic=650891.0
//  high solenoid every "x" items
//  V2: time it to low after each high (uses delay)

/*
  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;
int returnDelayTime = 1000;

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 swinging ");
        //chuteState = !chuteState; // means "not" so this sets the state to its opposite
        digitalWrite(sol, HIGH);
        //Serial.println(chuteState);
        itemCount = 0;
        delay(returnDelayTime);
        Serial.println(", returning");
        digitalWrite(sol, LOW);
      }
    }

    // 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

WOW that works exactly how I wanted it. Thank you..

Glad it works.

I used delay() (naughty me) but if the sketch doesn't do anything other than wiggle the chute, OP can perhaps live with that.

Does the program need to do anything else, that could be disrupted by my use of delay() which stops anything else happening during the delay?

Can you just explain for interest, why it must move one way on a count, but return based on time? I'm envisaging items going "left" or "right" into one of two boxes say, where moving the chute either way would be based on a count.

Obviously I have the wrong mental image....

I tried to sent you the video,but it only allows me to upload 2mb. so i didn't work

I am currently just learning and adjust on the fly.

So far, the idea is to count an object which falls into a chute, reach a certain quantity, opens the chute, close it back, and continue again..

hey 12stepper. what's your email address?I would like to send you a e-gift card.

That's very kind if you but unnecessary, thanks.

One day you'll be the one giving the help....

Hi 12stepper.
How did your 5k go?

I need your advice with continuing the from last program.
The picture shows 12 slots, for gummy worms to slide thru on a vibrating fixture.With 12 IR sensor at the end to count which will from to a chute(not yet fabricated). The slot will let only 1 worm slide thru at a time.

My question is after 1 is thru, what mechanism to stop it until all other are thru as well.

I was thinking having 12 small air nozzle in from of the senor, wouldn't that be too complicated?
What do you think??

Thank you

OP's pic from previous post:

Yes..
I was thinking these options..

  1. mini actuators...
  1. mini air pump /with controller..

I must say, I actually have no idea what you're trying to do or what you're asking...

heheh ok

Let me explain..

The 12 slots that you see.
Each slot will have a gummy worm slide down pass the sensor than into a closed chute.
The IR sensor will count them 1 than will trigger either a (small actuator or a air nozzle) that will stop the next 1 coming down.

After all 12 slots have 1 pass thru. Chutes open then close.
The trigger stops, gravity then allows the next 1 to slide down.