Category: Assignment-10-SensorDisplay

A catapult that almost works

So I sort of understand what’s wrong.
Basically, how it’s set up here is that I’m using the flex sensor to approximate a catapult; you bend the sensor and then release it, like a slingshot. In the code, it looks for that moment of release -when the values change drastically from much higher to much lower- and ideally that change in values would trigger the projectile to launch. I’ve gotten the catapult arm to move along with the flexing of the sensor, but for whatever reason the projectile fails to launch and I don’t quite understand why. I think it has something to do with the fact that it wants to launch the projectile when the value difference is >100, which if I’m right (which, as I’m a programming novice, I might not be) means that it will only launch the projectile if the difference is CONSTANTLY greater than 100. I think this is the reason mainly because the launch animation takes longer than a second, and a second is barely how long the difference in values remains greater than 100. So the period of time in which difference>100 isn’t sufficient for the entire projectile launch to occur, so therefore I’m witnessing nothing happening.
Or maybe I just frankensteined the codes together wrong.
(the code I’m using for the projectile is a modified version of the processing built-in example “Moving on Curves”)

Anyway, my concept was to use the flex sensor as what it pretty literally approximates, which is a catapult. It’s not a super sophisticated concept, but it seemed doable. As far as I’m concerned it still is, but it’ll require me to have a real knock-down drag-out battle with the code, which probably will result in me getting curbstomped by a computer. I’m happy I got the arm to move, at least.

catapult screenshot

flex sensor

flex

// This Processing program reads serial data for two sensors,
// presumably digitized by and transmitted from an Arduino. 
// It displays two rectangles whose widths are proportional
// to the values 0-1023 received from the Arduino.
 
// Import the Serial library and create a Serial port handler
import processing.serial.*;
Serial myPort;   
 
// Hey you! Use these variables to do something interesting. 
// If you captured them with analog sensors on the arduino, 
// They're probably in the range from 0 ... 1023:
int valueA;  // Sensor Value A
int valueB;  // Sensor Value B

int valueBPrevious;


 
//------------------------------------
void setup() {
  size(700,500);
  // List my available serial ports
  int nPorts = Serial.list().length; 
  for (int i=0; i < nPorts; i++) {     println("Port " + i + ": " + Serial.list()[i]);   }      // Choose which serial port to fetch data from.    // IMPORTANT: This depends on your computer!!!   // Read the list of ports printed by the code above,   // and try choosing the one like /dev/cu.usbmodem1411   // On my laptop, I'm using port #4, but yours may differ.   String portName = Serial.list()[0];    myPort = new Serial(this, portName, 9600);   serialChars = new ArrayList(); }   //------------------------------------ void draw() {     // Process the serial data. This acquires freshest values.    processSerial();      background(205,235,255);   noStroke();   fill(100,210,170);   rect(0,430, 700,500);    float x = map(valueB, 700,900, 130,50);  float y = map(valueB, 700,900, 320,400); //projectile variables float xcoord = x;        // Current x-coordinate float ycoord = y;        // Current y-coordinate float beginX = xcoord;  // Initial x-coordinate float beginY = ycoord;  // Initial y-coordinate float endX = 600;   // Final x-coordinate float endY = 430;   // Final y-coordinate float distX =endX-beginX;          // X-axis distance to move float distY=endY-beginY;          // Y-axis distance to move float exponent = 4;   // Determines the curve float step = 0.01;    // Size of each step along the path float pct = 0.0;      // Percentage traveled (0.0 to 1.0) //catapult base   fill (255);   rect(80,400, 80,30);   rect(110,390, 45,10);   rect(110,350, 10,40);   beginShape();     vertex(120,350);     vertex(120,360);     vertex(145,390);     vertex(155,390);   endShape(); //catapult arm   strokeWeight(5);   stroke(255);   line(110,400, x,y);   //curve motion adopted and modified from processing's example "moving on curves" //projectile somehow isn't working still, either go to office hours or pester mark   int difference = valueBPrevious - valueB;   if (difference > 100){
    println ("Thing happened!");
        xcoord = beginX + (pct * distX);
        ycoord = beginY + (pow(pct, exponent) * distY);
      }
      fill(255);
      ellipse(x, y, 10, 10);  
      pct = 0.0;
  valueBPrevious = valueB; // last thing we do.
}
 
 
//---------------------------------------------------------------
// The processSerial() function acquires serial data byte-by-byte, 
// as it is received, and when it is properly captured, modifies
// the appropriate global variable. 
// You won't have to change anything unless you want to add additional sensors. 
 
