Your browser does not support the canvas tag.

Week 5
From past years Ultrasonic Range Finder
Okay here's the thing. There are two types of ultrasonic range finders, the kind that does the number crunching for us and the kind that requires us to do it ourselves. Our kit comes with the latter. They also have a smaller range and are much cheaper.
The way the sensors in our kits work: one side sends a signal for a short burst (ten microseconds) the other side listens for the audio to reflect back. The longer it takes to get back the further the object is!
This should be easy to write in Processing HOWEVER, processing has Millis() which are millisectonds, but we need the arduino to work at micorseconds, which the Arduino does but Processing doesn't! SO we need to combine the ultrasound software with the a firmata like "simple analog firmata". In this example I have the firmata information sending on analog pin 0.
I Frankensteined the ultrasound example from this page with the "simple analog firmata" code example.
#include <Firmata.h>

byte analogPin = 0;

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;

void analogWriteCallback(byte pin, int value)
{
  if (IS_PIN_PWM(pin)) {
    pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
    analogWrite(PIN_TO_PWM(pin), value);// value
  }
}

void setup()
{
  Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  Firmata.begin(57600);
}

void loop()
{
  while (Firmata.available()) {
    Firmata.processInput();
  }
  // do one analogRead per loop, so if PC is sending a lot of
  // analog write messages, we will only delay 1 analogRead


  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;

  if (analogPin == 0) {
    Firmata.sendAnalog(0, distance);
  }
  else {
    Firmata.sendAnalog(analogPin, analogRead(analogPin));
  }

  analogPin = analogPin + 1;
  if (analogPin >= TOTAL_ANALOG_PINS) analogPin = 0;

}

Your browser does not support the canvas tag.

Here's a timer code. Use this to create a delay while the arduino loads, or it can pause the game before you restart. It also uses the random() function to create a new random color every second. The millis() starts counting in thousdandths of a second as soon as the sketch starts and never stops. To use this as a timer we need to set a variable to the current millis, then check the elapsed time from the moment you set the variable. When the time limit is hit, the variable resets to the new millis() time and the process repeats until there is a power failure or Skynet goes online.
	long timer;
	int timelength = 1000;
	color c = color(255, 10, 100); 
	void setup(){
	  size(300, 300); 
	  timer = millis();
	}

	void draw(){
	  if (millis() - timer >= timelength){
	    timer = millis();
	    print("bing!"); 
	    c = color (random(255), random(255), random(255)); 
	  }
	  fill(c); 
	  rect(100, 100, 100, 100); 
	}
 


Pong 2 - multiball
 
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

ArrayList <BallClass> ballslist = new ArrayList<BallClass>();
PVector player1, player2; 
int playerW = 16; // width of paddle
int playerH = 60; // hieght of paddle
int analogPin = 0;
boolean arduinoToggle = false; 
int pot1 = 300; 


void setup() {
  size(600, 400); 
  smooth();// make the graphics look purdy. 
  rectMode(CENTER); // draw rects from center
  player1 = new PVector(20, height/2); 
  player2 = new PVector(width -20, height/2); 
  for (int i = 0; i < 20; i ++) {
    ballslist.add(new BallClass());
  }
  if (arduinoToggle) {
    arduino = new Arduino(this, Arduino.list()[15], 57600);
  }
  //ball = new BallClass();
}

void draw () {
  background (50);// background color
  if (arduinoToggle) {
    pot1 = arduino.analogRead(analogPin);
  }
  println(pot1); 
  player1.y = map(pot1, 0, 115, 0, height); 
  rect(player1.x, player1.y, playerW, playerH); 
  rect(player2.x, player2.y, playerW, playerH); 
  for (int i = ballslist.size()-1; i >= 0; i--) { 
    // gets the current ball and names this instance "dball".
    BallClass dball = ballslist.get(i); 
    dball.display();
    dball.move();
  }

  //ball.display();
  //ball.move();
}

class BallClass {
  PVector ballLoc; 
  PVector velocity;
  int ballsize = 16; // width of ball
  float speed = 3; 
  float rot = .1; 
  BallClass() {
    ballLoc = new PVector(width/2, height/2);
   rot = random( .2); 
    speed = random(1, 5); 
   velocity = new PVector(random(1, 4), random(-1, 1));
    //if (random(2) < 1) {
    //  velocity.x = -velocity.x;
    //}
  }

  void move() {
    velocity.x = cos(rot)*(speed); // find the X and Y that will be the next "step" of the ball.
    velocity.y = sin(rot)*(speed); 
    ballLoc.add(velocity);
    if (ballLoc.x > width) {
      velocity.x *= -1;
       rot = PI - rot;
    }
    if (ballLoc.x < 0) {
      velocity.x *= -1;
       rot = PI - rot;
    }
    if (ballLoc.y > height) {
      velocity.y *= -1;
       rot = -rot; 
    }
    if (ballLoc.y < 0) {
      velocity.y *= -1;
       rot = -rot; 
    }
    if (paddleCollide(player1.x, player1.y)) {
      ballLoc.x = player1.x + playerW/2 + ballsize/2;
      rot = PI - rot;
    }
    
    
    if (paddleCollide(player2.x, player2.y)) {
      ballLoc.x = player2.x - playerW/2 - ballsize/2;
      rot = PI - rot;
    }
  }

  boolean paddleCollide(float px, float py) {
    if (ballLoc.x + ballsize/2 > px - playerW/2 && 
      ballLoc.x - ballsize/2 < px + playerW/2 && 
      ballLoc.y + ballsize/2 > py - playerH/2 && 
      ballLoc.y - ballsize/2 < py + playerH/2 
      ) {
      velocity.x *= -1;
      return true;
    } else {
      return false;
    }
  }

  void display() {
    ellipse(ballLoc.x, ballLoc.y, ballsize, ballsize);
  }
}
 


laser cutter check list.

If there's a fire
Q: "I know you didn't mention this material but is it okay if I cut n" - where n is all materials not mentioned.
A: Probably not and you'd 100% need to run it by Paul before you try. Some materials, when cut, create gasses which can kill you and worse, dammage the laser cutter.
-->