Digital Electronics Lab 10

Below is my Processing Code:

import processing.serial.*;

Serial teensySerial;

int inputVal = 0;
int pot1 = 0;
int pot2 = 0;
int pot3 = 0;
int pot4 = 0;
int pot5 = 0;


int squareSize = 0;
int squareX =0;
int squareSpeedX = 0;
int squareY = 0;
int squareSpeedY = 0;

void setup() {
  frameRate(30);
  size(500, 500);

  String usbPortName = Serial.list()[8];
  print(usbPortName);
  teensySerial = new Serial(this, usbPortName, 9600);
}

void draw() {

  if (teensySerial.available() >= 3) {
    inputVal = teensySerial.read();
    if (inputVal== 0) {
      pot1 = teensySerial.read();
      pot2 = teensySerial.read();
      pot3 = teensySerial.read();
      pot4 = teensySerial.read();   
      pot5 = teensySerial.read();
    }
  }

  background(255, 250, 250);

  fill(pot1, pot2, pot3);

  squareSpeedY = (int)map(pot4, 1, 255, -60, 60);

  squareSpeedX = (int)map(pot5, 1, 255, -60, 60);

  squareX += squareSpeedX;
  if (squareX >500 + squareSize/2) {
    squareX = -squareSize/2;
  } else if (squareX < -squareSize/2) {
    squareX = 500 + squareSize/2;
  }

  squareY += squareSpeedY;
  if (squareY >500 + squareSize/2) {
    squareY = -squareSize/2;
  } else if (squareY < -squareSize/2) {
    squareY = 500 + squareSize/2;
  }

  square(squareX, squareY, 250);
}

Below is my Teensy code:

int potPin1 = A13;
int potVal1 = 0;
int mappedPotVal1 = 0;

int potPin2 = A12;
int potVal2 = 0;
int mappedPotVal2 = 0;

int potPin3 = A16;
int potVal3 = 0;
int mappedPotVal3 = 0;

int potPin4 = A17;
int potVal4 = 0;
int mappedPotVal4 = 0;

int potPin5 = A18;
int potVal5 = 0;
int mappedPotVal5 = 0;

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

void loop() {

  Serial.write(0);

  potVal1 = analogRead(potPin1);
  mappedPotVal1 = map(potVal1, 0, 1023, 1, 255);
  Serial.write(mappedPotVal1);

  potVal2 = analogRead(potPin2);
  mappedPotVal2 = map(potVal2, 0, 1023, 1, 255);
  Serial.write(mappedPotVal2);

  potVal3 = analogRead(potPin3);
  mappedPotVal3 = map(potVal3, 0, 1023, 1, 255);
  Serial.write(mappedPotVal3);

  potVal4 = analogRead(potPin4);
  mappedPotVal4 = map(potVal4, 0, 1023, 1, 255);
  Serial.write(mappedPotVal4);

  potVal5 = analogRead(potPin5);
  mappedPotVal5 = map(potVal5, 0, 1023, 1, 255);
  Serial.write(mappedPotVal5);
  
  delay(50);
}

The first three pots control the color red, green, and blue.
The fourth pot controls the square’s movement in the y-axis.
The fifth pot controls the square’s movement in the x-axis.

Leave a comment

Design a site like this with WordPress.com
Get started