Lab 11 – Drawing Machines

Instructor: Marcela

Partner: Leslie Zhao, Amber Wang, and Eric Zhang

Materials: 1 stepper motor, 1 SH75440NE ic chip, 1 5 volt power supply, 1 power plug, 1 potentiometer from your kit, 1 Arduino and USB cable from your kit, Laser-cut mechanisms, Pens that fit the laser-cut mechanisms, 1 potentiometer

Experiment:

In Lab 11,  the task was to make a drawing machine using materials provided. Leslie and I built the circuits based on instructions online. We attached the arm to the stepper motor and jointed the Laser-cut mechanical arms together with three nails. With the code,  stepper_oneRevolution example, we managed to make the stepper motor work and the mechanical arms to move. As you can see in the model, the drawing machine does not work well. It simply enables the pen to leave marks randomly.

IMG_8724

After finishing our drawing machine, we tried to combine with the drawing machine of Amber and Eric. Since there were two mechanical arms that can control the pen, the machine now could draw some patterns.

IMG_8729

WechatIMG119

For the third part, we used the code stepper_speedControl and a potentiometer to control the speed of the motor. This time when we combined with Amber and Eric, we used to Laser-cut mechanisms to fix the position of the stepper motors in case that the motor would move when it drew. We could also contolled the machine to draw fast or slowly. However, the speeds of two motors were different, the lines drawn could not coincide very well. The second painting looked better. Finally, we exhibited out painting on the wall of the lab.

WechatIMG120

 

 

Final Project Proposal

Professor: Marcela

Partner of Final Project: Amber Wang

Personal Definition of Interaction

As far as I am concerned, interaction usually occurs when there are two or more objects that have influences on one another. Compare with one-way effect, information in the process of interaction is sent from different objects. This kind of information is in either form, such as sound, light and pressure. When one of the objects sends the information to the other objects, there must be responses from the receivers to the sender. In this way, the information flows in multiple directions and the objects affect each other mutually.

Final Project Proposal

Following the idea of interaction, I gradually came up with the idea of my final project. Amber and I would make a game named Beauty and the Beast. For the Processing part, we will sketch the game interface. We will set a castle, where the Beauty lives, at the bottom of the screen. There will be several spaths leading to the castle, and monsters will come forward to the castle along the paths. The job of the Beast is to kill monsters with his claws from a certain distance and protecting the Beauty. Like the monsters, the Beast can only move along the paths and cannot stay in the scope of the castle. If he wants to move from one path to another, he has to go across a tunnel connecting paths with each other. As time goes by, monsters would move in a faster speed. If the Beast can survive by keeping monsters away from the Beauty in a certain amount of time, the player would win. Otherwise, the monsters would catch the Beauty and the player would lose. We will project the scene of the game on the wall, instead of on a computer. Here is a sketch about the game.

屏幕快照 2017-04-26 下午11.19.18

For the Arduino part, we will make devices the player can use to control the Beast. Basically, we will laser cut four footprints which represent four directions of movement “UP”, “DOWN” “LEFT” and “Right”. Suggested by Marcela, we will use fabric adhering to the footprint. When the player steps on the footprint, there will be a change of evaluation which would make the game more interactive. There will also be pressure sensors right attached to the footprints so that the player can make the Beast move in a certain direction by stepping on the them. Here are two sketches about the footprint. The first one is about arrangement of footprints and the second one is about the a footprint’s construction.

屏幕快照 2017-04-26 下午11.06.40

屏幕快照 2017-04-26 下午11.06.46

Besides, we will use two gloves which the player will wear. There are sensors attached to the gloves, which can sense vibration. When the player moves his hands, the Beast moves his claws correspondingly to kill the monster. In this way, the whole body of the player gets involved in the game. And there is more space for the player to interact with the game, comparing with playing the game on a computer.

Critique on Interaction of Midterm Project

Actually, what originally generates my idea of the final project is my midterm project. We made a game named Dodgeplane as our midterm project. Generally, two players use joy stickers to control the movement of the planes. However, the environmental setting (including two joy stickers attached to two colorful balls, and a white box containing the Arduino board) does not fit the game very well. When players take the devices, they sometimes wonder how the devices relate to the game. Therefore, we are going to make some improvements about the environmental setting in our final project.

