Below the code i just finished. The "verify" buttons says it's correct, but i don't have yet all components to test it in real.Can you already check it and tell me if you see any weird/useless/wrong thing inside ? Or any comment on how to make it better already ?I put sometimes some comments when i was not sure about something. You'll see :-)

// ----- Code for Change State for FC-51// this constant won't change:const int PinFC51 = 2; // the pin that the pushbutton is attached toconst int PinButtonPlus1 = 3; // the pin that the pushbutton is attached toconst int PinButtonMinus1 = 4; // the pin that the pushbutton is attached toconst int PinButtonReset = 5; // the pin that the pushbutton is attached toconst int PinLED = 13; // the pin that the LED is attached to

// Variables will change:int buttonStateFC51 = 0; // current state of the FC51int lastButtonStateFC51 = 0; // previous state of the FC51

int buttonStatePlus1 = 0; // current state of the buttonint lastButtonStatePlus1 = 0; // previous state of the button

int buttonStateMinus1 = 0; // current state of the buttonint lastButtonStateMinus1 = 0; // previous state of the button

// ----- Code for Max7219 + LEDs lc.shutdown(0,false); //to activate the led's lc.setIntensity(0,5); //to define the brightness of the led's lc.clearDisplay(0); //to cancel all info on the led's lc.setDigit(0,0,0,false); //to show 0 as initial number lc.setChar(0,1," ",false); //to show 0 as initial number lc.setChar(0,2," ",false); //to show 0 as initial number lc.setChar(0,3," ",false); //to show 0 as initial number}

// compare the buttonState to its previous state if (buttonStateFC51 != lastButtonStateFC51) { // if the state has changed from low to high, increment the counter otherwise don't if (buttonStateFC51 == HIGH) { FC51=1; } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonStateFC51 = buttonStateFC51;

// compare the buttonState to its previous state if (buttonStatePlus1 != lastButtonStatePlus1) { // if the state has changed from low to high, increment the counter otherwise don't if (buttonStatePlus1 == HIGH) { ButtonPlus1=1; } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonStatePlus1 = buttonStatePlus1;

// compare the buttonState to its previous state if (buttonStateMinus1 != lastButtonStateMinus1) { // if the state has changed from low to high, increment the counter otherwise don't if (buttonStateMinus1 == HIGH) { ButtonMinus1=1; } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonStateMinus1 = buttonStateMinus1;

All your variables are declared as (2-byte) integers. Pin numbers and such can't go higher than 255, or be negative, and so can be byte size, uint8_t or unsigned char.

Same for the boolean variables, they can be declared as type bool and only occupy one byte. This also aids comprehension - if you know something is a bool then you know it can only be in one of two states, true or false.

This is why you might see something like if(lastButtonStateFC51) instead of if(lastButtonStateFC51 == true). They function alike it's just a bit of programming shorthand.

Everything we call real is made of things that cannot be regarded as real. If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

I'm proud to announced that the project seems to work pretty good ! See picture attached.I corrected some pieces of code (still not changed it with the boolean variables).

I have 1 mini-issue : when i start the arduino, the variable FC51 always goes to 1 by itself, so the counter starts at 1. When i press reset, it goes to 0 and everything becomes normal.Do you know why the FC51 reacts like that ? Is there a workaround that it doesn't, or at least that the counter really starts at 0 and not 1 ? Maybe something to change in the code ?

// ----- Code for Change State for FC-51// this constant won't change:const int PinFC51 = 12; // the pin that the pushbutton is attached toconst int PinButtonPlus1 = 8; // the pin that the pushbutton is attached toconst int PinButtonMinus1 = 9; // the pin that the pushbutton is attached toconst int PinButtonReset = 10; // the pin that the pushbutton is attached toconst int PinLED = 4; // the pin that the LED is attached to

// Variables will change:int buttonStateFC51 = 0; // current state of the FC51int lastButtonStateFC51 = 0; // previous state of the FC51

int buttonStatePlus1 = 0; // current state of the buttonint lastButtonStatePlus1 = 0; // previous state of the button