/*
The (expected) received serial data should look something like this:
 
 A903
 B412
 A900
 B409
 A898
 B406
 A895
 B404
 A893
 B404
 ...etcetera.
 */
 
ArrayList serialChars;      // Temporary storage for received serial data
int whichValueToAccum = 0;  // Which piece of data am I currently collecting? 
boolean bJustBuilt = false; // Did I just finish collecting a datum?
 
void processSerial() {
 
  while (myPort.available () > 0) {
    char aChar = (char) myPort.read();
 
    // You'll need to add a block like one of these 
    // if you want to add a 3rd sensor:
    if (aChar == 'A') {
      bJustBuilt = false;
      whichValueToAccum = 0;
    } else if (aChar == 'B') {
      bJustBuilt = false;
      whichValueToAccum = 1;
    } else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
      // If we just received a return or newline character, build the number: 
      int accum = 0; 
      int nChars = serialChars.size(); 
      for (int i=0; i < nChars; i++) {          int n = (nChars - i) - 1;          int aDigit = ((Integer)(serialChars.get(i))).intValue();          accum += aDigit * (int)(pow(10, n));       }         // Set the global variable to the number we captured.       // You'll need to add another block like one of these        // if you want to add a 3rd sensor:       if (whichValueToAccum == 0) {         valueA = accum;         // println ("A = " + valueA);       } else if (whichValueToAccum == 1) {         valueB = accum;         // println ("B = " + valueB);       }         // Now clear the accumulator       serialChars.clear();       bJustBuilt = true;       } else if ((aChar >= 48) && (aChar <= 57)) {
      // If the char is between '0' and '9', save it.
      int aDigit = (int)(aChar - '0'); 
      serialChars.add(aDigit);
    }
  }
}

Sensor Display : Prison Tap Code


So back in the Vietnam War, US prisoners of war would be isolated from each other in solitary cells, where verbal communication was impossible. To make up for this, prisoners used Tap Code, an ancient Greek invention. Tap code is based off of a 5×5 table of the Latin alphabet, as shown below:

One prisoner would knock a certain number of times to determine the row, pause, and then knock a certain number of times to determine the column of the letter they were describing. The listening prisoner would mark down the two numbers, and determine which letter was being knocked out by checking the table (usually mentally). So, for example, 1 tap then 1 tap would be A, 4 taps then 2 taps would be R, and so on.
I decided to create a listening prisoner, for whom I could tap messages, and they would print them out in a console. To better visualize this process, I created an animated tap table which would show the letter being tapped. Certainly, this makes the process much easier than it was the prisoners of war in Vietnam, but I lack the time or necessity to become fluent. Here it is in action:

To complete this, I used a piezo, a vibration sensor, to detect my taps on the table. Here is my circuit (real bare bones stuff):

20141028_203031

In Fritzing:

assignment 10 knock sensing fritzing

My Arduino code:

    
const int knockSensor = A0; 
const int threshold = 70; 
const int knockTimeMaxThreshhold = 1000;
const int knockTimeMinThreshhold = 100;

int sensorReading = 0;
int knockNum = 0;
long knockTime = 0;
int col = 0;
int row = 0;

void setup() {
 Serial.begin(9600); 
}

