All posts by douglastran

Final Project

Processing basketball game

f1

f2

code:

 

arduino:

void setup() {
Serial.begin(9600);     // initialize serial communication
}

void loop() {
// read the inputs

int xpos = analogRead(A0);
int ypos = analogRead(A1);

Serial.print(xpos);
Serial.print(“,”);                   // add a comma
Serial.println(ypos);
delay(10);                           // delay before sending the next set
}

 

processing:

/*
tweak from openprocessing example
http://www.openprocessing.org/sketch/104227
*/

import processing.serial.*;
Serial myPort;

PImage basketball, hoop, net, court;
float xpos, ypos, xvel, yvel, accel, n, xbasket, ybasket, boardHeight, give, space, i, wind;
int value = 0, dir = 1, level, score, hiscore, ballColor, comment;
boolean falling, Shoot, backboard, unlock1, unlock2, reset;
PShader blur;
void setup() {
size (1000, 800 );
basketball = loadImage(“Basketball.png”);
court = loadImage(“court2.png”);
hoop = loadImage(“hoop.png”);
xpos = width/5;
ypos = height-300;
yvel = .5;
xvel = .5;
accel = 2;
falling = false;
Shoot = false;
xbasket = random(400, 800);
ybasket = random(200, 600);
backboard = false;
give = 20;
score = 0;
space =  0;
level = 1;
hiscore = 0;
comment = 0;
unlock1 = false;
unlock2 = false;
ballColor = 4;
reset = false;

// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my  Arduino, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[5], 9600);
// don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);
}

void draw() {
//background();
image (court, 0, 0, width, height);

fill(255);
if (level == 2) {
unlock1 = true;
}
if (level == 3) {
unlock2 = true;
}
boardHeight = 180 – level*30;
n = 180 – level*30;

noStroke();
fill(255);
if (score > hiscore) {
hiscore = score;
}
textSize(20);
text(“HIGHSCORE: ” + hiscore, 10, height – 70);
text(“SCORE: ” + score, 10, height – 30);
level = score/10 + 1;
text(“LEVEL: ” + level, 10, height – 50);
image(hoop, xbasket, ybasket, 150, 100);
println(score + “, ” + level);
if (xpos >= width||ypos >= height) {
reset = true;
}
if (xvel > 25) {
xvel = 25;
}
if (yvel >25) {
yvel = 25;
}
rect(0, 0, yvel*20, 5);
rect(0, 10, xvel*20, 5);
rect(xbasket+n, ybasket – boardHeight, 5, boardHeight+5);
if (Shoot == false||falling==false) {
xvel = (mouseX-width/5)/7;
yvel = (height-300 – mouseY)/7;
stroke(255);
line(xpos, ypos, xpos+xvel*5, ypos-yvel*5);
noStroke();
}
if (Shoot == true||falling==true) {
falling = true;
if (backboard) {
yvel -= 5 ;
xpos -= 1.25*xvel;
give = 40;
} else {
xpos+= xvel;
}

yvel -= accel;
ypos -= yvel;
}
if (xpos > xbasket-20 + n && xpos < xbasket + n + 30 && ypos > ybasket -boardHeight-5 && ypos < ybasket) {
backboard = true;
}
if (xpos>xbasket-10 && xpos<xbasket+20 && ypos < (ybasket + give) && ypos > (ybasket -10)) {
yvel = -yvel;
xvel = -xvel;
}
if (xpos>xbasket+20 && xpos<(xbasket+n) && ypos < (ybasket + give) && ypos > (ybasket -10) && yvel < 1 ) {
xpos = width/5;
ypos = height-300;
yvel = .1;
xvel = .1;
accel = 1;
falling = false;
Shoot = false;
ybasket = random(300, 700);
xbasket = random(400, 800);

//  n -=30;
backboard = false;
//  boardHeight-=30;
score += 1;
comment = score + 1;
}
if (ballColor == 4) {
fill(242, 98, 15);
}
if (ballColor == 1) {
fill(255, 0, 0);
}
if (ballColor == 2) {
fill(0, 255, 0);
}
if (ballColor == 3) {
fill(random(0, 255), random(0, 255), random(0, 255));
}

image (basketball, xpos, ypos, 40, 40);
/* if (unlock1 == true){
fill(255, 0, 0);
ellipse(int(width-40), int(height – 40), 30, 30);
fill(0,255, 0);
ellipse(int(width-80), int(height – 40), 30, 30);
fill(random(0,255),random(0,255),random(0,255));
ellipse(int(width-120), int(height – 40), 30, 30);
} */
if (reset == true) {
xpos = width/5;
ypos = height-300;
yvel = .1;
xvel = .1;
accel = 1;
n = 150;
falling = false;
Shoot = false;
ybasket = random(200, 500);
xbasket = random(400, 800);
backboard = false;
boardHeight = 150;
give = 15;
score = 0;
i = 0;
space = 0;
comment = 1;
reset = false;
}
}

