Go Down

Topic: Object counter « counting box » (Read 2303 times) previous topic - next topic

Nagaro

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 ?

wvmarle

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

Nagaro

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

wvmarle

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

Nagaro

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

Nagaro

Hi,

I've modified the code to be adapted to the SW-420, but now when i press on one of my button, it almost never does anything.
I have the feeling something is "blocking" the arduino, and only at a specific moment the Arduino is "free" and the action from pressing a button is taken.

Below the 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  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
const int PinBuzzer = 7;
const int PinVibr = 13;
const long VibrTresh = 3000; //Treshold for the vibration sensor to count or not

// Variables will change:
bool buttonStateVibr = false;         // current state of the VibrationSensor
bool lastButtonStateVibr = false;     // previous state of the VibrationSensor

bool buttonStatePlus1 = false;         // current state of the button
bool lastButtonStatePlus1 = false;     // previous state of the button

bool buttonStateMinus1 = false;         // current state of the button
bool lastButtonStateMinus1 = false;     // previous state of the button

bool buttonStateReset = false;         // current state of the button
bool lastButtonStateReset = false;     // previous state of the button

bool Vibr = false;
bool ButtonPlus1 = false;
bool ButtonMinus1 = false;
bool ButtonReset = false;

// variables used for the count
int ones; 
int tens; 
int hundreds;

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


void setup()
{

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

// ----- Turn OFF the tone
  digitalWrite(PinBuzzer,HIGH); //LOW is ON, HIGH is OFF


// ----- 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 Vibr changes the state, it does +1
ChangeStateVibr();
if (Vibr == true) {
  Plus1();
  Update7segment(count);
  Serial.print("Vibr=");
  Serial.println(Vibr);
  Vibr=false;
  BeepPlus10();
  delay(150); // that 2 or more sensors do not overlap
}

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

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

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

} //end of proc

void ChangeStateVibr() {

  // read the Vibr input pin:
  unsigned long measurement;
  measurement = pulseIn (PinVibr, HIGH);  //wait for the pin to get HIGH and returns measurement
  delay(10);
  Serial.print("measurement = ");
  Serial.println(measurement);
  if (measurement >= VibrTresh){
    buttonStateVibr = true;
  }
  else{
    if (measurement == 0){ //the vibr needs to go back to 0 if it wants to count again
      buttonStateVibr = false;
    }
  }
 
  // compare the buttonState to its previous state
  if (buttonStateVibr != lastButtonStateVibr) {
    // if the state has changed from less than VibrTres to more than VibrTres, increment the counter otherwise don't
    if (buttonStateVibr == true) {
      Vibr=true;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonStateVibr = buttonStateVibr;

}

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=true;
    }
    // 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=true;
    }
    // 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=true;
    }
    // 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) { 

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

}

void BeepPlus10()
{
    //Tone
    if (ones == 0) {
    digitalWrite(PinBuzzer,LOW);
    tone(PinBuzzer, 1000, 100); //pin,hertz,duration
    delay(100); //delay start at the same time than tone; so thedelay should be at least the same than the duration
    digitalWrite(PinBuzzer,HIGH);
    }
}



Maybe you'll find something weird and not written properly ?

Thank you for your help,

wvmarle

I have the feeling something is "blocking" the arduino
I spotted at least two delay() calls in that code on a quick glance, those are blocking. How badly they block depend on the complete code flow, didn't analyse that.

So definitely room for improvement. Write all your functions non-blocking and return as soon as possible, make sure your loop() calls them all one after the other, so in turn you can rely on your functions being called frequently and replace those delay() calls by millis() based timing.
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.

Go Up