void loop() {
  sensorReading = analogRead(knockSensor);   
  if (millis() - knockTime > knockTimeMaxThreshhold)
  {
    if (knockNum == 1)
    {
      knockNum = 2;
      knockTime = millis();
    }
    else if (knockNum == 2)
    {
      Serial.print("A");
      Serial.println(100);
      knockNum = 0;
      col = 0;
      row = 0;
    }
  }
  if (sensorReading >= threshold && millis()-knockTime > knockTimeMinThreshhold)
  {
    knockTime = millis();
    if (knockNum == 0)
    {
      knockNum = 1;
    }
    if (knockNum == 2)
    {
      row++;
      Serial.print("B");
      Serial.println(row);
    }
    else
    {
      col++;
      Serial.print("A");
      Serial.println(col);
    }
  }    
  delay(1); 
}

My Processing code:

// This Processing program reads serial data for two sensors,
// presumably digitized by and transmitted from an Arduino. 
// It displays two rectangles whose widths are proportional
// to the values 0-1023 received from the Arduino.
 
// Import the Serial library and create a Serial port handler
import processing.serial.*;
Serial myPort;   
 
// Hey you! Use these variables to do something interesting. 
// If you captured them with analog sensors on the arduino, 
// They're probably in the range from 0 ... 1023:
int valueA;  // Sensor Value A
int valueB;  // Sensor Value B
 
//------------------------------------
void setup() {
  size(200, 200);
  String portName = Serial.list()[0]; 
  myPort = new Serial(this, portName, 9600);
  serialChars = new ArrayList();
}
void draw() { 
  int prevValA = valueA;
  processSerial();
  if (valueA == 100){
    print(tapTable(valueB-1, prevValA-1));
    valueA = 0;
    valueB = 0;
  }
  for(int i = 0; i < 5; i++)
  {
    float iw = width*i/5;
    float ih = height*i/5;
    line(iw,0,iw,height);
    line(0,ih,width,ih);
    for(int j = 0; j < 5; j++)
    {
      color c = color(0,0,0);
      if (valueA - 1 == j && valueB - 1 == i)
      {
        c = color(255,0,0);
      }
      float wOffset = width/10-5;
      float hOffset = height/10+3;
      float jh = height*j/5;
      fill(c);
      text(tapTable(i,j),iw+wOffset,jh+hOffset);
    }
  }
}

char tapTable(int col, int row)
{
  char[][] table = {{'A','B','C','D','E'},
                    {'F','G','H','I','J'},
                    {'L','M','N','O','P'},
                    {'Q','R','S','T','U'},
                    {'V','W','X','Y','Z'}};
  return table[constrain(row,0,4)][constrain(col,0,4)];
}
 
ArrayList serialChars;      // Temporary storage for received serial data
int whichValueToAccum = 0;  // Which piece of data am I currently collecting? 
boolean bJustBuilt = false; // Did I just finish collecting a datum?
 
void processSerial() {
 
  while (myPort.available () > 0) {
    char aChar = (char) myPort.read();
 
    // You'll need to add a block like one of these 
    // if you want to add a 3rd sensor:
    if (aChar == 'A') {
      bJustBuilt = false;
      whichValueToAccum = 0;
    } else if (aChar == 'B') {
      bJustBuilt = false;
      whichValueToAccum = 1;
    } else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
      // If we just received a return or newline character, build the number: 
      int accum = 0; 
      int nChars = serialChars.size(); 
      for (int i=0; i < nChars; i++) { 
        int n = (nChars - i) - 1; 
        int aDigit = ((Integer)(serialChars.get(i))).intValue(); 
        accum += aDigit * (int)(pow(10, n));
      }
 
      // Set the global variable to the number we captured.
      // You'll need to add another block like one of these 
      // if you want to add a 3rd sensor:
      if (whichValueToAccum == 0) {
        valueA = accum;
        // println ("A = " + valueA);
      } else if (whichValueToAccum == 1) {
        valueB = accum;
        // println ("B = " + valueB);
      }
 
      // Now clear the accumulator
      serialChars.clear();
      bJustBuilt = true;
 
    } else if ((aChar >= 48) && (aChar <= 57)) {
      // If the char is between '0' and '9', save it.
      int aDigit = (int)(aChar - '0'); 
      serialChars.add(aDigit);
    }
  }
}

