2012年5月6日 星期日

Final Project Anton Carlson & Anthony Lee Audi Racer

Final project by Anton Carlson 97470043 and Anthony Lee 97470105. This this project we created a racing game called Audi Racer. This game also includes a a gas pedal, brake pedal and steering wheel. The video shows how the game is played. In this section there is also a picture that shows how we created the pedals. As can be seen from the picture we used a rubber band to allow the pedal to return to it's original location. The pod resisters will send a value to the processing via the ardiuno and the serial port.



Balloon game

In this game we create two balloons and make the balloon blow up with the press of an arduino button.



Processing Code:

import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
Balloon b1, b2;
PImage Back;
void setup()
{
  size(1024, 530);
  b1 = new Balloon(200,300,false);
  b2 = new Balloon(800,300,true);
  background(0);
  frameRate(10);
  // Open the port that the board is connected to and use the same speed (9600 bps)
  println(Serial.list());
  String portName = Serial.list()[1];
  port = new Serial(this, portName, 9600);
}
void draw()
{
      println(val);
      if (0 < port.available())
      { // If data is available,
        val = port.read(); // read it and store it in val
      }
      if(b1.state==0)
      {
          if(val==50)
             b1.increaseBalloon();
          if(val==48)
             b1.decreaseBalloon();
      }
      else if(b1.state==1)
      {
        //b1.increaseCircle();
      }
     
      if(b2.state==0)
      {
          if(val==51)
             b2.increaseBalloon();
          if(val==48)
             b2.decreaseBalloon();
      }
      else if(b2.state==1)
      {
        //b2.increaseCircle();
      }
     
}
class Balloon
{
  public int Xpos=470;
  public int Ypos=330;
  public int BalloonSize=20;
  public int state=0;
  public int CicleSize=20;
  public boolean isRightHanded;
  public Balloon(int X, int Y, boolean rightHanded)
  {
    this.Xpos = X;
    this.Ypos = Y;
    this.isRightHanded = rightHanded;
  }
  public void increaseBalloon()
  {
       BalloonSize+=20;
       if(BalloonSize>300)
       {
         state=1;
       }
       drawBalloon();
  }

  public void decreaseBalloon()
  {
       if(BalloonSize<0)
      {
          BalloonSize=1;
      }
      BalloonSize-=20;
      drawBalloon();
  }
  private void drawBalloon()
  {
       background(0);
       if(isRightHanded)
         ellipse(Xpos-BalloonSize/2, Ypos, BalloonSize, BalloonSize);
       else
         ellipse(Xpos+BalloonSize/2, Ypos, BalloonSize, BalloonSize);
  }
  public void increaseCircle()
  {
      CicleSize+=40;
      drawExplode();
  }
  public void drawExplode()
  {
       background(Back);
       noFill();
       ellipse(Xpos, Ypos, CicleSize, CicleSize);
  }
}

Arduino Code:

int switchPin = 4; // Switch connected to pin 4
int switchPin2 = 6;
int var=0;
void setup()
{
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
  void loop()
  {
  if(digitalRead(switchPin) == HIGH && digitalRead(switchPin2) ==HIGH)
  {
    var=1;
  }
  else if(digitalRead(switchPin) == HIGH && digitalRead(switchPin2) ==LOW)
  {
    var=2;
  }
  else if(digitalRead(switchPin) == LOW && digitalRead(switchPin2) ==HIGH)
  {
    var=3;
  }
  else if(digitalRead(switchPin) == LOW && digitalRead(switchPin2) ==LOW)
  {
    var=0;
  }
  Serial.print(var);
  delay(100); // Wait 100 milliseconds
}

Fishing Game final project



Fishing game using Arduino processor. We made 5 buttons for the following controlls: up , down, left, right, and press button.

2012年4月19日 星期四

HW10_02: Speaker


// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin1 = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
int speakerOut = 9;
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup() {
  pinMode(speakerOut, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  /*
   #define c 3830 // 261 Hz
   #define d 3400 // 294 Hz
   #define e 3038 // 329 Hz
   #define f 2864 // 349 Hz
   #define g 2550 // 392 Hz
   #define a 2272 // 440 Hz
   #define b 2028 // 493 Hz
   #define C 1912 // 523 Hz
   */
  int tone_0 = 3830;
  int tone_1 = 3400;
  int tone_2 = 3038;
  int tone_3 = 2864;
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {  
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(tone_0 /2);
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_0 /2);
  } /*
  else {
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_0 /2);
  }*/

  if (buttonState1 == HIGH) {  
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(tone_1 /2);
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_1 /2);
  }/*
  else {
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_1 /2);
  }*/

  if (buttonState2 == HIGH) {  
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(tone_2 /2);
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_2 /2);
  } /*
  else {
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_2 /2);
  }*/

  if (buttonState3 == HIGH) {  
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(tone_3 /2);
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_3 /2);
  } /*
  else {
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(tone_3 /2);
  }*/
}

2012年4月15日 星期日

Piano show



In this show Anthony Lee and Anton Carlson play a classic number. Video shot by  Johnny Lee.