Lab 10: 3D Modeling

Professor: Marcela

Partner: None

Material: Tinkercad, a 4 digit display

In previous lectures, we have already leared how to use Tinkercad to create models for 3D printing. Today, we were required to use Tinkercad to design a 3D model of a wearable device, a game controller or a security camera that can support some of the sensors. And I decided to design a watch. I would draw the watchband and the container supporting  a 4 digit display (which is the dial plate) with Thinkercad.

I first create a solid cylinder with radius of 30mm and with height of 9 mm. In order to make a watchband, I inserted another hollow cylinder. However, these two clinder can not fit with each other very well, since I change rotated one of them. I deleted the original  hollow clinder and copied the solid cylinder to make these two models combine with each other. For the container for the 4 digit diplay, I created a half-cylinder. Before I made the model, I measured the size of the display with a venier caliper. I set the length of the display as the height of the half-cylinder, and the width of the display as the diameter of the bottom circle of the half-cylinder. In this way, cables connected with the  4 digit display woule be attched to the inner side of the the watchband and people can check the time with the diplay contained by the half-cylinde. Here are some pictures about the watch observed from different angles.

屏幕快照 2017-04-21 下午2.28.07

屏幕快照 2017-04-21 下午2.27.52

Through today’s lab, I became more familiar with Tinkercad and 3D printing. It is beneficial for me to  put it into practial project and use it in my lives.

Stamp Work

In today’s class, we learned about digital fabrication and how to use Illustrator. After watching a tutorial given by Marcela, I tried to make a 3D stamp by myself. In the beginning, I used a picture of stamp as a model to draw. I drew three circles with different sizes, which piled up with each other. Then I wrote “NYUSH IMA” and “ESME” on the edge of the smallest circle inside. But I failed to change the position of the text. With the help of Marcela, I found that I have to create another layer on which “ESME” lies on. That is to say, to move the position of “NYUSH IMA” and “ESME” separately, they should lie on different layers. Under this circumstance, I changed the position by moving three lines connected to the circle to which “ESME” is tied. For the center part of my stamp, I originally was attempted to draw a symbol of lips at the center. Marcela suggested me to use a picture of lips online, since drawing it by myself would take more time and might not look as good as expected. Thus I copied a picture of lips online and pasted it on Illustrator.  Then I transformed it into a shape and expanded the shape. When finishing drawing the stamp, I had to expand the whole stamp to make sure that everything is a shape. At the end, I added a curve around the outer circle, for the sake of cutting the stamp along the curve. I also rotate the whole shape vertically. Only in this way, can the text stamped on paper go from the left to the right.

Screen Shot 2017-04-11 at 8.02.07 PM

Finally, Marcela helped me to laser cut the shape. It took five times for the machine to cut the stamp. To make it work better, Marcela had to clean the dust from time to time.

I attached the stamp to the bottom of a cup, so that it is easier to use it. But somehow I could hardly stamp the complete shape.

IMG_5621

IMG_5622

I found it very interesting to create a real entity by using Illustrator and laser cutting. I think it would be great to combine laser cutting with my final project in the future.

Midterm Project – Dodgeplane

Professor: Marcela

Partner: Amber Wang

  1. Motivation

What motivated us to do the midterm project was to solve a daily problem of who should go down and pick the food delivery. Everytime I ordered food on Elema or Sherpas with my friends, nobody actually wants to go to pick the food delivery. In order to solve this life problem, Amber and I wanted to design a game which not only could involve two players in competeing against each other, but also could solve the problem in an entertaining way. Most importantly, it should take two players as short of timeas possible to play the game, in case of keeping the deliverman waiting for a long time.

2. Reference

Generally, we referred to two simple video games. The first one if a game called PlaneWar on Wechat. In this game, player (the plane at the botton of the screen) is required to shoot other enemy planes. The more planes you can shoot, the higher grade you would get. At the same time, you should avoid crashing other planes as well. It is an interesting game for challenging a player’s agility. Here is a picture about the PlaneWar.

屏幕快照 2017-03-31 下午4.32.23

The other reference is a game named Block Run on 4399.com.  As you can see from the picture following, this game is designed for the player (the red column) to dodge other grey columns. There are several chances for you to stay alive when failing to dodge greay columns.

