Menu

Arduino Piezo Miss Sensor - Debugging

2020-03-21
2022-12-14
1 2 > >> (Page 1 of 2)
  • Mike Voelker

    Mike Voelker - 2020-03-21

    I have spent many hours looking at the prior posts about Miss Sensors. To this date I don't see any code samples of people sharing their working code. I have a solution but it is a bit buggy. Most of the time a hit is registered. However the annoying thing is that sometimes the miss sensor registers and the therefore the hit does not. It must be a timing issue. I have spent 16+ hours trying to debug and I am clearly missing something.

    I thought I have evertything coded correctly and still don't understand what could be the problem. UGH!!!

    Has anyone gotten a piezo sensor to work reliably in their sketches?

    Anyways... Here is my code.

    /* ADD an LED on front of board inside of loop to indicate every board press.  Good for future debugging.
     * Map buttons or Matrix format.
     * Available ESP32 GPIO pins (16, 4, 15)  1 or 3 serial only????
     * Code based on Open darts and used keymap and while loop from PYDarts code.  HYBRID to work for me with ESP32.
     * HTTP OTA works using browser and use ip address in URL.  Credentials are Admin/Admin and upload an exported BIN file.
     */
    #include <jled.h>  // README found here --> https://github.com/jandelgado/jled#reset
    #include <ESP32OTAHTTP.h>  // Mike V made this file to make code more readable.
    
    //ESP32(Pins 36, 39, 34, 35 Inputs only with no internal pullup resistors.  Need external 10k resistors)
    const int debounceTime = 100;         //20 works better than great !
    const int ledPin = 2;
    const int missSensorPin = 32;
    
    char key = 0;
    int missSensorValue = 0; 
    boolean isMiss = false;
    unsigned long lastHitMillis = 0;             // will store last time event was triggered.  Use "unsigned long" for variables that hold time
    unsigned long lastMissMillis = 0;            // will store last time event was triggered.  Use "unsigned long" for variables that hold time
    int slaveLines = 8; //Change here to the number of lines of your Slave Layer WAS 7
    int masterLines = 12; //Change here to the number of lines of your Master Layer
    int matrixSlave[] = {36, 39, 34, 35, 4, 33, 25, 16}; //Put here the pins you connected the lines of your Slave Layer (INPUTS)
    int matrixMaster[] = {26, 27, 14, 12, 13,23, 22, 21, 19, 18, 5, 17}; //Put here the pins you connected the lines of your Master Layer {OUTPUTS)
    char keymap[8][12] = {     //define the symbols on the buttons of the keypads //§ is invalid replaced with _ in row 2
      {'T','L','1','m','5','G','7','8','9','*','/','"'},
      {'a','C','c','d','e','P','_','q','r','&','[','i'},
      {'B','k','1','m','n','o','p','q','r','&','(','>'},
      {'s','t','u','v','w','x','y','z','A','@','#','^'},
      {'B','C','D','E','F','G','H','I','J','}','(','"'},
      {'K','L','M','N','O','P','Q','R','S',')','=','i'},
      {'T','U','D','E','X','Y','Z','I','J','}','[','%'},
      {'l','W','b','f','h','g','$','j','V','|',']','!'}
    };
    auto hitLED = JLed(ledPin).Blink(1000, 500).Repeat(1); //Blink On, Blink Off
    void setup() {
      Serial.println("Booting");
      Serial.begin(9600);
       while (! Serial);
      pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW);
      for(int i = 0; i < slaveLines; i++){
          pinMode(matrixSlave[i], INPUT_PULLUP);
      }
      //These 4 pins cannot use internal pullup inputs on ESP32.
      pinMode(36, INPUT);pinMode(39, INPUT);pinMode(34, INPUT);pinMode(35, INPUT);
    
      for(int i = 0; i < masterLines; i++){
          pinMode(matrixMaster[i], OUTPUT);
          digitalWrite(matrixMaster[i], HIGH);
      }
    #include <ESP32OTAHTTPsetup.h>  
    }
    
    void loop() {
      server.handleClient();  //HTTP OTA Handler
      hitLED.Update();           //JLed Handler
      missSensorValue = analogRead(missSensorPin);   
      char key = getKey();
      if(key != 0) {       // if the character is not 0 then it's a valid key press
        Serial.print(key);
        hitLED.Reset();
        lastHitMillis = millis();                       // will store last time event was triggered.  Use "unsigned long" for variables that hold time
        isMiss = false;
        delay(10);                                   // delay for miss sensor picking up strong throw after registered hit
      }
      else if(missSensorValue > 10){  // value of 150 is a strong signal
        if (isMiss == false){
          isMiss = true; // was--> isMiss = !isMiss;
          lastMissMillis = millis();
        }
        else if (isMiss == true & millis() - lastHitMillis > 150){
           //Serial.print(missSensorValue); Serial.print(" @ "); Serial.print(millis()); Serial.print(" ==> ");
           //Serial.print(" -- Missed @ "); Serial.println(millis());
           //delay(150); // debounce
         Serial.println("|");  
         isMiss = false;
        }
      }
    }
    
    char getKey(){
      char key = 0;                                       // 0 indicates no key pressed.  Do not use 0 in segment mapping
      for(int i = 0; i < masterLines; i++){
          digitalWrite(matrixMaster[i], LOW);
          for(int j = 0; j < slaveLines; j++){
            if(digitalRead(matrixSlave[j]) == LOW){
              delay(debounceTime);                        // debounce
              while(digitalRead(matrixSlave[j]) == LOW){  // wait for key to be released
              } 
            key = keymap[j][i];                           // Remember which key was pressed.
            }
        }
        digitalWrite(matrixMaster[i], HIGH);              // De-activate the current column.
      }
      return key;  // returns the key pressed or 0 if none
    }
    
     
  • Cory Baumgart

    Cory Baumgart - 2020-03-22

    https://sourceforge.net/p/pydarts/discussion/general/thread/58625573/

    This was a thread on a miss sensor. Previously, the missdart would send a "m" to the program to register a missed dart. I believe now we have replaced it with just the playerbutton ehich is the space bar.

     
  • Mike Voelker

    Mike Voelker - 2020-03-22

    Yeah I understand that a button press works just fine but I am looking for a sensor solution. Thanks for sharing that thread Cory and I have read that several times. Once again nobody posted working code with that Piezo sensor. I really want to get it working in my arduino sketch. Does anyone have a working sketch with piezo sensor working reliably?

     
  • nauleau

    nauleau - 2020-03-22

    Hello Mike, I'm sharing my code with you (you will have to modify it), I'm using three sensors (maybe one is enough) and an Arduino Mega. I did a more advanced version, but I could not test, it seems to work well, but I did not find a version of pydarts which accepts "missdarts" and which does not crash after 'a moment at my place (without Bug).
    Which version of pydarts do you use?
    And can you tell me about your experience with this sketch?

    / @Based on file CustomKeypad.pde
    || @author Alexander Brevig
    || @contact alexanderbrevig@gmail.com
    || @modifié par Maxime NAULEAU nauleaum@neuf.fr
    || @add piezo-sensors
    || | Demonstrates changing the keypad size and key values.
    || #
    /
    /#include <keypad.h>
    </keypad.h>
    /
    const int knockSensor = A0;// the piezo is connected to analog pin 0
    const int knockSensor1 = A1;// the piezo is connected to analog pin 1
    const int knockSensor2 = A2;// the piezo is connected to analog pin 2
    const int threshold = 40; // threshold value to decide when the detected sound is a knock or not

    // these variables will change:
    int sensorReading = 0;// variable to store the value read from the sensor pin
    int sensorReading1 = 0;// variable to store the value read from the sensor pin
    int sensorReading2 = 0;// variable to store the value read from the sensor pin

    bool vibration = 0;
    char missed ='D';

    const byte numRows = 9; //four rows
    const byte numCols = 10; //four columns
    const int debounceTime = 200;//20 works better than great !
    //define the cymbols on the buttons of the keypads
    char keymap[numRows][numCols] = {
    {'s','t','u','v','w','x','y','z','A','@'},
    {'a','b','c','d','e','f','g','h','i','?'},
    {'j','k','l','m','n','o','p','q','r','&'},
    {'s','t','u','v','w','x','y','z','A','@'},
    {'B','C','D','E','F','G','H','I','J','}'},
    {'K','L','M','N','O','P','Q','R','S',')'},
    {'T','U','V','W','X','Y','Z','+','-',']'},
    {'/','!','$','#','(','=','[','_','<','>'},
    {'K','L','M','N','O','P','Q','R','S',')'},
    };
    byte rowPins[numRows]
    = {40, 41, 42, 43, 44, 45, 46, 47, 48}; //connect to the row pinouts of the keypad
    byte colPins[numCols] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39}; //connect to the column pinouts of the keypad

    //initialize an instance of class NewKeypad
    /Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
    /
    void setup()

    { Serial.begin(9600);

    for (int row = 0; row < numRows; row++)
    {
    pinMode(rowPins[row],INPUT); // Set row pins as input
    digitalWrite(rowPins[row],HIGH); // turn on Pull-ups
    }
    for (int column = 0; column < numCols; column++)
    {
    pinMode(colPins[column],OUTPUT); // Set column pins as outputs
    // for writing
    digitalWrite(colPins[column],HIGH); // Make all columns inactive
    }
    }

    void loop()
    { sensorReading = analogRead(knockSensor);
    sensorReading1 = analogRead(knockSensor1);
    sensorReading2 = analogRead(knockSensor2);
    vibration = false;

    if (sensorReading >= threshold || sensorReading1 >= threshold || sensorReading2 >= threshold)
    {
    vibration = true;
    }

    {  char key = getKey();
    

    if (vibration == true) {
    for(int compteur = 0 ; compteur < 10 ; compteur++){
    getKey();}
    if (key==0){
    Serial.print (missed);
    delay (1000);} }
    if( key != 0) { // if the character is not 0 then
    // it's a valid key press
    Serial.print(key);
    delay (1000);
    }
    }

    }

    char getKey()
    {
    char key = 0; // 0 indicates no key pressed

    for(int column = 0; column < numCols; column++)
    {
    digitalWrite(colPins[column],LOW); // Activate the current column.
    for(int row = 0; row < numRows; row++) // Scan all rows for
    // a key press.
    {
    if(digitalRead(rowPins[row]) == LOW) // Is a key pressed?
    {
    delay(debounceTime); // debounce
    while(digitalRead(rowPins[row]) == LOW)
    ; // wait for key to be released
    key = keymap[row][column]; // Remember which key
    // was pressed.
    }
    }
    digitalWrite(colPins[column],HIGH); // De-activate the current column.
    }
    return key; // returns the key pressed or 0 if none

    }

     
    • Anonymous

      Anonymous - 2020-03-29

      We tested your code because we would also like to install a Missdart sensor. We tried to adapt the code for us, but Arduiono always shows errors in the sketch. Could you upload the working file once? Or tell us what we may be doing wrong?

      Arduino: 1.8.12 (Windows Store 1.8.33.0) (Windows 10), Board: "Arduino Uno"

      arduino_mega_config_4x16_sample:1:3: error: stray '@' in program

      / @Based on file CustomKeypad.pde

      ^

      arduino_mega_config_4x16_sample:2:4: error: stray '@' in program

      || @author Alexander Brevig

      ^
      

      arduino_mega_config_4x16_sample:3:4: error: stray '@' in program

      || @contact alexanderbrevig@gmail.com

      ^
      

      arduino_mega_config_4x16_sample:3:28: error: stray '@' in program

      || @contact alexanderbrevig@gmail.com

                              ^
      

      arduino_mega_config_4x16_sample:4:4: error: stray '@' in program

      || @modifié par Maxime NAULEAU nauleaum@neuf.fr

      ^
      

      arduino_mega_config_4x16_sample:4:11: error: stray '\303' in program

      || @modifié par Maxime NAULEAU nauleaum@neuf.fr

             ^
      

      arduino_mega_config_4x16_sample:4:12: error: stray '\251' in program

      || @modifié par Maxime NAULEAU nauleaum@neuf.fr

              ^
      

      arduino_mega_config_4x16_sample:4:41: error: stray '@' in program

      || @modifié par Maxime NAULEAU nauleaum@neuf.fr

                                           ^
      

      arduino_mega_config_4x16_sample:5:4: error: stray '@' in program

      || @add piezo-sensors

      ^
      

      arduino_mega_config_4x16_sample:7:4: error: stray '#' in program

      || #

      ^
      

      arduino_mega_config_4x16_sample:9:2: error: stray '#' in program

      /#include <keypad.h></keypad.h>

      ^

      arduino_mega_config_4x16_sample:1:1: error: expected unqualified-id before '/' token

      / @Based on file CustomKeypad.pde

      ^

      arduino_mega_config_4x16_sample:43:1: error: expected unqualified-id before '/' token

      /Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);

      ^

      arduino_mega_config_4x16_sample:44:1: error: expected unqualified-id before '/' token

      /

      ^

      C:\Users\Manuel\Desktop\Pydart\arduino\arduino_mega_config_4x16_sample\arduino_mega_config_4x16_sample.ino: In function 'void loop()':

      arduino_mega_config_4x16_sample:63:30: error: 'knockSensor' was not declared in this scope

      { sensorReading = analogRead(knockSensor);

                                ^~~~~~~~~~~
      

      C:\Users\Manuel\Desktop\Pydart\arduino\arduino_mega_config_4x16_sample\arduino_mega_config_4x16_sample.ino:63:30: note: suggested alternative: 'knockSensor2'

      { sensorReading = analogRead(knockSensor);

                                ^~~~~~~~~~~
      
                                knockSensor2
      

      exit status 1
      stray '@' in program

      Dieser Bericht wäre detaillierter, wenn die Option
      "Ausführliche Ausgabe während der Kompilierung"
      in Datei -> Voreinstellungen aktiviert wäre.

       
  • Anonymous

    Anonymous - 2020-03-22

    Thanks for sharing your code. Looks likewhat I am doing. I am running version pydarts_v1.2.0-beta1. The problem gets worse the longer into the games we are playing. Everything is running great and then when I trhow a dart and hit an 18 for example the miss registers and not the hit. Of course the miss sensor registers but that needs to be ignored over a valid hit. It happens about 5% of the time in my sketch. That is why I think it is a timing issue. I wish I could enable bluetooth serial to debug my code while connected to my Pi 3 board. Any ideas on how I could debug this via pyDarts or another idea?

     
  • nauleau

    nauleau - 2020-03-22

    I think your code is very different from mine for managing the "missdart", your code looks if the target has been hit then if there is a vibration and decides what it should send according to a time variable (LastHitmillis) .
    Mine checks to see if there is a vibration and then immediately checks to see if the target was not hit.

     
  • nauleau

    nauleau - 2020-03-29

    Hello @Anonymous
    I did a sketch for you, for a 4x16-arduino-Mega configuration , with one sensor conneted to the positive in the annalog pin 0 ans the negative in the ground pin. Add a 1MOhms resistance between the +and- of the sensor.
    The serial key print for a missed dart is "Z" , you can modify it or just add it in the Pydart config file.
    modify the thresold if needed.
    Tell me news

     

    Last edit: nauleau 2020-03-29
    • Andrew Thompson

      Andrew Thompson - 2022-01-09

      @nauleau I appreciate your work with this, I attempted to use a variation of this sketch but ran into a few things I wasn't sure of.

      First, you connect the piezo to A0 but then later use A0 in your rowPins. I assume this is just a typo and the sensor is using it's own input pin.

      Second, what is the purpose of the getKey(); in the compteur for loop? The return value from getKey isn't getting put into a variable in that loop.

       
  • Anonymous

    Anonymous - 2020-03-29

    Hello nauleau,

    I tried your sketch and I have to say it works very well! Thanks for your help!!

     
  • starline

    starline - 2020-04-05

    Hi hi

    that with the miss sensor works very well !! But what is still missing is an ultrasonic sensor !! The mode of operation would then be like this, you throw your 3 darts very slowly, then Pydarts waits until you get the arrows and only switches to the next player when you have left the sensor. If I could I would do it myself, but unfortunately I can't :( !!

     
  • Anonymous

    Anonymous - 2020-06-28

    cane some one help with a sketch?
    i need a 7x12 Sketch, but i want that there are 3 outpouts with 5v,
    this 3 outpouts should blink 3 times when a button is pressed.

     
    • poilou

      poilou - 2020-06-29

      Hi Anonymous,

      Thanks for joining in.

      Hope you'll find help here !

      This is not really relevant to pyDarts directly, but if you manage to
      make it, please post it here anyway !

      Cheers

      Poilou

      On 28/06/2020 16:22, noreply@sourceforge.net wrote:

      cane some one help with a sketch?
      i need a 7x12 Sketch, but i want that there are 3 outpouts with 5v,
      this 3 outpouts should blink 3 times when a button is pressed.


      Arduino Piezo Miss Sensor - Debugging


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/pydarts/discussion/general/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       
  • Anonymous

    Anonymous - 2021-10-23

    Hello!
    Im using your code for arduino and in Cricket it works, but in Ho one it crashes imediatly....
    Im using pydarts_v1.2.0-beta7-win_x32_bin.
    Does it work for you?

    keep the good work!

     
    • nauleau

      nauleau - 2021-11-07

      HI Anonymous!
      Sorry for the late response, to be honest I am using an improved version of this Arduino code because I added LED animations, better portion detection (because I was having physical issues with my target) etc ... .
      Regarding your problem, I think it is from your version of pydarts. Please try to update. Poilou fixed several bugs on the latest versions.
      I am currently using pydarts_v1.2.0-beta18.
      Tell me news!

       
      • iztok slanič

        iztok slanič - 2021-11-27

        Where can i find beta 18 version please help!

         
        • nauleau

          nauleau - 2021-11-28

          if you are on a linux machine, install GIT ,and in a terminal whrite: cd "your pydarts folder" && git pull (example: cd pydarts-source && git pull ).

           
          • Anonymous

            Anonymous - 2022-03-31

            Im on win10 machine and i have the same problem with sensor. Pydarts crashes in HO one in cricket it works fine. Please help! Thanks!

             
  • nauleau

    nauleau - 2022-04-01

    Hello
    For my part, I have no problem with the miss-sensor on "HO one" and I also tested on WIN10.
    Try with the latest version of pydarts in the source tab.
    To do this:
    Install Git on your PC, then open Git Bash from the folder where you want to install the latest version of pydarts, for example "Documents".
    In the GIT Bash window, paste the source page link, ("git clone https://git.code.sf.net/p/pydarts/source pydarts-source") press enter . When the operation is finished, you will have a new folder "pydarts-source" in your documents folder.
    Next, open the pydarts-source folder in the command prompt: C:\Users\NAULEAU\Documents\pydarts-source> . (to be filled in with your name (replace "NAULEAU") and the correct location where you cloned pydarts if it is not in "documents". )
    Then open pydarts with the CMD command:" python pydarts.py" .
    Of course for this to work, you must first install PIP, then install pexpect, netifaces, pygame and pyserial. With the commands ...pip install pyserial, ..pip install pygame.. etc.
    Let me know if it's ok for you.

     
    • iztok slanič

      iztok slanič - 2022-04-01

      Im trying to install netifaces and gives me an c++ error. Now i will try
      download c++ .
      I successfully installed everything else....
      I ll let you know in the minute!
      Thank you for now!!!

      Iztok

      V V pet., 1. apr. 2022 ob 16:49 je oseba nauleau mogette@users.sourceforge.net napisala:

      Hello
      For my part, I have no problem with the miss-sensor on "HO one" and I also
      tested on WIN10.
      Try with the latest version of pydarts in the source tab.
      To do this:
      Install Git on your PC, then open Git Bash from the folder where you want
      to install the latest version of pydarts, for example "Documents".
      In the GIT Bash window, paste the source page link, ("git clone
      https://git.code.sf.net/p/pydarts/source pydarts-source") press enter .
      When the operation is finished, you will have a new folder "pydarts-source"
      in your documents folder.
      Next, open the pydarts-source folder in the command prompt:
      C:\Users\NAULEAU\Documents\pydarts-source> . (to be filled in with your
      name (replace "NAULEAU") and the correct location where you cloned pydarts
      if it is not in "documents". )
      Then open pydarts with the CMD command:" python pydarts.py" .
      Of course for this to work, you must first install PIP, then install
      pexpect, netifaces, pygame and pyserial. With the commands ...pip install
      pyserial, ..pip install pygame.. etc.
      Let me know if it's ok for you.


      Arduino Piezo Miss Sensor - Debugging
      https://sourceforge.net/p/pydarts/discussion/general/thread/3c4300f06b/?limit=25#f92a


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/pydarts/discussion/general/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
  • Anonymous

    Anonymous - 2022-04-01

    You are the man!!!! I finaly did it ! And it works now on win10!
    Thank you werry werry much!
    Best regards!
    Iztok

     
  • nauleau

    nauleau - 2022-04-01

    Very happy for you.
    Have fun!
    nauleau (mogette)

     
  • nauleau

    nauleau - 2022-04-04

    Yes, of course it is possible. But this requires modifying Pydarts to add this function. I don't believe anyone has ever realized that.

     
1 2 > >> (Page 1 of 2)

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.