Running From The Ghost

I wanted to make a spooky game for Halloween. So I did.

This is a game where one’s goal is to run away from the ghost, the scariest series of ellipses ever. The game itself runs in Processing and interfaces with Arduino. The controller consists of a circuit which has two force sensitive resistors that are used to control the player character’s legs. (The circuit’s box, which is not unlike the casing of a mummy, was lovingly crafted from tape, cardboard, and a box of Ritz Crackers.)
IMG_9596run fingersInitially, I was planning to have more types of enemies to run away from. It would be fun if the player character and enemies cycled, so that whoever you were were caught by last is who you play as next. It would also be interesting to implement different movement types, such as jumping, standing, and different walking/running speeds, so that each enemy has a different type of action that is best suited for a successful escape.
arduinoRunnerSketchAs for minor changes to the existing game, it would be more visually pleasing if the player character had arms, knees, and a more worried expression (the latter of which could be accomplished by the inclusion of eyebrows). fritzingDiagram_Runner_bbI’d say I spent my time 1/6th on the circuit, 1/6th on the circuit’s box, 2/6ths on the game’s mechanic, and 2/6ths on the visual look. (That’s 2/6ths physically making things and 4/6ths programming things. This does not take into account how long documentation took.)

Oh, and be sure to scroll down to the end of the code. Happy Halloween!

//Miranda Jacoby
//EMS Interactivity Section 4
//majacoby@andrew.cmu.edu
//Copyright Miranda Jacoby 2014

//Code for interfacing with Arduino provided by Golan Levin

//Big thanks to Matt for helping me
//figure out how to implement the counter

// This Processing program reads serial data for two sensors,
// presumably digitized by and transmitted from an Arduino. 
// It displays two rectangles whose widths are proportional
// to the values 0-1023 received from the Arduino.

// Import the Serial library and create a Serial port handler
import processing.serial.*;
Serial myPort;   

PImage gameover;
// Hey you! Use these variables to do something interesting. 
// If you captured them with analog sensors on the arduino, 
// They're probably in the range from 0 ... 1023:
int valueA;  // Sensor Value A
int valueB;  // Sensor Value B
int legPosy1;//70
int legPosy2;//70
int legPosx1;
int legPosx2;

//Gradient Variables
int Y_AXIS = 1;
color b1, b2;
color ghostCol = color(245, 249, 247);
int i = 0;
int j = 0;
int h = 150;
int toothWidth = 20;

boolean leftDown = true;
int runCounter = 50;

//------------------------------------
void setup() {
  size(800, 600);

  gameover = loadImage("scare.tif");

  b1 = color(27, 73, 85);//color(0, 102, 153);
  b2 = color(104, 20, 0);

  // List my available serial ports
  int nPorts = Serial.list().length; 
  for (int i=0; i < nPorts; i++) {
    println("Port " + i + ": " + Serial.list()[i]);
  } 

  // Choose which serial port to fetch data from. 
  // IMPORTANT: This depends on your computer!!!
  // Read the list of ports printed by the code above,
  // and try choosing the one like /dev/cu.usbmodem1411
  // On my laptop, I'm using port #4, but yours may differ.
  String portName = Serial.list()[5]; 
  myPort = new Serial(this, portName, 9600);
  serialChars = new ArrayList();
}