屏幕快照 2017-03-31 下午4.42.53

3. Idea

With the help of two references, we decided to make a game called Dodgeplane. Before coding, we first stretched the idea on a paper. There are two players, which are attacker and defender. The screen would be seperated as two parts. The defender is on the upper part and the attacker is on the lower part. Both of them can move up, down, left and right on their own part. The attacker can send missiles to shoot the defender. In 60 seconds, if missiles hit the defender, the attacker would win. Otherwise, if the defender can sucessfully dodge missiles, the defender would win. Players can use “w”, “a”, “s”, “d” to make the defender move up, left, down, right and they can use the keyboard of UP, LEFT, DOWN, RIGHT to control the movement of the attacker. However, we decided to use joysticks to control the movement of planes at last. Comparing with crowding in front of a computer, joystickes would make the game more interactive and interesting.

IMG_5577

4. Coding

In order to improve work efficiency, I was in charged of the Processing part and Amber focued on the Arduino part (which mainly includes attaching joysticks to the game).

In the begining, I drew two ellispses to represent two airplanes. Amber would photoshop two pictures of planes to replace the ellipses when I finished coding. With the help of Jiwon,  I started with coding the defender and the attacker. Since planes can move in four directions, I set x, y of the defender and the attacker as variables. I used keyPressed function so that I could use keyboards to control the planes. I also print the keyCode I used so that I could find clearly whether the keyPressed function worked.

The most challenging part for me was to code the missile. There is no restrictive condition for the attacker to send missiles, and the player can send as many missiles as he  wants. The player could send missiles through pushing the space bar. Jiwon taugt how to use boolean to set the missiles. There were two variables, boolean shoot and boolean setStart. Initially, they are equal to “false”. When the player pushed space bar, they are equal to “true”. Under the circumstance of “true”, a missile would move forward and its position would go back to the initial position when it is out of the screen. However, in this way, the attacker cannot constantly send missiles. If the attacker send missiles before former ones reach the end of the screen, the former would turn back to the starting position. Here is a video about the problem of missiles.

Professor Antonius helped me to improve the code of missiles by adding an important part–resetting the missile. That is to say, only when a missile reaches the end of the screen, can the position of the missile be reset. As a result, the attacker cannot send another missile until the former one reaches the end of the screen. Missiles that has already been sent will keeping moving and would not be drove back by pushing the space bar.  Here is a video about the imoroved version of missiles.

It is after I took the class of Arraylist that I truly figured out how to code the missiles. Marcela’s examples helped me a lot to learn Arraylist. There were 14 examples from Array to Arraylist, from drawing random line to drawing random circles, from random fixed circles to circles with random speed. It took me a whole afternoon to understand the examples. By changing some values of codes, I managed to figure out what exactly every function works. Moreover, Marcela recommended a Youtube Tutorial which educative as well as funny. Although some coding in the video is outdated, the general logic remains unchanged.

For the missiles, I used Arraylist, the class function including display function and move function. The starting position of missile is the same as that of the attacker so that missiles can fly away from where the attacker is. In the move function, the x of missiles would not change while the y of missiles would decreases at a speed of 2. When referring to examples of Arraylist, I used mousePressed to add new missiles. But I found that once I pressed the mouse, there would be missiles continuously sent out even if I released the mouse button. Therefore, I used mouseClicked instead, which means, the moment you click the mouse, a missile would be sent out. By decreasing the size of missiles, I managed to control the amount of missiles I want to send accurately.

Another tricky part was to set that when a missile hits the defender, the attacker will win the game is over. I set time as a variable and the time would decrease by subtracting mills. When the difference between y2 (y of the defender) and y (y of the missile) is 0, the screen will show a picture that with “YOU LOSE” on the upper side and “YOU WIN” on the lower side. However, since I used Arraylist as a function to code missiles, the y of missiles would keep decreasing. That is to say, there is only a moment that y2-y=0. And it is so fast that the picture cannot showed on the screen. With the help of Avan, I used dist, the absolute value of distance, to express the distance between the defender and the missile.

Thanks to the “Tree Model” Professor Antonius taught me, I could organize my codes clearly. For example, I would first write p1.move(); as a title in the draw function. And under a new Tab “Move”, I would add the move function of plane 1 separately. Organizing code in this way could effectively helps me to avoid missing } and makes it more convenient for me to improve my codes.