void mouseClicked() {
if (value == 0) {
Shoot = true;
}
}

void keyPressed() {
if (key == ‘s’) {
Shoot = true;
}
if (key == ‘r’) {
reset = true;
}
if ( unlock1 == true) {
if (key == ‘c’) {
ballColor = 1;
}
if (key == ‘v’) {
ballColor = 2;
}
if (key == ‘b’) {
ballColor = 3;
}
if (key == ‘n’) {
ballColor = 4;
}
}
}

void serialEvent(Serial thisPort) {

// read the serial buffer:
String inputString = thisPort.readStringUntil(‘\n’);
println(inputString);

if (inputString != null)
{
// trim the carrige return and linefeed from the input string:
inputString = trim(inputString);

// split the input string at the commas
// and convert the sections into integers:
int sensors[] = int(split(inputString, ‘,’));

// if we have received all the sensor values, use them:
if (sensors.length == 2) {
// scale the sliders’ results to the paddles’ range:
mouseX = int(map(sensors[0], 0, 1023, 0, width));
mouseY = int(map(sensors[1], 0, 1023, 0, height));
}
}
}

Servo hw

s1

code:

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees
{                                  // in steps of 1 degree
myservo.write(pos);              // tell servo to go to position in variable ‘pos’
delay(10);                       // waits 15ms for the servo to reach the position
}
for(pos = 90; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
{
myservo.write(pos);              // tell servo to go to position in variable ‘pos’
delay(10);                       // waits 15ms for the servo to reach the position
}
}

lol shield animation

For the lol shield I did a grid animation:

code:

