MPR121 module - how to know when an electrode is touched? #5057
Replies: 1 comment
-
Posted at 2021-09-22 by @gfwilliams Hi! It looks like you have to repeatedly 'poll' the module to ask it what keys are pressed, and then you get back a number where each bit represents a key.
This isn't tested, but should work... Posted at 2021-09-22 by bertjerred Oh wow - thank you for such a quick reply! I will go test and report back. You are a true blessing! Posted at 2021-09-22 by bertjerred It is starting to make sense. Thanks again! Currently, the Espruino IDE is unhappy with the word, "function," as a 'reserved word,' and is giving red flags there and at the 100ms specification. I shall read up on setInterval and tinker more... Here are some bold (untested) modifications for my project, in case anyone wishes to see:
Posted at 2021-09-22 by Robin Wed 2021.09.22 Not near development PC. Guess here: Missing Would you mind posting the contents of the L-Hand console side showing that error. Posted at 2021-09-22 by Robin Check for mis-matched curly braces by loading code file into notepad++ editor and use it's verification tool. Click on a trailing curly brace to hilight in red. Look upwards in file for matching leading brace, also hilit in red. Also look for missing commas and semi-colons using red arrows in IDE. Posted at 2021-09-22 by @allObjects two (2) issues there:
The second argument of Small change fixes this.
If you do not like the extra ('external', non-anonymous, named)
Posted at 2021-09-22 by bertjerred Thank you very much for this advice, and for your patience with my learning curve. I will get notepad++ and use it as you have suggested to make sure all my things are properly enclosed. Awesome! Posted at 2021-09-22 by bertjerred Again, thank you very much for taking the time. I imagine my console stuff is less interesting (for the version I was writing, which had malformed elements) given @allObjects solution below. I appreciate you! Posted at 2021-09-22 by bertjerred I really am amazed at how much time you just saved me. Thank you so much. Later tonight, I'll be able to hook up my device and send this to it. As you may have guessed, I am at a very beginner level of understanding - yet I understand and appreciate your clear explanation. The fact that you handed it to me on a silver platter is just amazing. Thank you so much. Once I have my beep-boop-beep DIY "synth" making sounds, I will show the results. Lastly, I've never experienced a more positive helpful forum anywhere on the Internet. So, thank you @gfwilliams @allObjects @robin Posted at 2021-09-22 by @SimonGAndrews Hi Bert, Posted at 2021-09-22 by @SimonGAndrews Sorry not trying to confuse, if polling works that's fine I'm sure. Just so you know the option. Posted at 2021-09-23 by Robin Wed 2021.09.22
I like that post #11 suggestion @SimonGAndrews and although I haven't measured, I believe that technique also extends battery life, as Javascript isn't continuously executing as a result of the setInterval duration. Posted at 2021-09-23 by Robin Wed 2021.09.22
Thank you for the kind affirmation @bertjerred I agree wholeheartedly as there are many that frequent the forums providing exceptional responses. Welcome to the Espruino community!! Posted at 2021-09-23 by Robin Wed 2021.09.22 To extend your exposure to Espruino, There are many available examples along with tutorials to learn from: In addtition, I recently put together a quick summary of links that seem to be visited often: Posted at 2021-09-23 by bertjerred No apologies required! :) This project has already brought me closer than ever to "bare metal" than I've really gone before (like, looking - even from the distance of a free library that I didn't have to write - at things like hardware addresses), so it is all new, all fun, all confusing, and all very much appreciated. I also find myself into deeper functions - more complex thinking - than I'm used to, but I welcome the challenge fully. So, your suggestion helps me learn and do new things. How amazing. And you have drawn my attention to options I've certainly overlooked. I was accidentally pretending the IRQ pin wasn't even there. ;) Posted at 2021-09-23 by bertjerred That is a useful insight! The device I'm working on will be plugged in via USB the whole time, but future projects might be portable... thank you! Posted at 2021-09-23 by bertjerred Oh wow - great! Thank you for this as well. I imagine it's hard to predict where people come in from when they're trying out a project, but I guess if you know where they end up, the "commonly referenced links" is pretty handy. I'll be checking that out soon for sure. As a side note, my day job is writing instructor at a small college in Syracuse - and one of the things that interests me is hardware/software documentation online. A fascinatingly complex endeavor - and not sure if it could even exist outside of group efforts and the thoughtfulness of people like yourself who contribute to a community's efficiency. Posted at 2021-09-23 by @SimonGAndrews Pure coincidence but I was working with an MRP121 keyboard on an Espruino MDBT42Q using the interrupt method. The interrupt is actually active low and hence needs a falling edge detection on the setwatch() function. Also the direct connection of the interrupt to an Espruino pin needs the internal pullup resistor enabling. Here is a working snipet to see the approach.
Also Note the MPR121 keyboard im using does not have pullup resistors for the I2C: SCL and SDA, so I have added a 4.7 K resistor on my prototype board between each of: sclPin and 3.3v and sdaPin and 3.3v , I believe some MRP121 breakout boards already have these. Attachments: Posted at 2021-09-23 by @SimonGAndrews also note the interrupt fires on both the contact and release of the keypad button. In this snip Im assuming on release , mpr.touched() = 0, that is : no keys are touched so dont print the value. I imangine on a keyboard this may not be the case, so will need another way to to handle this. Good luck :). Posted at 2021-09-23 by @allObjects I'd go for the interrupt setup like @SimonGAndrews shows. You will get a better overall behavior than you get with polling, especially when you plan to do also additional things in our application. First it may look more complicated, but in the end it will be much simpler. You obviously still have to do a comparison what has changed since you always get all bits back. Posted at 2021-09-23 by @SimonGAndrews Ta, and sorry for typos above. It is an MPR 121 and not MRP! Posted at 2021-09-24 by bertjerred @SimonGAndrews @allObjects thank you so much. This looks fun, and I feel a wave of new questions percolating. But to be sure, I first have some testing and experimenting to do. I'm going to try the IRQ method. @SimonGAndrews, with your 12-key project, do you happen to remember where you embedded your key-specific functionality? For example, did they live inside your touchedIRQ() function? And were they, like, "If keys == 3, do something?" Posted at 2021-09-24 by bertjerred Oh I see - maybe they would be part of the subsequent "If..." section...? Posted at 2021-09-24 by @allObjects you mean something like this (code for 4 bits / 4 touch buttons):
Ignore the IDE warning for line #. There are other options: composing an attribute name from the bit and the bit's value identifying the respective bit handling function in a functions object:
To use just the value of the bit - think one bit shifted to the left:
Would be interesting to have some timing stats on the variations to find the least processing overhead - least amount of time to figure what function to call and the calling of it. Remember, Espruino interprets on the source - no JIT or other byte code compile - and therefore less source code bytes / statements to process in a loop, the faster the loop and the more responsive the behavior of the device. Posted at 2021-09-24 by bertjerred Wow, thank you. So many things here for me to digest. As always, please forgive my lack of experience, but 1) these are functions within a function... each assigned to 1-bit changes? Is that right? and 2) Espruino (and JavaScript) interpret 'on the fly,' so to speak? And 3) if I was using, say, a polling version for MPR121 key strokes successfully (e.g., I can play a C# for as long as I hold down the appropriate key), and I wanted to use an ADC (potentiometer) to alter PWM in realtime while the note is playing continuously, could I? I'm not sure I fully understand the order in which the code gets interpreted by the Espruino, and therefore where to embed my "mod wheel" potentiometer loop. Or maybe I just get confused by loops within loops... In any case, I sure do appreciate all of this, @allObjects! Posted at 2021-09-25 by @allObjects
I'm not an audio engineer, but from the past I know you can use various devices to turn on the path and let the signal pass to a sum... I'm thinking about a 4916 or better a 4066 - see for example https://www.ti.com/lit/ds/symlink/cd74hc4016.pdf?ts=1632556242364 , or a DG408LE https://www.vishay.com/docs/78084/dg408le.pdf. You could also use an op-amp. For producing a sound based on a rotary device: yes you can use a pot and poll with an ADC... (I did this with a circuit to excite an (RF) LC circuit to figure its resonance frequencies... see Espruino controlling LC resonance experiment in HAM Radio class). But you also can just use an encoder that delivers you pulses and a direction - clock and counter clock wise. You set a watch - like you do on an interrupt of a device - and then read the direction in the function and adjust the frequency... In my ham circuit I used watch and polling: watched buttons that increase / lower the frequency, and polling of a sliding pot... The pot I use two ways: controlling the frequency increment/decrement speed when clicking (and holding) the red and green buttons, and when pressed at the same time, the app switches into the analog mode where I poll pot and map it to a frequency. Posted at 2021-09-25 by bertjerred @allObjects you are a Godesend! I'm going to stop asking questions for a while and go try to build build build with all of this magnificent teaching. You (and others on this thread) are really very good educators and I respect the levels of complexity. Would love to see some pictures (when the mood strikes you) of things you've made - not that I've ever ventured into ham, but it is intriguing. The closest I've come is an SDR dongle as a listener/explorer of frequencies... Posted at 2021-09-25 by @allObjects ...did dig up the code:
Posted at 2021-09-25 by Robin Sat 2021.09.25
Code snippets:
Keeping it short so you can:
Posted at 2021-09-26 by @SimonGAndrews @allObjects , all useful stuff above Ta,. ...not to distract from @bertjerred 's build mission but Think it may be useful here to note what happens if a setWatch interrupt occurs while the previous SetWatch function is still running. Was going to test but see you comented on the subject ( 7 yrs ago :). http://forum.espruino.com/comments/11932979/ Posted at 2021-09-26 by Robin Sun 2021.09.26
Posted at 2021-09-26 by @allObjects any event / interrupt goes into the queue w/ related context and queue is worked on by the JS 'loop'. Espruino error flags tell you about overruns - http://www.espruino.com/Reference#l_E_getErrorFlags . Messing with Espruino's architectural setup is not forbidden, but one should not be surprised when things don't go the way as expected... For 'extremely' time sensitive things, write a dedicated client ad let it co-op with Espruiono as the 'overseer'. Posted at 2021-09-27 by bertjerred I've never seen anyone draw their circuit in the comments before and I love it so much. Posted at 2021-09-27 by bertjerred Thank you :) Posted at 2021-09-28 by Robin Mon 2021.09.27 reply to post #34
@bertjerred feel free to peruse the plethora of posts that @allObjects has done over the years. His expertise in complete embedded ASCII circuit documentation is legendary here within the Espruino forum. I too love it when his skill with a new presentation piques a forgotten hidden corner of the cob web infested aging mind. Always something to garner from that insight. I also am old school and keeping documentation within the code file ensures that it won't get lost or more so not forgotten during a future code update or modification. When I stumble across some of those special treats, I'll send or post here. *reply to post #35*
Hope you had an enjoyable time over the week end bit twiddling!! ;-) Posted at 2022-05-13 by mmix2009 @gfwilliams: does the MPR121 module support the filtered and baseline touch values like the Adafruit MPR121 library? The functions are: filteredData(pin) and baselineData(pin). https://github.com/adafruit/node_mpr121/blob/master/index.js Posted at 2022-05-16 by @gfwilliams Hi! It doesn't support it, but it should be easy enough to add. If you stick this code after you instantiate it, it should add the functionality:
If you can let me know if it works for you then I'll add it into the main module - but I can't test it here as I don't have the hardware :) Posted at 2022-05-17 by mmix2009 Thanks, @gfwilliams.
Attachments: Posted at 2022-05-17 by @gfwilliams Ok, thanks! I think what might be happening is the readWord/readByte isn't working properly - they're just effectively reading from address 0. It could be because the sensor resets if it receives a STOP I2C signal between packets... Please could you try:
Posted at 2022-05-17 by mmix2009 Thanks, @gordon. It's working much better now. I'll do more tests to determine the dynamic range. Attachments: Posted at 2022-05-18 by @gfwilliams Great, glad it's working! Let me know when you're happy that it's working as expected and I can push an update to the module. Posted at 2022-05-18 by @allObjects @gfwilliams, skimming over the code in post #38 as the extension (and post #39 in its use), I think that the 4th 'item' in post #39 should read Posted at 2022-05-19 by @gfwilliams Thanks - yes, I noticed @MMIX2009 had already tweaked that ;) I've just changed it, and added my updated I2C comms code Posted at 2022-05-19 by mmix2009 @gfwilliams: I have completed my tests. Posted at 2022-05-20 by @gfwilliams Great! I'll update the module |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-09-22 by bertjerred
Hi - just getting started with an original Espruino board. I'm trying to test my MPR121 capacitive sensor breakout board, which I've connected via I2C.
I am aware of this: https://www.espruino.com/MPR121 but I just don't know where to start understanding how to talk to the 12 electrodes.
Anyone have a beginner-friendly explanation of how to set up some basic code or blocks - something that would display something like, "Key 1 of 12 has been pressed!" "Key 2 of 12 has been released!"
Truly, any nudges in a learning direction would be much appreciated.
Attachments:
Beta Was this translation helpful? Give feedback.
All reactions