5. Physical Surrounding

We tried to improve the physical surrounding of the game to make it more interactive. Since it is not comfortable to hold the joysticks we found in lab, we attached two rubber hemispheres to them. The rubber hemispheres could fit into people’s hands very well. this way, players would find it easier and more convenient to use the joysticks. IMG_5572

IMG_5575

6. Dodgeplane is Born!

7. Improvement

After we finished out project, some of my classmates tried the game. Most of them found that it is an unfair game. Since there is no limit to the amount of missiles, attacker can easily kill the defender. Lack of time to improve the project, I merely shortened the time the game lasts to 20 seconds. So far, there has been no player of defender that could hold on more than 15 seconds.  If we had more time, we would have improved the project by limiting the missiles the attacker can send at one time. For example, the attacker can only send five missiles at one time. After the fifth missile reaches the end of the screen, can the attacker send another round of missiles.

8. Reflection

I really appreciate the help of professors and teaching fellow. I would not have made Dodgeplane come into being without their assistance. The midterm project was a valuable opportunity for me to integrate codes I learned before and use them in a practical way. At the same time, I earned a strong sense of achievement from the project. In this process, I sometimes felt very depressive because of failing to write the codes as I expected. Thanks to the support of Marcela, I gradually understand that the process of learning is more important than a final result. The purpose is not only to make a game, but also to learn how to code, how to interact and enjoying the process of coding.

//Processing
int time;
Plane1 p1;
Plane2 p2;
import processing.serial.*;
String myString = null;
Serial myPort;
int NUM_OF_VALUES = 5;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues; 
PImage plane1, plane2, plane1win, plane2win;
int x1, y1; //plane1
int x2, y2; //plane2
ArrayList<Ball> balls = new ArrayList<Ball>();

void setup() {
  size(800, 700);
  //printArray(Serial.list());
  time = 20; 
  plane1 = loadImage("plane1.png");
  plane2 = loadImage("plane2.png");
  plane1win = loadImage("lose-win.png");
  plane2win =  loadImage("win-lose.png");
  setupSerial();
  x1 = width/2-50;
  y1 = height-100;
  x2 = width/2-50;
  y2 = 0;
}

void draw() {
  background(81, 54, 96);
  updateSerial();
  p1 = new Plane1();
  p2 = new Plane2();
  p1.display();
  p1.move();
  p2.display();
  p2.move();
  //p1w.display();
  //p2w.display();
  println(sensorValues[4]);
  //while (myPort.available() >0) {
  //valueFromArduino = myPort.read();
  //}
  // //  println(valueFromArduino);
  fill(255);
  textAlign(CENTER);
  textSize(32);
  text(time+1-(millis()/1000), width/2, height/2);

  if (sensorValues[4] == 1) {
    println("adding new ball");
    balls.add( new Ball(x1+48, y1));
  }
  for (int i=0; i<balls.size(); i++) {
    Ball b = balls.get(i); 
    b.move();
    b.display();
  }
}

void mouseClicked() {
  balls.add( new Ball(x1+48, y1));
}

class Plane1 {
  Plane1() {
  } 

  void display() {
    image(plane1, x1, y1, 100, 100);
  }

  void move() {
    if (sensorValues[1] < 300) {
      if (y1 > height/2 ) {
        y1 = y1 - 20;
      }
    }
    if (sensorValues[1] > 700 && sensorValues[4] == 0) {
      if (y1 < height -100) {
        y1 = y1 + 20;
      }
    }
    if (sensorValues[0] < 300) {
      if (x1 > 0) {
        x1 = x1 - 20;
      }
    }
    if (sensorValues[0] > 700) {
      if (x1 < width - 100) {
        x1 = x1 + 20;
      }
    }
  }
}

class Plane2 {
  Plane2() {
  } 

  void display() {
    image(plane2, x2, y2, 100, 100);
  }