/*
Basic LoL Shield Test

Writen for the LoL Shield, designed by Jimmie Rodgers:
http://jimmieprodgers.com/kits/lolshield/

This needs the Charliplexing library, which you can get at the
LoL Shield project page: http://code.google.com/p/lolshield/

Created by Jimmie Rodgers on 12/30/2009.
Adapted from: http://www.arduino.cc/playground/Code/BitMath

History:
December 30, 2009 – V1.0 first version written at 26C3/Berlin

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Version 3 General Public
License as published by the Free Software Foundation;
or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <avr/pgmspace.h>  //AVR library for writing to ROM
#include <Charliplexing.h> //Imports the library, which needs to be
//Initialized in setup.

//Sets the time each frame is shown (milliseconds)
const unsigned int blinkdelay = 1000 / 50;

/*
The BitMap array is what contains the frame data. Each line is one full frame.
Since each number is 16 bits, we can easily fit all 14 LEDs per row into it.
The number is calculated by adding up all the bits, starting with lowest on
the left of each row. 18000 was chosen as the kill number, so make sure that
is at the end of the matrix, or the program will continue to read into memory.

Here PROGMEM is called, which stores the array into ROM, which leaves us
with our RAM. You cannot change the array during run-time, only when you
upload to the Arduino. You will need to pull it out of ROM, which is covered
below. If you want it to stay in RAM, just delete PROGMEM
*/
PROGMEM const uint16_t BitMap[][9] = {
{1,0,0,0,0,0,0,0,0},
{3,0,0,0,0,0,0,0,0},
{7,0,0,0,0,0,0,0,0},
{15,0,0,0,0,0,0,0,0},
{31,0,0,0,0,0,0,0,0},
{63,0,0,0,0,0,0,0,0},
{127,0,0,0,0,0,0,0,0},
{255,0,0,0,0,0,0,0,0},
{511,0,0,0,0,0,0,0,0},
{1023,0,0,0,0,0,0,0,0},
{2047,0,0,0,0,0,0,0,0},
{4095,0,0,0,0,0,0,0,0},
{8191,0,0,0,0,0,0,0,0},
{16383,0,0,0,0,0,0,0,0},
{16383,0,1,0,0,0,0,0,0},
{16383,0,3,0,0,0,0,0,0},
{16383,0,7,0,0,0,0,0,0},
{16383,0,15,0,0,0,0,0,0},
{16383,0,31,0,0,0,0,0,0},
{16383,0,63,0,0,0,0,0,0},
{16383,0,127,0,0,0,0,0,0},
{16383,0,255,0,0,0,0,0,0},
{16383,0,511,0,0,0,0,0,0},
{16383,0,1023,0,0,0,0,0,0},
{16383,0,2047,0,0,0,0,0,0},
{16383,0,4095,0,0,0,0,0,0},
{16383,0,8191,0,0,0,0,0,0},
{16383,0,16383,0,1,0,0,0,0},
{16383,0,16383,0,3,0,0,0,0},
{16383,0,16383,0,7,0,0,0,0},
{16383,0,16383,0,15,0,0,0,0},
{16383,0,16383,0,31,0,0,0,0},
{16383,0,16383,0,63,0,0,0,0},
{16383,0,16383,0,127,0,0,0,0},
{16383,0,16383,0,255,0,0,0,0},
{16383,0,16383,0,511,0,0,0,0},
{16383,0,16383,0,1023,0,0,0,0},
{16383,0,16383,0,2047,0,0,0,0},
{16383,0,16383,0,4095,0,0,0,0},
{16383,0,16383,0,8191,0,0,0,0},
{16383,0,16383,0,16383,0,0,0,0},
{16383,0,16383,0,16383,0,1,0,0},
{16383,0,16383,0,16383,0,3,0,0},
{16383,0,16383,0,16383,0,7,0,0},
{16383,0,16383,0,16383,0,15,0,0},
{16383,0,16383,0,16383,0,31,0,0},
{16383,0,16383,0,16383,0,63,0,0},
{16383,0,16383,0,16383,0,127,0,0},
{16383,0,16383,0,16383,0,255,0,0},
{16383,0,16383,0,16383,0,511,0,0},
{16383,0,16383,0,16383,0,1023,0,0},
{16383,0,16383,0,16383,0,2047,0,0},
{16383,0,16383,0,16383,0,4095,0,0},
{16383,0,16383,0,16383,0,8191,0,0},
{16383,0,16383,0,16383,0,16383,0,0},
{16383,0,16383,0,16383,0,16383,0,1},
{16383,0,16383,0,16383,0,16383,0,3},
{16383,0,16383,0,16383,0,16383,0,7},
{16383,0,16383,0,16383,0,16383,0,15},
{16383,0,16383,0,16383,0,16383,0,31},
{16383,0,16383,0,16383,0,16383,0,63},
{16383,0,16383,0,16383,0,16383,0,127},
{16383,0,16383,0,16383,0,16383,0,255},
{16383,0,16383,0,16383,0,16383,0,511},
{16383,0,16383,0,16383,0,16383,0,1023},
{16383,0,16383,0,16383,0,16383,0,2047},
{16383,0,16383,0,16383,0,16383,0,4095},
{16383,0,16383,0,16383,0,16383,0,8191},
{16383,0,16383,0,16383,0,16383,0,16383},
{16383,2,16383,0,16383,0,16383,0,16383},
{16383,2,16383,2,16383,0,16383,0,16383},
{16383,2,16383,2,16383,2,16383,0,16383},
{16383,2,16383,2,16383,2,16383,2,16383},
{16383,10,16383,2,16383,2,16383,2,16383},
{16383,10,16383,10,16383,2,16383,2,16383},
{16383,10,16383,10,16383,10,16383,2,16383},
{16383,10,16383,10,16383,10,16383,10,16383},
{16383,42,16383,10,16383,10,16383,10,16383},
{16383,42,16383,42,16383,10,16383,10,16383},
{16383,42,16383,42,16383,42,16383,10,16383},
{16383,42,16383,42,16383,42,16383,42,16383},
{16383,170,16383,42,16383,42,16383,42,16383},
{16383,170,16383,170,16383,42,16383,42,16383},
{16383,170,16383,170,16383,170,16383,42,16383},
{16383,170,16383,170,16383,170,16383,170,16383},
{16383,682,16383,170,16383,170,16383,170,16383},
{16383,682,16383,682,16383,170,16383,170,16383},
{16383,682,16383,682,16383,682,16383,170,16383},
{16383,682,16383,682,16383,682,16383,682,16383},
{16383,2730,16383,682,16383,682,16383,682,16383},
{16383,2730,16383,2730,16383,682,16383,682,16383},
{16383,2730,16383,2730,16383,2730,16383,682,16383},
{16383,2730,16383,2730,16383,2730,16383,2730,16383},
{16383,10922,16383,2730,16383,2730,16383,2730,16383},
{16383,10922,16383,10922,16383,2730,16383,2730,16383},
{16383,10922,16383,10922,16383,10922,16383,2730,16383},
{16383,10922,16383,10922,16383,10922,16383,10922,16383},

};

