Go Down

Topic: 3 Digit counter with photoelectric barrier  (Read 164 times) previous topic - next topic

alex2070

Hey I've wanted to make an Airsoft BB Counter, with reset mechanism so I searched the web found something usable tried it aaaaaaand it didn't work. So now I'm here asking you why it's not working and if there would be a better solution or a solution at all?

The Idea of my Projekt:
If I shoot with my BB-Gun the BB will fly through a photoelectric barrier, there for shortly blocking it, then the the number on the 7-Segment-Counter will increase. (so: if barrier is blocked then add 1 to counter)
I also wanted to let it reset when I reload. Aka. when two metal parts touch again it should reset itself.

Components:
1x Arduino UNO (if everything it will be soldered to a NANO)
1x 7 Segment Display (KYX-5361BS)
1x  Photoelectric barrier (LM393 H2010)

Code:
Code: [Select]
//https://www.hackster.io/EasyMades/arduino-seven-segment-counter-734da8

/*

/** This code is edited by www.easymades.com **/


int Ntime = 0;
int Otime = 0;
float X = 0;
int time = 4;

int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int pinDP = 1;

int DIG1 = 12;
int DIG2 = 11;
int DIG3 = 10;
int DIG4 = 9;

int buttonPin = A0;
int buttonPin_state =0;

void choose_digit(char num) {
  switch (num) {
  default: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, HIGH); digitalWrite(pinDP, HIGH); break; case 1: digitalWrite(pinA, HIGH); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, HIGH); digitalWrite(pinE, HIGH); digitalWrite(pinF, HIGH); digitalWrite(pinG, HIGH); digitalWrite(pinDP, HIGH); break; case 2: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, HIGH); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, HIGH); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 3: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, HIGH); digitalWrite(pinF, HIGH); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 4: digitalWrite(pinA, HIGH); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, HIGH); digitalWrite(pinE, HIGH); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 5: digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, HIGH); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 6: digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 7: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, HIGH); digitalWrite(pinE, HIGH); digitalWrite(pinF, HIGH); digitalWrite(pinG, HIGH); digitalWrite(pinDP, HIGH); break; case 8: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 9: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, HIGH); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break;

      ;
  }
}

void pick_digit(int digit) {
  digitalWrite(DIG1, LOW);
  digitalWrite(DIG2, LOW);
  digitalWrite(DIG3, LOW);
  digitalWrite(DIG4, LOW); switch (digit) {
  case 1: digitalWrite(DIG1, HIGH); break; case 2: digitalWrite(DIG2, HIGH); break; case 3: digitalWrite(DIG3, HIGH); break; default: digitalWrite(DIG4, HIGH); break;
  }

}

void decimal_place() {
  digitalWrite(pinDP, LOW);
}

void seven_segment(int number) {
  unsigned char thousands = int (number / 1000);
  unsigned char hundreds = int ((number / 100) % 10);
  unsigned char tens = int ((number / 10) % 10);
  unsigned char ones = int (number % 10); //1234
  if (thousands > 0) {
    choose_digit(thousands);
    pick_digit(1);
  }

  if ((hundreds > 0) | (thousands > 0)) {
    delay(time); choose_digit(hundreds);
    pick_digit(2);
  }

  if ((tens > 0) || (hundreds > 0) | (thousands > 0)) {
    //decimal_place();
    delay(time); choose_digit(tens);
    pick_digit(3);
  }


  // add decimal point
  delay(time); choose_digit(ones);
  pick_digit(4);

  delay(time);

}

void setup() {
  pinMode(DIG1, OUTPUT);
  pinMode(DIG2, OUTPUT);
  pinMode(DIG3, OUTPUT);
  pinMode(DIG4, OUTPUT);
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinDP, OUTPUT);

  pinMode(buttonPin, INPUT);

}

