Arduino Forum

Using Arduino => Sensors => Topic started by: Nagaro on Mar 15, 2019, 07:36 pm

Title: Object counter « counting box »
Post by: Nagaro on Mar 15, 2019, 07:36 pm
Hello,

I have as project at work to create a « counting box ».
People in my team are counting daily small pieces (5 cm x 2 cm x 1 cm), in plastic, 1 by 1. For them to make their life easier (and increase our accuracy), i would like to create a small box where they could throw the pieces inside, and it would count them.

On this box, i would like 5 electonic parts :
- a PIR motion sensor (as the parts a really small, i don't want to have a laser only taking in consideration 1 direction but more a surface)
- a display (big and simple), where it would be possible to count from 0 to 100 (no lore info is needed, therefore a lcd screen is not mandatory
- a button « reset » (to put back to counter to 0)
- a button + (to manually increase the counter of 1)
- a button - (to manually decrease the counter of 1)
I bought a Arduino nano, i think totally enough to perform the job.

I found a lot of videos taking about it in internet, but none of them are fulfilling my requirements. Like this one : https://youtu.be/WSDeNvYDZOw

Would anybody have information to help me ?
Thank you
Title: Re: Object counter « counting box »
Post by: Paul_KD7HB on Mar 15, 2019, 07:47 pm
Please Google pir sensor and study how they work.

Paul
Title: Re: Object counter « counting box »
Post by: Nagaro on Mar 16, 2019, 11:00 am
Hi,

Thank you for the answer.
I just figured out that pir sensors can only detect « warm » bodies. So i don't think it will detect my plastic bags. Is that right ?

If that's the case, which kind of sensor do you think i should use to detect those plastic bags ?

Again, i would like to cover a surface more than a single line. So to have a detector capable to check in 2 dimensions, and not only 1.

Or maybe there are some « tricks » that could do the job as well.

Thank you for your support
Title: Re: Object counter « counting box »
Post by: esd125 on Mar 18, 2019, 08:50 am
IR emitter and IR detector or Ultrasonic Sensor will do the job.
Title: Re: Object counter « counting box »
Post by: neiklot on Mar 18, 2019, 09:13 am
Ultrasonic Sensor will do the job.
Might be too slow, and miss some items.

I'd go with....

IR emitter and IR detector
... though.

EDIT
Get one of these  (https://www.pololu.com/product/2465)to test with; they come in different distances. You can test without an Arduino: just apply 5V and see if its onboard led blinks as the item drops past.

When it gets to coding, you'll need to go the state change detect (https://www.arduino.cc/en/Tutorial/StateChangeDetection) route, else it will likely give multiple counts while the item's in front of the beam for a while as it passes..
Title: Re: Object counter « counting box »
Post by: wvmarle on Mar 18, 2019, 05:32 pm
Plastic piece? Plastic bag? What exactly do you have? Bags don't throw well, and you mention you're throwing them.

I was thinking of a bounce plate. Like used in seed counters. But it depends on what your plastic pieces really are, so do post some images!
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 05, 2019, 10:48 pm
Hi,

Sorry for the long time without replying.
Attached 2 pictures to show which kind of items to count we are talking about.
They are plastic bags with jewellery items inside, in silver or gold. Therefore the accuracy needs to be perfect, as it's high value items (and not just screws or anything). The size of the bag can be 4x6 cm (like the small one o nthe picture), or 8x12 (like the big picture).

Would this "Pololu Carrier with Sharp GP2Y0D815Z0F Digital Distance Sensor 15cm" you propose detect those bags? If yes, as i'm leaving in Germany, do you know if i can get it somewhere in europe where i don't need to wait 1 month to receive it ? Or a similar component maybe ?
And if no, any other idea of cheap but reliable component i could use ?

Other question : for the display and the buttons, would you have references you rely on i could buy ? I don't want to buy anything, and then it breaks after 2 hours...

Thank you for the help again,
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 06, 2019, 06:33 pm
That seriously ups the ante.

IR: the bags may be transparent (probably are) to IR. So your bags may not be seen (all transparent passing in front of the sensor), or counted double (object & label as separate items) or triple (the necklace: two signals when the necklace passes, and a third for the label). You will have to try that out.

Speed: check the data sheet of the sensors for how quick they can react, and make sure your bags don't fall past the sensor too fast. And make sure that your bags fall in front of the sensor in the first place, in an orientation that allows them to be seen reliably.

You will anyway have to make sure there's enough time between the items, to be able to count properly. So the actual handling is going to be a major factor. Is it people throwing the bags one by one in the counting machine? Or do you want them to dump a box full of items in some hopper that then does the separation into single items and counting?
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 10, 2019, 10:31 pm
The people will throw each bag one by one (not a full bag), so that's already a good point.

I understand that the IR might count double or triple the item, which need to be send in a way that the sensor recognize them.
But what about if i install 2 or multiple IR (to make a kind of grid). With the code making sure when 1 IR detects the item, it deactivate all other IR for a few milliseconds before they are all activated again ?
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 11, 2019, 06:02 am
I wouldn't disable the sensors, just ignore the input from the others. If you know you're not going to get more than say one bag per second, you can safely ignore all other triggers for half a second before counting again.

The main thing is going to be the physical layout of the whole thing. You have to cover it in a sensor grid that's fine enough to not let the smallest bag pass undetected. That's why I'm still thinking you may be better off with a bounce plate - I think at least you should have a look at the option. A piezo disk or two and rigid plate is what's needed.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 11, 2019, 11:18 am
You're right, ignoring the signal for a couple of milliseconds would make more sense :-)

If i understand it correctly, the bounce plate would count a item from the moment it touches it. Right ?
If i have in my box 50 or 60 pieces already, and i continue to launch some inside, would it still count it accuratly ?

I've ordered yesterday some push buttons, IR sensors and 7-sensors components with 3 digits (as we'll never count more than 999 pieces at once, but more than 99 yes).
Do you know where i can find which resistors i need to use ? And how to place them ?
I don't want to make any mistake and break the components from the 1st use.

For the arduino code, i'll check around if i find easy code. Otherwise, do you know where i could find some help ?

Thank you very much again for your help, and your quick responses always
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 11, 2019, 01:30 pm
A bounce plate would be mounted at the top of the box, so the item is falling first against that plate, then slides into the box.

Which resistors (if any, and capacitors for that matter) to use depends on the exact components you have.

For code, lots of examples out there for all kinds of components, do a Google search of your part number with "Arduino library" and good chance you'll find some. There's definitely code out there for all kinds of 7-segment displays and other kids of displays, for break beam sensors, for push buttons, bounce plates (aka "knock sensor", see also this recent post & thread (http://forum.arduino.cc/index.php?topic=608771.msg4132944#msg4132944)), etc.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 11, 2019, 07:41 pm
Thanks for the description from the bounce plate. It changes the way i was seeing this counting box, but why not ;-)

I checked the link, and the video from the guy : basically i want to do the same principle, not with nerf but with jewelry :-)

I'll have a deeper look, and see how it inspires me.
I'll buy already now a piezo vibration sensor like in the link, that i can try both option (IR or this one).

Now i need to receive it, and make some tests.
I guess i'll still need some help in a few days :-)

Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 11, 2019, 10:52 pm
Hi,

I've tried to make some small schema of the 2 possibilities for this counting box :

Schema 1 : with IR sensors

Schema 2 : with Bounce Plate

To make sure we are on the same level of understanding, and also that we go to the right direction, do you confirm that these schema are correct ?

If yes, we'll have our physical prototypes :-)
Then I'll need to figure out the electrical schema, and then the Arduino code. Still some steps to go.

Thanks
Title: Re: Object counter « counting box »
Post by: dougp on Apr 12, 2019, 12:00 am
Or, combine the two into a chute with an IR sensor.  Mount the IR as low to the sliding surface as practicable.  As the package slides it breaks the IR beam.


(https://forum.arduino.cc/index.php?action=dlattach;topic=603864.0;attach=303546)
Title: Re: Object counter « counting box »
Post by: DaveEvans on Apr 12, 2019, 02:06 am
And add a capacitive sensor to the chute.
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 12, 2019, 05:14 am
The IR sensors pictured are proximity sensors ,not break beam sensors. They work basically with opposite principles: break beam reacts to the absence of a beam because it's blocked by an object that's in the way; the proximity sensor reacts to the presence of IR light as it's reflected by an object in front of the sensor.

With those bags I think the first will work better: if the beam is reflected away, the proximity sensor will still not see it. Experimentation is needed, for sure.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 12, 2019, 08:01 am
Thanks for all these replies.

Now i have 3 questions for you :

- what is the maximum distance that a ir proximity sensor can detect ?

- can i mount both ir sensors together to build a break beam sensor (like the picture from dougp) ?

- what is the added value to have a capacitor ?
Title: Re: Object counter « counting box »
Post by: dougp on Apr 12, 2019, 04:12 pm
- can i mount both ir sensors together to build a break beam sensor (like the picture from dougp) ?
That exact configuration probably will not work.  You need to know whether the sensors are modulated.  If they are, each sensor is looking for its own light beam and others will be out of sync.  You could try a corner cube reflector (https://en.wikipedia.org/wiki/Corner_reflector) and a single sensor to make a retro-reflective sensor (http://senasysphotosensors.com/photoelectric-sensors/retroreflective-sensors) but I believe you'd have more success - and less fuss - using something made for the purpose (https://www.automationdirect.com/adc/shopping/catalog/sensors_-z-_encoders/photoelectric_sensors).  You might have to do some level translation but the sensor will be reliable.

Don't forget, you have to come up with a way to mount whatever you decide on.  Commercial offerings are amenable to a wide variety of mounting options.

Some additional reading material. (https://literature.rockwellautomation.com/idc/groups/literature/documents/br/sensor-br001_-en-p.pdf?event-category=Brochure&event-action=Download&event-label=SENSOR_FamilyBrochure_EN)
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 12, 2019, 04:40 pm
- what is the maximum distance that a ir proximity sensor can detect ?
Depending on the actual sensor up to a few meters. The one pictured, 10-20 cm or so, but it depends highly on the object, the incident angle and the level of ambient IR.

Quote
- can i mount both ir sensors together to build a break beam sensor (like the picture from dougp) ?
A proximity sensor looks for the presence of an IR signal, a break beam sensor for the absence. If you would place two proximity sensors opposite one another they would not give any signal: when there's no object presence they see the transmitted IR of the opposite emitter, when there's an object they see the reflected IR of their own.

If you want to go the break beam route, get a break beam sensor.

Quote
- what is the added value to have a capacitor ?
Depends on where (both from an electrical and physical standpoint) exactly in your circuit you plan to add one, what value, and maybe even what type of capacitor it is.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 12, 2019, 06:03 pm
Ok, thank you.

I think i have enough info now to start building the project, and come back with a feedback and probably more questions :-)
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 12, 2019, 06:16 pm
Good luck!
Hope to hear about the result.
Title: Re: Object counter « counting box »
Post by: DaveEvans on Apr 12, 2019, 07:29 pm
My (mostly tongue-in-cheek) suggestion was to add a capacitive sensor, not a capacitor.  That would add a third sensor (probably) capable of sensing a plastic bag sliding by it.  While you're at it, you could also try a laser proximity sensor. :)

All types of sensors may be confounded by two or more bags hitting nearly at the same time or sliding down the ramp together.  Unless you can somehow guarantee that the bags will be separated by a "large enough" gap, your software will have to be capable of distinguishing between sensor data for one bag and multiple bags.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 12, 2019, 07:38 pm
Yes, you're right, a capacitive sensor might also work.
The good thing is that all your suggestions gave me several potential solutions. I'll start from mine (proximity sensors), and move to other ones if this one is not good enough.

For the time gap, as it will be launched by people using this box, they are anyway limited physically to a certain speed. And i guess this speed is lower higher than what the arduino can process. But this will be part of the test :-)

See you soon, for more concrete news :-)
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 12, 2019, 08:09 pm
A capacitive sensor will NOT detect a plastic bag passing by. Non-conductive materials are generally not detectable by such as sensor.

It may however detect the metal bits in the jewellery, depending on how much metal is in there and how sensitive it is. It also requires a capacitor that is designed to detect proximity, not touch.
Title: Re: Object counter « counting box »
Post by: DaveEvans on Apr 12, 2019, 08:15 pm
A capacitive sensor will NOT detect a plastic bag passing by. Non-conductive materials are generally not detectable by such as sensor.
<>
 
Quote
Capacitive sensors are used for non-contact detection of metallic objects & nonmetallic objects (liquid, plastic, wooden materials and so on).
http://www.fargocontrols.com/sensors/capacitive.html (http://www.fargocontrols.com/sensors/capacitive.html)

Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 12, 2019, 08:36 pm
Of course plastic does affect the electric fields.
A little.
If there's enough plastic.
Covalent bonds in the plastic will help a lot.
Thin polyethylene bags? Won't work.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 13, 2019, 10:12 pm
Hi,

I've started to build the circuit, and i'm facing the 1st issues i didn't think about.
With the 7-segment with 3 digits, i'm using 11 pins on the arduino. Then with the 3 buttons (reset, +1, -1), and the ir sensors, it's minimum 4 pins additionnal (and more if i use more IR sensors...). But the arduino only has 12 pins available.
I was checking on the internet, and i found a way to reduce the amount of pins used for the 7-segments from 11 to 7 pins, using a 7447.
https://www.youtube.com/watch?v=Z4tmelKKGfw (https://www.youtube.com/watch?v=Z4tmelKKGfw)
But it would still be ready sharp for amounts of pins.

- is the 7447 solution good, or is there a most recent way to do it, or easier or cheaper ?

- is there a way to reduce even more the amount of pins needed, that they all fit on the arduino without any restrictions ?

For information, i'll be using an Arduino Nano (i don't have any other), but i guess it will be enough for this.

Thank you,
Title: Re: Object counter « counting box »
Post by: dougp on Apr 13, 2019, 10:39 pm
The MAX7219 display driver is mentioned often here.  It's a serial interface requiring only three Arduino  I/O pins.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 13, 2019, 11:27 pm
Sounds great.
I've checked on google, and found a tutorial on how to configure it (plaisirarduino.fr or https://thecustomizewindows.com/2017/11/arduino-max-7219-cng-basic-circuit-led-7-segment-displays/)
I just bought on ebay the MAX7219CNG.

I'll use a 10kOhm resistor, but not yet the 2 capacitors proposed (it seems like it's only to have a more stable signal, so it will be in phase 2 for all improvements :-) )

