I made some headway on all aspects of my project. I got a prototype of the pressure matrix sensor working, I got my Arduino nano to transmit data over WiFi, and I got the pressure to send data in serial to a P5 sketch.
The sensor was a lot more complicated than I originally thought. Getting analog readings from the positive connections was straight forward. I set up the Arduino using the internal pull-up resistor, so the positive leads just plug straight into the Arduino analog pins, and then the negative ends (the perpendicular / opposite coordinates of the matrix) go to ground. I was so stumped, though… I understood how you get different Y values from analog readings, but how do you get the X values when they’re going to ground?
I spent so much time racking my brain trying to figure it out. Kobakant’s schematics seemed so thorough and so vague at the same time. For “Arduino Code,” they simply write:
GROUND the pin:
pinMode(pin#, OUTPUT);
digitalWrite(pin#, LOW);
IGNOTE the pin:
pinMode(pin#, INPUT);
What????? First of all, why would you put the digital pin as OUTPUT? also, I assume “ignote” is meant to say “ignore?” And, what?
The only other part of the code says:
READ from this pin:
pinMode(pin#, INPUT);
analogRead(pin#);
I just assumed their code was terribly vague, and it kind of is. But then I paid more attention to the schematics. How the sensor works is actually pretty damn brilliant. See if you can figure out what these code instructions are saying.
So this is how the sensor actually works: It can only read one column at a time, because that’s what is sending readings back to the Arduino. So, the way it generates Y data is by switching the ground back and forth over and over again, and then you record those data into unique sections of your array depending on which ground is active. You set a 1 millisecond delay in between each reading, and 10 milliseconds between each cycle, and you end up with a full reading in what effectively translates to real time data of the whole matrix.
This is the magic that makes it happen:
GROUND the pin:
pinMode(pin#, OUTPUT);
digitalWrite(pin#, LOW);
IGNOTE the pin:
pinMode(pin#, INPUT);
By plugging the ground of each component of the matrix to digital pins, you can convert those pins to ground by telling one pin at a time to write “OUTPUT, LOW.” It’s pretty goddamn brilliant.
I should note that I couldn’t have figured this out without my classmate, Max (whose last name I don’t know at the moment). He and I worked through this together ad nauseam last night. The few examples of pressure matrix code we did find on github seemed absolutely ludicrous until we sort of figured out this fundamental concept behind how the sensor works.
Here’s how the code we wrote for a 2x2 matrix looks:
Basically this is saying: read rows 1 and 2 at column 1, put those values in spots 0 and 1 of the array, then read rows 1 and 2 again, but this time at column 2, and put those values in spots 2 and 3 of the array. It was lot easier to troubleshoot this sensor using a 2x2 instead of my original 4x4 prototype. What was I thinking? But, now I’m ready to move on to a bigger sized matrix.
troubleshooting resistors
One thing I didn’t mention in my sensor troubleshooting (and textile troubleshooting in general) is futzing with resistors. Unlike most breadboard-ready nifty components from Adafruit or Sparkfun, conductive textiles are a moving target in terms of resistance and conductivity.
All of the examples I’ve seen of textile interface schematics use Arduino’s internal pull-up resistor. The nifty thing about that is that you just plug the positive end of your sensor straight into an analog pin, set the pinmode to “INPUT_PULLUP,” and you don’t have to worry about putting resistors in your circuit. When I first experimented with the pressure matrix sensor using Velostat (see last week’s post), I connected it using a pull-down resistor on the breadboard. What I didn’t mention is that which resistor you use with textiles matters, like a lot.
When I first plugged it in I put in a 10K Ohm resistor, because everything takes a 10K resistor, right? Wrong. After a little experimentation I found that a 20K resistor gave me much nicer data readings. Sure, you can always just change the threshold in code, but that doesn’t actually always work. My most successful pressure matrix I have so far is setup using internal pull-up, but my very next step in working with that sensor will be to change it to pull down with 20K resistors on my breadboard.
I suspect textile folks use internal resistors because it’s less crap that you will literally have to wear on your clothing. Unfortunately, the readings I’m getting using Arduino’s internal resistor are kind of sucky, so I will have to use 20K resistors in my circuit. I could still use pull-up resistors physically, but the readings are also backwards and counter-intuitive to me. So, since I’m adding resistors to my circuit anyway, I might as well use pull-down so everything isn’t ass-backwards.
WiFi
On another progress note. I got the Arduino to connect to a WiFi network using the example provided by the Arduino library.
Here is the link on how to do it.
Honestly it was pretty straightforward. I don’t feel terribly obligated to explain it. The hard part is going to be actually making serial communication work with P5 over WiFi. More on that next week….