//------------------------------------
void draw() {
  // Process the serial data. This acquires freshest values. 
  processSerial();

  //A is left leg, B is right leg
  valueA = (int)map(valueA, 0, 1023, 0, 100);
  valueB = (int)map(valueB, 0, 1023, 0, 100);

  legSwitch();
  println(runCounter);

  setGradient(0, 0, width, height, b1, b2, Y_AXIS);
  drawScenery();
  drawEnemy();
  drawHill();
  drawPlayer(valueA, valueB);
  if (runCounter < 0){    image(gameover, 0, 0);    }   // draw a pink rectangle displaying valueA:   //fill (255, 200, 200);      //rect (0, 0, valueA, 100);   // draw a blue rectangle displaying valueB:   //fill (200, 200, 255);    //rect (0, 100, valueB, 100);    //fill (0);    // draw the letters A and B:   //text ("A", 20, 60);    //text ("B", 20, 160);   //println("A "+ valueA);   //println("B "+ valueB); } //--------------------------------------------------------------- // The processSerial() function acquires serial data byte-by-byte,  // as it is received, and when it is properly captured, modifies // the appropriate global variable.  // You won't have to change anything unless you want to add additional sensors.  /* The (expected) received serial data should look something like this:    A903  B412  A900  B409  A898  B406  A895  B404  A893  B404  ...etcetera.  */ ArrayList serialChars;      // Temporary storage for received serial data int whichValueToAccum = 0;  // Which piece of data am I currently collecting?  boolean bJustBuilt = false; // Did I just finish collecting a datum? void processSerial() {   while (myPort.available () > 0) {
    char aChar = (char) myPort.read();

    // You'll need to add a block like one of these 
    // if you want to add a 3rd sensor:
    if (aChar == 'A') {
      bJustBuilt = false;
      whichValueToAccum = 0;
    } else if (aChar == 'B') {
      bJustBuilt = false;
      whichValueToAccum = 1;
    } else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
      // If we just received a return or newline character, build the number: 
      int accum = 0; 
      int nChars = serialChars.size(); 
      for (int i=0; i < nChars; i++) {          int n = (nChars - i) - 1;          int aDigit = ((Integer)(serialChars.get(i))).intValue();          accum += aDigit * (int)(pow(10, n));       }       // Set the global variable to the number we captured.       // You'll need to add another block like one of these        // if you want to add a 3rd sensor:       if (whichValueToAccum == 0) {         valueA = accum;         // println ("A = " + valueA);       } else if (whichValueToAccum == 1) {         valueB = accum;         // println ("B = " + valueB);       }       // Now clear the accumulator       serialChars.clear();       bJustBuilt = true;     } else if ((aChar >= 48) && (aChar <= 57)) {
      // If the char is between '0' and '9', save it.
      int aDigit = (int)(aChar - '0'); 
      serialChars.add(aDigit);
    }
  }
}

void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {

  noFill();

  if (axis == Y_AXIS) {  // Top to bottom gradient
    for (int i = y; i <= y+h; i++) {
      float inter = map(i, y, y+h, 0, 1);
      color c = lerpColor(c1, c2, inter);
      stroke(c);
      line(x, i, x+w, i);
    }
  }
}

void drawPlayer(int legPosy1, int legPosy2) {
  noStroke();
  pushMatrix();
  translate(250, 210);

  //shadow
  fill(5, 40, 4);
  ellipse(0, 275, 200, 100);

  //player's right leg top
  stroke(0);
  strokeWeight(20);
  pushMatrix();
  translate(40, 170);
  rotate(6);
  line(0, 0, 0, 100 - legPosy1); //70 is variable
  popMatrix();
  //player's right leg bottom

  //player's left leg top
  stroke(0);
  strokeWeight(20);
  pushMatrix();
  translate(-40, 170);
  rotate(-6);
  line(0, 0, 0, 100 - legPosy2); //70 is variable
  popMatrix();

  //player's left leg top

  //player's body
  noStroke();
  fill(88, 3, 1);
  triangle(0, -20, 100, 170, -100, 170);
  //player's head
  fill(147, 125, 97);
  ellipse(0, 0, 130, 90);
  //player's eyes
  //player's right eye
  pushMatrix();
  translate(30, 0);
  fill(255);
  ellipse(0, 0, 40, 40);
  fill(0);
  ellipse(10, 0, 15, 15);
  popMatrix();
  //player's left eye
  pushMatrix();
  translate(-30, 0);
  fill(255);
  ellipse(0, 0, 40, 40);
  fill(0);
  ellipse(10, 0, 15, 15);
  popMatrix();

  popMatrix();
}