void loop() {
float sensor_value = map(analogRead(A0),0,1023,0,3000); // for Flood level Sensor this Eg:3000 is the maximum level




//Continue counter and Visitors counter

  Ntime = millis();
  if (analogRead(A0) > 0) {

    if ((Ntime - Otime) > 200) {
      X = X + 1;
      {
        if (X == 9999) {
          X = 0;
        }
      }
    }
    Otime = Ntime; // Active this to continue counter

  }



 
  seven_segment(sensor_value);
}

hzrnbgy

Sort of like Master Chief's rifle. This would be a cool project

dougp

So now I'm here asking you why it's not working and if there would be a better solution or a solution at all?
Define 'not working'.
I don't trust atoms.  They make up everything.

No private consultations undertaken!

Grumpy_Mike

#3
Aug 01, 2020, 06:13 am Last Edit: Aug 01, 2020, 06:22 am by Grumpy_Mike
Quote
So now I'm here asking you why it's not working
Because the Photoelectric barrier (LM393 H2010) is not fast enough to register the flight time of the projectile, and the projectile is probably too small to completely break the beam.

Also an analogue read will take about 10mS so the beam break has to be at least that.
Finally the code is a load of crap, sadly just like instructables, hackster has no editorial control on what is published. The seven segment display has no current limiting resistors attached to it, a sure sign of an author who doesn't know what they are doing.

Does it "work" when you use a piece of cardboard to block the beam?  

alex2070

Because the Photoelectric barrier (LM393 H2010) is not fast enough to register the flight time of the projectile, and the projectile is probably too small to completely break the beam.

Also an analogue read will take about 10mS so the beam break has to be at least that.
Finally the code is a load of crap, sadly just like instructables, hackster has no editorial control on what is published. The seven segment display has no current limiting resistors attached to it, a sure sign of an author who doesn't know what they are doing.

Does it "work" when you use a piece of cardboard to block the beam? 
When I put a piece of Cardboard into the beam it changes the Sensor value, but doesn't count up or down.
So it just shows the Sensor value on the 7-Segment Display.


My first self made attempt weren't a big success. Code was something like this:

if counter goes below ___
then display digit one
wait till counter get's again under ___
(and so on till 100)

alex2070

Define 'not working'.
it doesn't count up it just displays the sensor value.

alex2070

Cound't I just handle the barrier as an Button so if the value is HIGH it counts 1 up and the continues till I reset it.

By the way is there a possibility to make the reset mechanism reset when there's a electronic connection again?


so there's current flowing trough the reset mechanism all the time and when the current breaks it should reset to 0 after the current is reapplied.

Grumpy_Mike

Wtf is this
Code: [Select]
[code] void choose_digit(char num) {
  switch (num) {
  default: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, LOW); digitalWrite(pinG, HIGH); digitalWrite(pinDP, HIGH); break; case 1: digitalWrite(pinA, HIGH); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, HIGH); digitalWrite(pinE, HIGH); digitalWrite(pinF, HIGH); digitalWrite(pinG, HIGH); digitalWrite(pinDP, HIGH); break; case 2: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, HIGH); digitalWrite(pinD, LOW); digitalWrite(pinE, LOW); digitalWrite(pinF, HIGH); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 3: digitalWrite(pinA, LOW); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, HIGH); digitalWrite(pinF, HIGH); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 4: digitalWrite(pinA, HIGH); digitalWrite(pinB, LOW); digitalWrite(pinC, LOW); digitalWrite(pinD, HIGH); digitalWrite(pinE, HIGH); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 5: digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(pinE, HIGH); digitalWrite(pinF, LOW); digitalWrite(pinG, LOW); digitalWrite(pinDP, HIGH); break; case 6: digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); digitalWrite(pinC, LOW); digitalWrite(pinD, LOW); digitalWrite(

Why all on one line?
Why the default case first?
This is the display what it produces should change with what digit is being displayed.

Don't play with guns that is about as stupid as this line of code.

Go Up