void setup() {
LedSign::Init(DOUBLE_BUFFER | GRAYSCALE);  //Initializes the screen
}
void loop() {
for (uint8_t gray = 1; gray < SHADES; gray++)
DisplayBitMap(gray);  //Displays the bitmap
}

void DisplayBitMap(uint8_t grayscale)
{
boolean run=true;    //While this is true, the screen updates
byte frame = 0;      //Frame counter
byte line = 0;       //Row counter
unsigned long data;  //Temporary storage of the row data
unsigned long start = 0;

while(run == true) {

for(line = 0; line < 9; line++) {

//Here we fetch data from program memory with a pointer.
data = pgm_read_word_near (&BitMap[frame][line]);

//Kills the loop if the kill number is found
if (data==18000){
run=false;
}

//This is where the bit-shifting happens to pull out
//each LED from a row. If the bit is 1, then the LED
//is turned on, otherwise it is turned off.
else for (byte led=0; led<14; ++led) {
if (data & (1<<led)) {
LedSign::Set(led, line, grayscale);
}
else {
LedSign::Set(led, line, 0);
}
}
}

LedSign::Flip(true);

unsigned long end = millis();
unsigned long diff = end – start;
if ( start && (diff < blinkdelay) )
delay( 3*blinkdelay – diff );
start = end;

frame++;
}
}

midterm

1 2


#include <NewPing.h>
#include <Servo.h>

#define MAX_DISTANCE 200
#define TRIG_PIN  4
#define ECHO_PIN  2

#define LEFT 0
#define CENTER 90
#define RIGHT 180
#define ENABLE1 3
#define INPUT1 9
#define INPUT2 8
#define ENABLE2 11
#define INPUT3 7
#define INPUT4 6

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Servo ultrasonicServo;

float dangerThreshold = 40.0;

int currentPos = 0;

#define LEFT_FORWARD_RIGHT_FORWARD {HIGH, HIGH, LOW, HIGH, HIGH, LOW}
#define LEFT_OFF_RIGHT_FORWARD {LOW, HIGH, LOW, LOW, HIGH, LOW}
#define LEFT_FORWARD_RIGHT_OFF {HIGH, LOW, LOW, HIGH, LOW, LOW}
#define LEFT_REVERSE_RIGHT_FORWARD {HIGH, HIGH, HIGH, LOW, HIGH, LOW}
#define LEFT_FORWARD_RIGHT_REVERSE {HIGH, HIGH, LOW, HIGH, LOW, HIGH}
#define LEFT_REVERSE_RIGHT_REVERSE {HIGH, HIGH, HIGH, LOW, LOW, HIGH}
#define LEFT_OFF_RIGHT_OFF {HIGH, HIGH, LOW, LOW, LOW, LOW}
#define LEFT_FREEWHEEL_RIGHT_FREEWHEEL {LOW, LOW, LOW, LOW, LOW, LOW}

#define FORWARD LEFT_FORWARD_RIGHT_FORWARD
#define REVERSE LEFT_REVERSE_RIGHT_REVERSE
#define LEFT_TURN LEFT_OFF_RIGHT_FORWARD
#define RIGHT_TURN LEFT_FORWARD_RIGHT_OFF
#define ROTATE_LEFT LEFT_REVERSE_RIGHT_FORWARD
#define ROTATE_RIGHT LEFT_FORWARD_RIGHT_REVERSE
#define BRAKE LEFT_OFF_RIGHT_OFF
#define FREEWHEEL LEFT_FREEWHEEL_RIGHT_FREEWHEEL

