Wednesday, March 16, 2016

Processing Labs

Oscillating circle


1. Java
2. Technically speaking, the only distinction is scope (where they live and die). Global variable has a scope of the file (or more depending on your linker), and a local variable has a scope of whatever block you're in.
3. void which means no return type
4. When the ellipse reaches the top or bottom of the screen, reverse its direction of movement

Potentiometer controlled ellipse


(no questions)

Etch-a-Sketch



(no questions)
Here is my code:

Processing:
import processing.serial.*;

Serial arduino;
int x;
int y;
int x_prev;
int y_prev;

void setup()
{
  x = 0;
  y = 0;
  x_prev = 0;
  y_prev = 0;
  
  size(320, 240);
  background(0,255,255);
    
  String port = Serial.list()[0];
  arduino = new Serial(this, port, 9600);
}

void draw()
{
  fill(0, 0, 0);
  if (!(x_prev == 0 && y_prev == 0))
  {
    line(x_prev, y_prev, x, y);
  }
  x_prev = x;
  y_prev = y;
}

void serialEvent(Serial port)
{
  try
  {
    String input = port.readStringUntil('*');
    if (input != null)
    {
      int[] vals = int(splitTokens(input, ",*"));
      x = vals[0];
      y = vals[1];
      println("x: " + x + ", y: " + y);
    }
  }
  catch (Exception e)
  {
    
  }
}

Arduino:

const int pot_left = A0;
const int pot_right = A1;

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

void loop()
{
  int pot_left_val = analogRead(pot_left);
  int pot_right_val = analogRead(pot_right);
  int left = map(pot_left_val, 0, 1023, 127, 0);
  int right = map(pot_right_val, 0, 1023, 127, 0);
  // pack the two values together into one
  Serial.write(44);
  Serial.print(left, DEC);
  Serial.write(44); // comma
  Serial.print(right, DEC);
  Serial.write(42); // asterisk
}

No comments:

Post a Comment