int buttonStateMinus1 = 0; // current state of the buttonint lastButtonStateMinus1 = 0; // previous state of the button

int buttonStateReset = 0; // current state of the buttonint lastButtonStateReset = 0; // previous state of the button

// compare the buttonState to its previous state if (buttonStateFC51 != lastButtonStateFC51) { // if the state has changed from low to high, increment the counter otherwise don't if (buttonStateFC51 == HIGH) { FC51=1; } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonStateFC51 = buttonStateFC51;

// compare the buttonState to its previous state if (buttonStatePlus1 != lastButtonStatePlus1) { // if the state has changed from low to high, increment the counter otherwise don't if (buttonStatePlus1 == HIGH) { ButtonPlus1=1; } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonStatePlus1 = buttonStatePlus1;

// compare the buttonState to its previous state if (buttonStateMinus1 != lastButtonStateMinus1) { // if the state has changed from low to high, increment the counter otherwise don't if (buttonStateMinus1 == HIGH) { ButtonMinus1=1; } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonStateMinus1 = buttonStateMinus1;

// compare the buttonState to its previous state if (buttonStateReset != lastButtonStateReset) { // if the state has changed from low to high, increment the counter otherwise don't if (buttonStateReset == HIGH) { ButtonReset=1; } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonStateReset = buttonStateReset;

I found the issue : the IR sensor is by default set as High. When an object is in front, it goes to Low. As i'm stating the « LastButtonStatus » as false by default, but the ir sensor itself is by default as high, the change status function runs directly when the arduino starts, and increments the count. To solve this, i just needed to change the « LastButtonStatus » from false to true, and here we go ;-)

I built a prototype of the box, and as expected, 1 sensor is good for normal stuff, but not enough to detect the bags falling. I'll try to put directly 4 sensors as a cross, modify the code and see what's the outcome. Otherwise i move to a piezoelectric sensor. The good thing is that the code won't change that much :-)

Thanks.Actually it does : what i called « reset » was my reset button i've implemented, not the reset button directly on the arduino. Pressing this reset on the arduino was doing the issue too :-)Sorry, maybe i should have called my button different :-)

So now everything is fine, it only needs to work with this plastic bags with jewelry inside.

I made a first protoype of the box (in carton paper), having both ir sensors as i wanted first. It is working, but not accurate enough.

So i built a second protoype (see picture attached), using the idea of the ramp.I figured out that both ir sensors is front of each other is actually working as a beam break (2 beam breaks, as there are 2 sensors). So i only need to hide 1 sensor (the small piece of carton on the left), and it's working really accurate.Now the problem is that the ramp makes the piece falling slowlier, which is not good for the productivity of counting. So i'll not consider the ramp anymore, meaning noto considering using a piezoelectric sensor anymore.

So i'll come back to the 1st idea, but putting the 2 sensors in front of each other.I'll also try to had 2 more sesnors, to have a bigger surface to cover. Let's see how it will work :-)

You're right, i didn't consider this :-)So piezo might be back again. Probably for the next gen :-)

I improved again the prototype, now using the duplo from my sons (see picture).As i need a quite big whole for the bags, 4 IR sensors are not enough. I'll need to put 6. But the good thing is that only 3 sensors need to be connected to the arduino (the ones placed on the left). The 3 on the right are only there to create the break beam, and don't need to be connected (so i don't need the small carton to hide anymore). I also put a delay of 150 ms for each sensor, so that the 3 sensors are not processed at the same time. I'll need to test in real conditions to see if i need to extend this delay or not.

As i'm missing 2 IR sensors, i ordered new ones. So next post in a few days :-)

Get a second set that goes perpendicular, the gaps between the sensors are big enough for a small bag to slip through when aligned just right (this sooner or later will happen).

And why the delay? Only gives more opportunity for bags to slip through. I'd read the sensors as often as possible, but after detecting and counting a bag wait some time (300-500 ms maybe) or for the sensors to not detect the bag any more before counting a new one.

Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.