  void move() {
    if (sensorValues[3] < 300) {
      if (x2 > 0) {
        x2 = x2 - 20;
      }
    }
    if (sensorValues[3] > 700) {
      if (x2< width - 100) {
        x2 = x2 + 20;
      }
    }
    if (sensorValues[2] < 300) {
      if (y2 > 0 ) {
        y2 = y2 - 20; 
      }
    }
    if (sensorValues[2] > 700 && sensorValues[2] < 1023) {
      if (y2 < height/2 - 100 ) {
        y2 = y2 + 20;
      }
    }
  }
}

void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[2], 115200);
  myPort.clear();
  // Throw out the first reading,
  // in case we started reading in the middle of a string from the sender.
  myString = myPort.readStringUntil( 10 );  // 10 = 'n'  Linefeed in ASCII
  myString = null;
  sensorValues = new int[NUM_OF_VALUES];
}

void updateSerial() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); // 10 = 'n'  Linefeed in ASCII
    if (myString != null) {
      String[] serialInArray = split(trim(myString), ",");
      if (serialInArray.length == NUM_OF_VALUES) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

class Ball{
  float x, y, size;
  color clr;
  float xspeed, yspeed;
  
Ball (float tempX, float tempY){
    x = tempX;
    y = tempY;
    size = 5;
    clr = color(0);
    xspeed = 0;
    yspeed = 2;
  }
  void display(){
    fill(clr);
    ellipse(x, y, size, size);
  }
  void move() {
    x += 0;
    y -= yspeed;
  }
}


//Arduino 
int buttonPin = 0;

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

void loop() {
  int pot1 = analogRead(A0);
  int pot2 = analogRead(A1);
  int pot3 = analogRead(A2);
  int pot4 = analogRead(A3);

  buttonPin = digitalRead(12);

  Serial.print(pot1);
  Serial.print(",");  // put comma between sensor values
  Serial.print(pot2);
  Serial.print(",");
  Serial.print(pot3);
  Serial.print(",");
  Serial.print(pot4);
  Serial.print(",");
  //  Serial.print(buttonPin);

  if (pot2 == 1023) {
    Serial.print(1);
  }else{
    Serial.print(0);
  }
  Serial.println();


  //Serial.write(buttonPin);
  delay(10);


}

Lab 6 Communication between Arduino and Processing

Instructor: Marcela Godoy

Partner: Amber Wang

Goal: From Aruduino to Processing & From Processing to Arduino

Material Needed: Power Supply, breadboard, cables, 1 buzzer, 1 switch, 1 resister

Experiments:

Before gatting to start experiments, we referred to examples in class to revise how to make Arduino and Processing communicate. The first experiement was about sending value from Arduino to Processing. Basically, we wanted to use a switch to change the color shown on Processing window. We first set a variable on Arduino buttonPin. The value of buttonPin is either 0 or 1, which represents switching off or swittching on. On Processing, the value of buttonPin would be translated to color changes. When buttonPin is 0, the color would not change. When buttonPin is 1, the color would change. However, when we tested our codes, we found that the color kept flashing when someone push the button. Thanks to the help of Luis, we learned that adding another condition, pv == 0. This condition means that when buttonPin is 1 and at the same time, the previous buttonPin value was 0, the color would change. Otherwise, if the previous buttonPin value has already been 1 (someone already pushed the button), the color would not change. Also we changed to delay from 10 to 100, so that Processing can receive information properly. Here is the video of this experiment.

The second experiment was about sending from Processing to Arduino. Basically, we wanted to turn on and off a buzzer by changing the value sending from Processing. When someone clicked on the Processing window, the buzzer would alarm. To write codes, I reffered to one of the examples about using Processing to control an Led light. The variable we set was mousePressed. If the value of mousePressed is H, the buzzerPin value would be 1 and the buzzer would alarm. Here is the video of the second expriement.

Through Lab 6, we both have a better understanding about how Arduino and Processing communicate with each other. It would be very helpful to our midterm project.

//From Arduino to Processing 

//Arduino 
int buttonPin = 0;

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

void loop(){
  buttonPin = digitalRead(12);
  
  Serial.write(buttonPin);
  
  delay(100);

//Processing 
import processing.serial.*;

Serial myPort;
int valueFromArduino, pv;

void setup(){
  size(500, 500);
  myport = new Serial(this, Serial.list()[1], 9600);
}
void draw(){
  while (myPort.available() > 0) {
    valueFromArduino = myPort.read();
  }
  if (valueFromArduino == 1 && pv == 0){
    int r = int(random(0, 255));
    int g= int(random(0, 255));
    int b = int(random(0, 255));
    background(r,g,b);}
    pv = valueFromArduino;
}


//From Processing to Arduino 

//Processing 
import processing.serial.*;

Serial myPort;
int valueFromArduino;


void setup() {
  size(500, 500);
  background(0);

  printArray(Serial.list());
  
  myPort = new Serial(this, Serial.list()[3], 9600);
}

void draw() {
  // to send a value to the Arduino
  if (mousePressed) {
    myPort.write('H');
  } else {
    myPort.write('L');
  }
}

//Arduino 
int valueFromProcessing;
int buzzerPin = 12;

void setup() {
  Serial.begin(9600);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    valueFromProcessing = Serial.read();
  }
   
  if(valueFromProcessing == 'H'){
    digitalWrite(buzzerPin, 1);
  }
  else{
     digitalWrite(buzzerPin, 0);
  }

  delay(100);
}

Stupid Pet Trick_Stupid Toy

Project: Stupid Pet Trick

Instructor: Marcela Godoy

Partner: None

Goal: Make a stupid toy with fading eyes controlled by a tilt switch

Material Needed: Power Supply, breadboard, cables, 10 LED, tilt switch, 10 resistors, paper board

Idea:

The process of coming up with an idea was a biit tricky. Originally, I wanted to make a heart shape formed by 20 LEDs. Controlled by an accelerometer, the LEDs could shine and fade gradually. After I talked about the idea with Professor Antonious, he suggested me to make it relatively simple. After all, it was merely aa small project. Therefore, I decided to make a stupid toy with two eyes, which would be made up of 10 LEDs. Instead of being controled by an accelerometer, I used a tilt switch. When I shake the tilt switch, the eyes sould shine. Otherwise, the eyes would fade gradually.

Code:

Generally, I referred to two examples, Fade and Button when writing the codes. With the help wo Marcela and Lewis, I set two variables in order to use tilt switch to sense speed. The first variable I set was buttonState. When someone shakes the tilt switch, the button is on so the buttonState is 1. While the button is off, the buttonState is 0. The second variable I set was a, which was used for measuring the brightness. As you can see my code below, as button is on for more times, there will be many buttonStates that equals 1, and a is a certain amount of added buttonStates. In this way, as someone shakes the tilt switch, a would get bigger so that brightness would be higher and the LED would get brighter. While when buttonState achieves 255, the highest value, the brightness would decrease by a certain fadeamount. Since I wrote the code before I actully finished my circuits, I used a LED to test my code. When testing my codes, I found that the LED faded too fast. Hence, I made the fadeamount smaller so that people can notice that the brightness is changing.

Circuits:

Actually, I think the most diffifult for me in completeing this project was to build an organized circuit. To attach LEDS to a paper board, I cut two small holes to insert two legs of the LED. After that, I soldered the legs with cables. In case of short circuits, I tied tap to every connected part. When I plugged the end of cables to the breadboard, I found they were loosen. Because there were twenty cables in total, it was easily for me to mess them up, which made me fail to light all the LEDS up in the first time.

In order to fix this problem, I intended to tie all the positive ends together and all the negative ends together. Then I connected a single cable with either side. It worked t first, but half of the LEDs were broken due to short circuits. As a result, I had to change the LEDs and soldered them again, which made me suffer from a heavier work.

IMG_5463

With the help of Marcela, I tried to organize the circuits clearly. We cut part of the legs of the resistors, in case that they would connect with other parts of cables. As the photo shows, the resistors and cables are very short and tightly plugged into the breadboard. In this way, the negative ends and the positive sides were seperated to two sides of the breadboard. When I connected cables of one LED, it suddenly blew up. In the end, I had to replace with with a drawn eye. To make the toy look more natural, I attached the tilt switch to the back of a cute cat paw. After I completed the circuits, I tested the project. I found that the tilt switch was too sensitive sometimes that LEDs could not fade but became brighter again and again. To make the buttonState zero, I had to prevent the sitch from moving by pressing it against a table. Due to the time limit, I did not continued to improve my project at this aspect.

IMG_5465

Although I have been through many troubles, I have learned a lot about how to write codes and build circuits. I think that will be very helpful to my midterm and final projects.

const int buttonPin = 2;    
const int ledPin =  9;     
int brightness = 0;
int fadeAmount = 3;
int a = 0;
int buttonState = 0;    
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  Serial.print(buttonState);
  Serial.print(",");
  Serial.print(a);
  Serial.print(",");
  analogWrite(ledPin, brightness);
  Serial.println(brightness);
  a = a + buttonState;
  
  if (buttonState == 1) {
    brightness = 70 + a*5;
  }

  if (brightness == 255){
    a = 0;
  }

  brightness = brightness - fadeAmount;

  if (brightness <= 0) {
   brightness = 0;
  }

 delay(100);
    
}