Also : i had to buy a new 3-digits 7-segment, as mine was Anode and it seems like the MAX7219CNG only accepts Cathode. Too bad :-(

I'll try to build the circuit on Fritzing, to document all of this, and also start checking the Arduino code.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 15, 2019, 10:44 pm
Hi,

Attached the Fritzing schematic from this project.
I've tried to make it as easy to read as possible.

Can somebody validate it, or tell me what to change/improve ?

Thank you,
Title: Re: Object counter « counting box »
Post by: dougp on Apr 15, 2019, 11:48 pm
It's a very nice looking drawing but, (sorry) for a circuit of more than a few connections it becomes very difficult for someone not familiar with the thing to decode what they're seeing.  This is because with all the components out there, some with tens of pin connections, it's nearly impossible to tell just by looking how something is wired up.  Most experienced people are used to looking at schematics where the parts' pins are named & numbered so you can see at a glance what's connected to what.  Here's an example from Maxim's site:

(http://forum.arduino.cc/index.php?action=dlattach;topic=603864.0;attach=304053)

This emphasizes the Maxim part but it illustrates the principle.  A full schematic would include all  connections, with pin names/numbers, for all  components (http://electronics-diy.com/schematics/107/1.gif).  Unfortunately this would also mean expanding signals grouped into a single line for brevity like the digit and segment lines in the drawing.

Even a hand-drawn schematic including the pin names/numbers is better in this case than a pictorial and would, IMHO, get more responses and allow people to see more exactly how you've made all the connections and so give an informed response.

Yes, it's more work but it would be a useful experience.  Besides, you'll need a real drawing to work from and to have available for future reference.  There are choices of drawing software but I use the free ExpressSCH (https://www.expresspcb.com/expresssch/).
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 16, 2019, 01:05 am
Agreed, totally unreadable. Especially as the MAX7219 and display pinouts are not given. So can't tell whether the connections are correct or not.

No decoupling cap at the MAX7219. That's an issue.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 16, 2019, 09:15 am
Ahah, i guessed you would answer that it's unreadable :-)

I'll try to make a more readable one, following your advices.

Also, i'll add the 10 uf electrolytic and 0.1uf(100n) ceramic capacitors, as it seems like it's mandatory to have them.

Would that be ok ?

Thanks,
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 16, 2019, 01:54 pm
The 100n one yes - all digital ICs need one. You can also see one in the reference circuits of the datasheet, p.12/13 (https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf).

The 10u, where did you find this? I don't see an obvious need for it, but that doesn't mean it's not necessary.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 17, 2019, 11:57 am
Great, thank you. I can even see where to install it :-)

For the second capacitor, i found it there :
https://thecustomizewindows.com/2017/11/arduino-max-7219-cng-basic-circuit-led-7-segment-displays/
Honestly, i trust more what you say than any website. So if you say only the one for 100 nm is enough, then i'll only install this one :-) I need to order it too :-)

I'll update the Fritzing sketch, but that might take some days. In between, i'll start with the Arduino code, as now i know exactly which components i use, so i have all ly inputs/outputs.
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 17, 2019, 12:06 pm
Trust the manufacturer's data sheet; not what random people (including myself) tell you on the Internet.

Just checked the link - and found one major issue in the image where they have the two capacitors, and that's with the physical layout.

The 100nF should be placed physically as close as possible to the pin. That wire is a pretty bad thing here, it adds stray induction which in turn negates a lot of the effect of that cap. The 100 nF should go between the GND bus and the strip the Vcc pin is connected, when placing it on a breadboard (and even closer when you're using a proper PCB - there are chips that have the GND and Vcc power pins next to one another, soldering a 0805 package on top of those two leads is then the ideal placement).

Adding a 10µF electrolytic capacitor won't hurt, anyway. Just try it out.

If you order components you may consider adding a pack of 20 x 10 values of electrolytic, and 20 x 30 values of ceramics. Something like that. It's just very convenient to have those values on hand even if you end up not using half of them, they're really cheap so it's not much of a financial investment.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 17, 2019, 12:10 pm
Awesome, thank you for the advise. I'll take care of it when i'll have all components to make the physical layout.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 18, 2019, 12:22 am
Hi,

I was writting the code, everything was going well until i got this error message : Exit status 1, Error compiling for board Arduino Nano.
Do you know what can be the problem ?

Below the code (not finished yet, as i'm still checking how to display the numbers with the Max7219+7segments) :

Code: [Select]

#include "LedControl.h"
LedControl lc = LedControl(4,5,6,4);

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

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

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

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

int buttonStateReset = 0;         // current state of the button

int FC51 = 0;
int ButtonPlus1 = 0;
int ButtonMinus1 = 0;
int ButtonReset = 0;


// ----- Code for Max7219 + LEDs
int count = 1000;


void setup()
{

// ----- Code for Change State for FC-51
  // initialize the button pin as an input:
  pinMode(PinFC51, INPUT);
  pinMode(PinButtonPlus1, INPUT);
  pinMode(PinButtonMinus1, INPUT);
  pinMode(PinButtonReset, INPUT);
 
  // initialize the LED as an output:
  pinMode(PinLED, OUTPUT);
 
  // initialize serial communication:
  Serial.begin(9600);

// ----- Code for Max7219 + LEDs
    lc.shutdown(0,false);
    lc.setIntensity(0,5);
    lc.clearDisplay(0);
}

void loop() {

// if FC51 changes the state, it does +1
ChangeStateFC51();
if (FC51 == 1) {
  Plus1();
  Update7segment();
  FC51=0;
  }

// if PushButtonPlus1 is active, it does +1
PushButtonPlus1();
if (ButtonPlus1 == 1) {
  Plus1();
  Update7segment();
  ButtonPlus1=0;
  }

// if PushButtonMinus1 is active, it does -1
PushButtonMinus1();
if (ButtonMinus1 == 1) {
  Minus1();
  Update7segment();
  ButtonMinus1=0;
  }

// if PushButtonReset is active, it resets to 0
PushButtonReset();
if (ButtonReset == 1) {
  Reset();
  Update7segment();
  ButtonReset=0;
  }

}

void ChangeStateFC51() {

  // read the FC51 input pin:
  buttonStateFC51 = digitalRead(PinFC51);

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

}

void PushButtonPlus1() {
 
  // read the pushbutton input pin:
  buttonStatePlus1 = digitalRead(PinButtonPlus1);

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

void PushButtonMinus1() {
 
  // read the pushbutton input pin:
  buttonStateMinus1 = digitalRead(PinButtonMinus1);

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

void PushButtonReset() {
 
  // read the pushbutton input pin:
  buttonStateReset = digitalRead(PinButtonReset);

  // compare the buttonState to its previous state
  if (buttonStateReset = 1) {
      ButtonReset=1;
    }
    // Delay a little bit to avoid bouncing
    delay(50);

}

void Plus1()
{
 
}

void Minus1()
{
 
}

void Reset()
{
 
}

void Update7segment() {

// ----- Code for Max7219 + LEDs
    String t = String(count++);
    lc.setDigit(0,0,(int)(t[0]-'0'),true);
    lc.setDigit(0,1,(int)(t[1]-'0'),false);
    lc.setDigit(0,2,(int)(t[2]-'0'),false);
    lc.setDigit(0,3,(int)(t[3]-'0'),false);
    delay(100);
    lc.shutdown(0,true);
    lc.shutdown(0,false);

}



PS : If you find already something wrong in the code (not linked to this issue), you can already let me know :-)
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 18, 2019, 02:00 am
Why don't you post the whole error message? The problem is in the part you didn't post... the part where the compiler tells you what happened.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 18, 2019, 07:25 am
Here is the full error message :


Code: [Select]

C:\Users\xxx\Documents\Arduino\libraries\LedControl\src\LedControl.cpp: In function 'setDigit.constprop':

C:\Users\xxx\Documents\Arduino\libraries\LedControl\src\LedControl.cpp:169:1: internal compiler error: Segmentation fault

 }

 ^

Please submit a full bug report,

with preprocessed source if appropriate.

See <http://gcc.gnu.org/bugs.html> for instructions.

lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status

compilation terminated.

c:/program files/windowsapps/arduinollc.arduinoide_1.8.21.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

exit status 1
Erreur de compilation pour la carte Arduino Nano



Hope it will help more :-)
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 18, 2019, 01:53 pm
"internal compiler error"

That's about the last I expected to see! Exit status 1 is most commonly an undeclared variable, or closing } missing, something like that. Tbh no idea how to handle this one. Nasty. You just ran into a compiler bug.
Title: Re: Object counter « counting box »
Post by: dougp on Apr 18, 2019, 02:32 pm
Please submit a full bug report,

with preprocessed source if appropriate.

See <http://gcc.gnu.org/bugs.html> for instructions.
I would: not alter this code at all.  Save a copy of the code which produced this error and supply the information requested as above.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 18, 2019, 09:11 pm
Hi,

I actually found on a forum on arduino a workaround for this bug (which is a compiler bug, you're right. I changed the Arduino AVR Boards from version 1.6.23 to 1.6.21 in the preferences, and the issue doesn't appear anymore.
I'll anyway submit the issue via the link as dougp proposed, and continue to write the code. And let's see if the support team will solve the issue, that i can come back to the newer version of the Arduino AVR Board.
Title: Re: Object counter « counting box »
Post by: wvmarle on Apr 19, 2019, 05:25 am
The bug itself is in gcc, not Arduino, but it's a very specific interaction between your code, the Arduino IDE and the compiler itself that triggers it.

So when submitting the bug add as much detail of your system as possible, especially the IDE version, so they have a chance of reproducing it, and hopefully fixing the problem.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 19, 2019, 10:48 pm
Hi,

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 :-)

Thank you very much for your support,

Code: [Select]

#include "LedControl.h"
LedControl lc = LedControl(4,5,6,4);

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

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

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

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

int buttonStateReset = 0;         // current state of the button

int FC51 = 0;
int ButtonPlus1 = 0;
int ButtonMinus1 = 0;
int ButtonReset = 0;


// ----- Code for Max7219 + LEDs
int count = 0;


void setup()
{

// ----- Code for Change State for FC-51
  // initialize the button pin as an input:
  pinMode(PinFC51, INPUT);
  pinMode(PinButtonPlus1, INPUT);
  pinMode(PinButtonMinus1, INPUT);
  pinMode(PinButtonReset, INPUT);
 
  // initialize the LED as an output:
  pinMode(PinLED, OUTPUT);
 
  // initialize serial communication:
  Serial.begin(9600);

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

void loop() {

// if FC51 changes the state, it does +1
ChangeStateFC51();
if (FC51 == 1) {
  Plus1();
  Update7segment(count);
  FC51=0;
  }

// if PushButtonPlus1 is active, it does +1
PushButtonPlus1();
if (ButtonPlus1 == 1) {
  Plus1();
  Update7segment(count);
  ButtonPlus1=0;
  }

// if PushButtonMinus1 is active, it does -1
PushButtonMinus1();
if (ButtonMinus1 == 1) {
  Minus1();
  Update7segment(count);
  ButtonMinus1=0;
  }

// if PushButtonReset is active, it resets to 0
PushButtonReset();
if (ButtonReset == 1) {
  Reset();
  Update7segment(count);
  ButtonReset=0;
  }

}

void ChangeStateFC51() {

  // read the FC51 input pin:
  buttonStateFC51 = digitalRead(PinFC51);

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

}

void PushButtonPlus1() {
 
  // read the pushbutton input pin:
  buttonStatePlus1 = digitalRead(PinButtonPlus1);

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

void PushButtonMinus1() {
 
  // read the pushbutton input pin:
  buttonStateMinus1 = digitalRead(PinButtonMinus1);

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

void PushButtonReset() {
 
  // read the pushbutton input pin:
  buttonStateReset = digitalRead(PinButtonReset);

  // compare the buttonState to its previous state
  if (buttonStateReset = 1) {
      ButtonReset=1;
    }
    // Delay a little bit to avoid bouncing
    delay(50);

}

void Plus1()
{
  if (count=999) {
    count = 0;
  } else {
    count = count + 1;
  }
}

void Minus1()
{
  if (count<=0) {
    count = 0;
  } else {
    count = count - 1;
  }
}

void Reset()
{
  count = 0;
}

void Update7segment(int v) { 
   
    int ones; 
    int tens; 
    int hundreds;

    //set the different numbers
    ones=v%10; 
    v=v/10;
     
    tens=v%10; 
    v=v/10;
   
    hundreds=v; 

    //print the numbers digit by digit
    lc.shutdown(0,true); //to deactivate the calculation of the led's : usefull ??
    lc.setDigit(0,2,(byte)hundreds,false);
    lc.setDigit(0,1,(byte)tens,false);
    lc.setDigit(0,0,(byte)ones,false);
    lc.shutdown(0,false); //to activate all led's at once : usefull ??

}
Title: Re: Object counter « counting box »
Post by: dougp on Apr 20, 2019, 12:09 am
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.
Title: Re: Object counter « counting box »
Post by: Nagaro on Apr 24, 2019, 12:08 pm
Hi,

Quick message to tell you that i'll be on holiday until end of this week. So nothing will happen on that project :-)

Thank you,
Title: Re: Object counter « counting box »
Post by: Nagaro on May 01, 2019, 12:06 am
Hi,

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 ?

Below the current code :

Code: [Select]

#include "LedControl.h"
LedControl lc = LedControl(4,5,6,4);

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

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

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

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

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


int FC51 = 0;
int ButtonPlus1 = 0;
int ButtonMinus1 = 0;
int ButtonReset = 0;


// ----- Code for Max7219 + LEDs
int count = 0;


void setup()
{

// ----- Code for Change State for FC-51
  // initialize the button pin as an input:
  pinMode(PinFC51, INPUT);
  pinMode(PinButtonPlus1, INPUT);
  pinMode(PinButtonMinus1, INPUT);
  pinMode(PinButtonReset, INPUT);
 
  // initialize the LED as an output:
  pinMode(PinLED, OUTPUT);
 
  // initialize serial communication:
  Serial.begin(9600);

// ----- 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 - hundreds
    lc.setChar(0,1,0,false); //to show 0 as initial number - tens
    lc.setChar(0,2,0,false); //to show 0 as initial number - units
    lc.setChar(0,3,0,false); //to show 0 as initial number - doesn't exist as 3-digit
}

void loop() {

// if FC51 changes the state, it does +1
ChangeStateFC51();
if (FC51 == 1) {
  Plus1();
  Update7segment(count);
  Serial.print("FC51=");
  Serial.println(FC51);
  FC51=0;
  }

// if PushButtonPlus1 is active, it does +1
PushButtonPlus1();
if (ButtonPlus1 == 1) {
  Plus1();
  Update7segment(count);
  Serial.print("ButtonPlus1=");
  Serial.println(ButtonPlus1);
  ButtonPlus1=0;
  }

// if PushButtonMinus1 is active, it does -1
PushButtonMinus1();
if (ButtonMinus1 == 1) {
  Minus1();
  Update7segment(count);
  Serial.print("ButtonMinus1=");
  Serial.println(ButtonMinus1);
  ButtonMinus1=0;
  }

// if PushButtonReset is active, it resets to 0
PushButtonReset();
if (ButtonReset == 1) {
  Reset();
  Update7segment(count);
  Serial.print("ButtonReset=");
  Serial.println(ButtonReset);
  ButtonReset=0;
  }

}

void ChangeStateFC51() {

  // read the FC51 input pin:
  buttonStateFC51 = digitalRead(PinFC51);

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

}

void PushButtonPlus1() {
 
  // read the pushbutton input pin:
  buttonStatePlus1 = digitalRead(PinButtonPlus1);

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

void PushButtonMinus1() {
 
  // read the pushbutton input pin:
  buttonStateMinus1 = digitalRead(PinButtonMinus1);

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

void PushButtonReset() {
 
  // read the pushbutton input pin:
  buttonStateReset = digitalRead(PinButtonReset);

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

void Plus1()
{
  if (count>=999) {
    count = 0;
  } else {
    count = count + 1;
  }
}

void Minus1()
{
  if (count<=0) {
    count = 0;
  } else {
    count = count - 1;
  }
}

void Reset()
{
  count = 0;
}

void Update7segment(int v) { 
   
    int ones; 
    int tens; 
    int hundreds;

    //set the different numbers
    ones=v%10; 
    v=v/10;
     
    tens=v%10; 
    v=v/10;
   
    hundreds=v; 

    //print the numbers digit by digit
    lc.setDigit(0,0,(byte)hundreds,false);
    lc.setDigit(0,1,(byte)tens,false);
    lc.setDigit(0,2,(byte)ones,false);

  Serial.print("count : ");
  Serial.println(count);
  Serial.print("v : ");
  Serial.println(v);
  Serial.print("ByteHun : ");
  Serial.println((byte)hundreds);
  Serial.print("ByteTen : ");
  Serial.println((byte)tens);
  Serial.print("ByteOne : ");
  Serial.println((byte)ones);

}


I now need to build the real protoype, and see if 1 IR sensor works with the plastic bags, or if need to put more, or change to the piezoelectric one.

Thanks again for your help,
Title: Re: Object counter « counting box »
Post by: dougp on May 01, 2019, 02:13 am
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.
Which is button FC51 in the photo?  What is the voltage on the PinFC51 when the switch is not actuated?
Title: Re: Object counter « counting box »
Post by: Nagaro on May 01, 2019, 07:39 am
FC51 is actually the IR sensor (on the left on the picture). It receives the voltage from the computer, so 5V i believe.
It's not a big issue, but it would be better to have a clean code.

Thx
Title: Re: Object counter « counting box »
Post by: wvmarle on May 01, 2019, 09:45 am
My guess: the Arduino starts up faster than the sensor, and upon starting up the sensor's output changes which is (correctly!) sensed by the Arduino.

Simple and often effective solution: add a delay() in setup(). Something like 100-1000 ms probably.
Title: Re: Object counter « counting box »
Post by: Nagaro on May 01, 2019, 11:02 am
Hi,

Good analysis, that's probably the case. I'll do so and see what happens :-)
Thank you
Title: Re: Object counter « counting box »
Post by: Nagaro on May 04, 2019, 08:31 am
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 :-)

I'll keep you updated, like always.
Title: Re: Object counter « counting box »
Post by: wvmarle on May 04, 2019, 08:39 am
Ah, well done.
That doesn't explain, though, why it worked fine after a reset as you mentioned in #48.
Title: Re: Object counter « counting box »
Post by: Nagaro on May 04, 2019, 09:11 am
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.
Title: Re: Object counter « counting box »
Post by: Nagaro on May 05, 2019, 11:09 pm
Hi,

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 :-)

Little by little we are getting to the final solution.
Title: Re: Object counter « counting box »
Post by: wvmarle on May 06, 2019, 02:45 am
A ramp does't preclude an impact sensor, as it's the hit of arrival you're measuring, not presence on the ramp.
Title: Re: Object counter « counting box »
Post by: Nagaro on May 06, 2019, 11:49 pm
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 :-)

See you,
Title: Re: Object counter « counting box »
Post by: wvmarle on May 07, 2019, 03:13 am
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.
Title: Re: Object counter « counting box »
Post by: Nagaro on May 12, 2019, 02:56 pm
The delay i've implemented is actually what you just explained : it waits only after detecting and counting it.

I've tried it now with the 6 IR sensors, it works pretty well. But i've realized that we count the person is throwing the bags slowlier, as he check everytime if it counts correctly or not. So i would like to include a buzzer, to beep for each count (so we don't need to check the numbers).

I read there are active and passive buzzers, but i would like to have the easiest possible, with the easiest code. Is it then the active or the passive ?
Title: Re: Object counter « counting box »
Post by: wvmarle on May 12, 2019, 03:21 pm
A basic buzzer will just produce a beep when powered on. May be able to connect it directly to an output pin, check current draw.
Title: Re: Object counter « counting box »
Post by: Nagaro on May 29, 2019, 12:41 am
Hi,

I found the solution for the buzzer. And it works just awesome !

In general, the protoype is working quite good. Not 100% accurate as i would expect, but i can still find a way to optimize it.

Now is coming the time to transform this prototype to a « real » system.
But i've never done it.
I can imagine i need to build a PCB where i can put all buttons and electronical parts.

Do you know where i could design it, and who could produce it for ? Would you have any tipp regarding this ?

Thank you for the help :-)
Title: Re: Object counter « counting box »
Post by: wvmarle on May 29, 2019, 03:45 am
I'm using KiCAD to design, then SeeedStudio to make my PCBs, and assemble them myself.

For a 1-2 off project this is generally not necessary, soldered on a piece of perfboard works just as well.
Title: Re: Object counter « counting box »
Post by: Nagaro on Jun 15, 2019, 09:29 pm
Hi wvmarle,

Sorry for the late reply, but it doesn't mean this project didn't move forward.
I showed it at work, they loved the concept ! So now i need to create the  prototype with KiCAD and SeedStudio. As i lever used any of them, i need to get use to it to manage both in a proper way. And when i see on SeedStudio all the parameters possible to put (no of layers, surface finish, minimum solder mask dam...etc...), it might take a bit time :-)

In parallel, i bought a vibration sensor SW-420, and tried the concept of launching on a plate which will count the pieces.
It's not really accurate yet, i have some issues with the code, but i'll see the result i'll reach.
And let you know of course :-)

See you soon i guess