void drawEnemy() {
  noStroke();
  fill(ghostCol);
  pushMatrix();
  translate(600, 300);
  scale(2 * (1.0 - runCounter/100.0));
  //ghost's body
  ellipse(0, 0, 210, 400);
  ellipse(0, 100, 210, 200);
  ellipse(0, 0, 200, 400);
  for (j = 0; j < 4; j++) {
    if ((j == 1) || (j == 2)) {
      h = 170;
    } else if ((j == 0) || (j == 3)) {
      h = 150;
    }
    ellipse(-75 +(j*50), h, 60, 90);
  }
  //ghost's eyes
  //ghost's right eye
  pushMatrix();
  translate(-40, 0);
  fill(250, 200, 200);
  ellipse(0, -100, 50, 50);
  fill(200, 150, 150);
  ellipse(0, -100, 40, 40);
  fill(150, 100, 100);
  ellipse(0, -100, 30, 30);
  fill(100, 50, 50);
  ellipse(0, -100, 20, 20);
  fill(50, 0, 0);
  ellipse(0, -100, 10, 10);
  popMatrix();
  //ghost's left eye 
  pushMatrix();
  translate(40, 0);
  fill(250, 200, 200);
  ellipse(0, -100, 50, 50);
  fill(200, 150, 150);
  ellipse(0, -100, 40, 40);
  fill(150, 100, 100);
  ellipse(0, -100, 30, 30);
  fill(100, 50, 50);
  ellipse(0, -100, 20, 20);
  fill(50, 0, 0);
  ellipse(0, -100, 10, 10);
  popMatrix();

  //ghost's mouth
  fill(250, 200, 200);
  ellipse(0, 20, 125, 175);
  fill(200, 150, 150);
  ellipse(0, 20, 105, 155);
  fill(150, 100, 100);
  ellipse(0, 20, 85, 135);
  fill(100, 50, 50);
  ellipse(0, 20, 65, 115);
  fill(50, 0, 0);
  ellipse(0, 20, 45, 95);
  for (i = 0; i < 5; i++) {     fill(ghostCol);     ellipse(-45 +(i*23), -52, 30, 45);   }   //ghost's right row of teeth   fill(250, 250, 225);   ellipse(-46, 35, toothWidth, 65);   fill(250, 250, 200);   ellipse(-46 +(10), 50, toothWidth, 65);   fill(250, 250, 225);   ellipse(-46 +(23), 65, toothWidth, 65);   fill(250, 250, 200);   ellipse(-46 +(33), 70, toothWidth, 65);   //ghost's left row of teeth   fill(250, 250, 225);   ellipse(-46 +(92), 35, toothWidth, 65);   fill(250, 250, 200);   ellipse(-46 +(79), 50, toothWidth, 65);   fill(250, 250, 225);   ellipse(-46 +(69), 65, toothWidth, 65);   fill(250, 250, 200);   ellipse(-46 +(59), 70, toothWidth, 65);   //ghost'sfront tooth   fill(250, 250, 225);   ellipse(-46 +(46), 75, toothWidth, 65);   popMatrix(); } void legSwitch() {   if (valueA > valueB + 50 && leftDown==false) {
    leftDown = true;
    runCounter+=10;
  } else if (valueB > valueA + 50 && leftDown==true) {
    leftDown = false;
    runCounter+=10;
  }
  runCounter = constrain(runCounter, 0, 100);

  ghostApprocah();
}

void ghostApprocah() {
  runCounter--;
}

void drawHill(){
  noStroke();
  fill(5, 47, 4);
  ellipse(width/2.5, height + (height/8), 1000, 700);
}

void drawScenery(){
    noStroke();
    fill(5, 27, 24);
    ellipse(width/2, height, 500, 700);
    ellipse(width/4, height, 600, 850);
}

scare