Digital Electronics Lab #10

Video:

Arduino Code:

int potPin = A14;
int potVal = 0;
int mappedPotVal = 0;

int potPin2 = A15;
int potVal2 = 0;
int mappedPotVal2 = 0;

int potPin3 = A13;
int potVal3 = 0;
int mappedPotVal3 = 0;

int potPin4 = A12;
int potVal4 = 0;
int mappedPotVal4 = 0;

int potPin5 = A16;
int potVal5 = 0;
int mappedPotVal5 = 0;

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

}

void loop() {

  Serial.write(0);


  potVal = analogRead(potPin);
  mappedPotVal = map(potVal, 0, 1023, 1, 255);
  Serial.write(mappedPotVal);


  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);

}

Processing Code:

import processing.serial.*;

Serial teensySerial;

int inputVal = 0;
int inputVal2 = 0;
int inputVal3 = 0;
int inputVal4 = 0;
int inputVal5 = 0;
int inputVal6 = 0;

int circleSize = 0;
int circleY = 0;
int circleX = 0;
int circleSpeedX = 0;
int circleSpeedY = 0;

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

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

void draw() {
  if (teensySerial.available() >= 6) {
    inputVal= teensySerial.read();
    if (inputVal == 0) {
      inputVal2 = teensySerial.read();
      inputVal3 = teensySerial.read();
      inputVal4 = teensySerial.read();
      inputVal5 = teensySerial.read();
      inputVal6 = teensySerial.read ();
    }
  }

  background(inputVal6, 200, 200);

  fill(0, 100, inputVal5);

  circleSize = (int)map(inputVal2, 1, 255, 0, 500);
  
  circleX = (int)map(inputVal4, 1, 255, 0, 500);

  circleSpeedY = (int)map(inputVal3, 1, 255, -50, 50);


  circleY += circleSpeedY;
  if (circleY > 500 + circleSize/2) {
    circleY = -circleSize/2;
  } else if (circleY < -circleSize/2) {
    circleY = 500 + circleSize/2;
  }

  circle(circleX, circleY, circleSize);
}

Leave a comment

Design a site like this with WordPress.com
Get started