Lab 4 – Sensors

Lab 4 – Sensors

Instructor: Marcela Godoy

Individual Project

Goal: Test Moisture Sensor

Material Needed: a Moisture Sensor, Power Supply, breadboard, cables,1 buzzer,

Experiments:

In Lab 4, I chose one of the sensors, the moisture sensr to test with a buzzer. I intended to control the tone of the buzzer by changing the moisture variables. First of all, I built the Arduino curcits. There are four cables connected to the sensors. The yellow cable connects to A0 (which the code showed), the white one connects to nothing. the red one connects to power, and the black one connects to the ground. I also coded that the buzzerPin was 8. After that, I had to write codes based on the codes provided online. Basically, I used the map function to convert sensor values (input) into buzzerTone (output). The higher the moisture is, the louder the buzzer will be. As a result, the buzzer alarmed in a low sound even if the value of moisture was zero. With the help of Marcela, I managed to make the tone of the buzzer fade. I combined the if & else statement with the map function. I coded that if the sensor value is smaller than 100, there should be no tone at all. And if the sensor valule is more than 100, the buzzr would alarm frequently. In this way, the buzzer would not keep alarming. Here is the video about my project.

Generally, the Arduino coding is still a very challenging part for me so far. Through this lab, I feel it necessary to refer to codes online frequently. I can’t wait to start my own stupid pet trick project.

