Computational Craft | Salil Parekh

Week 4 | Sensor Knitting

Knitting the conductive sensor using the Knitting Nancy was far more difficult than I anticipated. The instructions seemed simple enough, but in practice it was slightly trickier. I haven't worked much with linear material previously, and have even less experience knitting with it. I first used some regular thread to learn the process of knitting using the tool before using the conductive thread. The trick was to loosen up, and not induce too much tension. Only then could I pull the threads inside the nancy, to enable the creation of the knit and to loop the thread below.

Once I understood the process, it was smooth sailing thereafter. The knitting became meditative, and I had to stop myself from knitting a sensor that was too long! I could see that the resistance was becoming fairly high by testing it on the custom knitting nancy I had created.

process picture of knitting conductive yarn, connected in a circuit with an led on the knitting nancy lit up Knitting a sensor using conductive yarn

I built a simple circuit which used the knitted conductive yarn a sensor. When paired with a resistor, it could be used to detect the resistance. I wanted to use the yarn to control the speed of the fading of the LED.

image of circuit image of the circuit

cleaned up illustration of circuit cleaned up illustration of the circuit

The circuit had to detect the resistance from the yarn, and use those values to control the speed of the fading of the LED. I planned to use a sin wave to control the fade of the LED, and pair it with a value which calculated from the yarn sensor. Luckily, Liza did the difficult part figuring out the correct configuration of the Sin/Cos wave in this snippet.

Next, I had to figure out how to change the speed of the fade using values from the conductive yarn. I wanted to create a linear correlation between the yarn being manipulated and the speed of the fade. This was far more difficult than anticipated.

glitchy graph of the values from the conductive yarn See those dirty, glitchy lines? This equates to a LED which can't figure out what it's being told to do

I used two separate instances of smoothing, but I still couldn't get the values to smoothen out. Begrudgingly I used simple conditional statements to break up the values into a blocks of fade speed. This allows the LED to operate without having to deal with a constant change in instructions.

Smoother, blockier graph Smooth, blocky graphs make for better behaved LEDs

See how it works on this short video:

video

Code for the circuit:

//Fading LED algorithm taken from Liza Stark:  https://github.com/lizastark/Teaching/blob/master/Comp_Craft/Week_4_Code/Fade_PWM_cos_Example_10_16/Fade_PWM_cos_Example_10_16.ino

//Smoothing variables
#define sample 300
int array1[sample];
unsigned long averaging1;
int array2[sample];
unsigned long averaging2;

//Pins
int sensorPin = A0;
int ledPin = 9;

float sensorValue = 0;
float pulseVal = 0;
float ledVal = 0;
int value;
long timeVal = 0;
float periode = 2000; //Change this value to change the speed of the fade

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(sensorPin, INPUT_PULLUP);
}

void loop()
{
  timeVal = millis();

  //Resistor Read+Smoothing
  int sensorValue = analogRead(sensorPin);
  for (int i = 0; i < sample; i++) {
    array1[i] = analogRead(sensorPin);
    averaging1 += array1[i];
  }
  sensorValue = averaging1 / sample; //Final yarn value

  //LED Fade value+smoothing
  periode = map(sensorValue, 350, 550, 1000, 4000);
  for (int i = 0; i < sample; i++) {
    array2[i] = periode;
    averaging2 += array2[i];
  }
  pulseVal = averaging2 / sample;

  //Use steps to "smooth" out values
  if ((pulseVal > 1000) && (pulseVal < 1400)) {
    ledVal = 1000;
  }
  else if ((pulseVal > 1400) && (pulseVal < 1800)) {
    ledVal = 1400;
  }
  else if ((pulseVal > 1800) && (pulseVal < 2200)) {
    ledVal = 1800;
  }
  else if ((pulseVal > 2200) && (pulseVal < 2600)) {
    ledVal = 2200;
  }
  else if ((pulseVal > 2600) && (pulseVal < 3000)) {
    ledVal = 4000;
  }
  else if (pulseVal > 3000) {
    ledVal = 4500;
  }
  
  value = 128 + 127 * cos(2 * PI / ledVal * timeVal);
  analogWrite(ledPin, value);

  //Reset smoothing values
  averaging1 = 0;
  averaging2 = 0;

}