#define SLOW 130;
#define MEDIUM 195;
#define FAST 255;

int throttle = MEDIUM;

void setup() {

pinMode(ENABLE1, OUTPUT);
pinMode(ENABLE2, OUTPUT);
pinMode(INPUT1, OUTPUT);
pinMode(INPUT2, OUTPUT);
pinMode(INPUT3, OUTPUT);
pinMode(INPUT4, OUTPUT);

ultrasonicServo.attach(5);

servo_position(CENTER);
}

void loop() {

float distanceForward = ping();

if (distanceForward > dangerThreshold)
{
drive_forward();
}
else
{
brake();

servo_position(LEFT);
float distanceLeft = ping();

servo_position(RIGHT);
float distanceRight = ping();

if (distanceLeft > distanceRight && distanceLeft > dangerThreshold)       //if left is less obstructed
{

rotate_left();
}
else if (distanceRight > distanceLeft && distanceRight > dangerThreshold) //if right is less obstructed
{

rotate_right();
}
else
{

u_turn();
}

servo_position(CENTER);
}
}

void freewheel(){
const int driveControl[] = FREEWHEEL;
drive(driveControl);
delay(25);

void brake(){
const int driveControl[] = BRAKE;
drive(driveControl);
delay(25);
}

void drive_forward(){
const int driveControl[] = FORWARD;
drive(driveControl);
}

void drive_backward(){
const int driveControl[] = REVERSE;
drive(driveControl);
}

void turn_left(){
const int driveControl[] = LEFT_TURN;
drive(driveControl);
delay(600);

}

void turn_right(){
const int driveControl[] = RIGHT_TURN;
drive(driveControl);
delay(600);

}

void rotate_left(){
const int driveControl[] = ROTATE_LEFT;
drive(driveControl);
delay(300);
}
void rotate_right(){
const int driveControl[] = ROTATE_RIGHT;
drive(driveControl);
delay(300);
}

void u_turn(){
const int driveControl[] = ROTATE_RIGHT;
drive(driveControl);
delay(600);
}

void drive(const int settings[6]){

if (settings[0] == HIGH)
analogWrite(ENABLE1, throttle);
else
digitalWrite(ENABLE1, LOW);

if (settings[1] == HIGH)
analogWrite(ENABLE2, throttle);
else
digitalWrite(ENABLE2, LOW);

digitalWrite(INPUT1, settings[2]);
digitalWrite(INPUT2, settings[3]);
digitalWrite(INPUT3, settings[4]);
digitalWrite(INPUT4, settings[5]);
}

void servo_position(int newPos){

if (newPos > currentPos){
for(int pos=currentPos; pos < newPos; pos += 1)
{
ultrasonicServo.write(pos);
delay(15);
}
currentPos = newPos;
}
else if (newPos < currentPos){
for(int pos=currentPos; pos > newPos; pos -= 1)
{
ultrasonicServo.write(pos);
delay(15);
}
currentPos = newPos;
}
}

float ping(){
delay(50);

unsigned int seconds = sonar.ping();

if (seconds == 0)
return MAX_DISTANCE;
else
return seconds / US_ROUNDTRIP_CM;
}

hw week 5

I created a led lit cigarette box that is controlled by a tilt sensor which I attached to the top of the box which turns the led light on when the top of the box is tilted open. The sensor was a bit buggy and led flickered on and off quite a bit.

int SensorPin = 2;
int LEDPin = 3;

int LEDstate = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 50;

void setup()
{
pinMode(SensorPin, INPUT);
digitalWrite(SensorPin, HIGH);
pinMode(LEDPin, OUTPUT);
}

void loop()
{
int switchstate;

reading = digitalRead(SensorPin);

if (reading != previous) {

time = millis();
}

if ((millis() – time) > debounce) {

switchstate = reading;

if (switchstate == HIGH)
LEDstate = LOW;
else
LEDstate = HIGH;
}
digitalWrite(LEDPin, LEDstate);

previous = reading;
}

hw5.2 hw5