int sensorPin = A0; 
int sensorValue = 0;
float buzzerTone;
int buzzerPin = 8;    

void setup() {
  
    Serial.begin(9600); 
    pinMode(buzzerPin, OUTPUT); 
}
void loop() {

    sensorValue = analogRead(sensorPin); 
    Serial.print("sensor = " ); 
    Serial.println(sensorValue); 
    delay(1000); 
    
    if (sensorValue < 100){
      noTone(buzzerPin);
    }else{
    buzzerTone = map (sensorValue, 0, 800, 0, 600);
    tone(buzzerPin, buzzerTone); 
    }
}

Lab III – Animation in Processing

Instructor: Marcela Godoy

Goal: Basic Code on Processing

Materials: Processing

Experiments:

In lab III, I worked on basic coding on Processing. After reading instructions online, I started to create the pattern. Firstly, I drew a cat on a square paper. Because the square paper can help better position the points and lines of the pattern. When I was coding the eyes of the cat, it was still very hard to arrange them in a proper way. I had to adjust the codes for many times. Thanks to Sam, I got to learn that the adding the code,  println(mouseX, mouseY), would make the coordeinate of my mouse showed on Processing. In this way, I could find ideal coordinates effectively. When I  coded the ears of the cat, I found that I failed to fill colors in the ears. With the help of Lewis, I realized the ear I drew was made of only two lines. To fill in color, I was supposed to change it to a triangle. And I should also draw the ear first so that the face can cover it.

The most challenging problem I came accross was about rotating the cat. In order to rotate the cat around its nose, I translated the center point from (0,0) to (width/2, height/2). As a result, the cat was dismembered that, the face was at the center of the background but other parts, like nose, eyes, were in the same place as before. Faced with this problem, Lewis suggested me to subtract 250 from every number. And I still had to make some detailed changes to make every parts match perfectly. Since I failed to translate the center point at the begining, I made the work more complecated.

 

屏幕快照 2017-02-24 下午9.18.30

 

 

It was the first time for me to use Processing to code. Generally, I reached the basic requirement, but I was unfamillar with the processing languange and logic. Therefore, I think it necessary for me to practice coding after class. After all, practice makes perfect.