Object counter « counting box »

The 100n one yes - all digital ICs need one. You can also see one in the reference circuits of the datasheet, p.12/13.

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.

Great, thank you. I can even see where to install it :slight_smile:

For the second capacitor, i found it there :

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 :slight_smile: I need to order it too :slight_smile:

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.

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.

Awesome, thank you for the advise. I’ll take care of it when i’ll have all components to make the physical layout.

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

#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 :slight_smile:

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.

Here is the full error message :

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

"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.

Nagaro:
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.

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.

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.

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

Thank you very much for your support,

#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 ??

}

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 [color=blue]if(lastButtonStateFC51)[/color] instead of [color=blue]if(lastButtonStateFC51 == true)[/color]. They function alike it's just a bit of programming shorthand.

Hi,

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

Thank you,

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 :

#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,

Nagaro:
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?

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

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.

Hi,

Good analysis, that’s probably the case. I’ll do so and see what happens :slight_smile:
Thank you

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

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

I’ll keep you updated, like always.