Digital Electronics Lecture Assignment 9 (Edited)

Below is my Processing code:

import processing.serial.*;

Serial teensySerial;

int val = 0;
int r = 0;
int g = 0;
int b = 0;

int shapeX = 0;
int shapeY = 0;
int shapeSize = 0;
int shapeSpeed = 0;

void setup() {
  size(600, 600);
  printArray(Serial.list());
  String usbPortName = Serial.list()[8];
  teensySerial = new Serial(this, usbPortName, 9600);
  
  while ( teensySerial.available() > 0) {
    teensySerial.read();
  }
}

void draw() {
  if(teensySerial.available() > 0) {
    val = teensySerial.read();
    println(val);
    if (val == 0) {
      r = 200;
      g = 200;
      b = 0;
    } else if (val == 1) {
      r = 200;
      g = 0;
      b = 0;
    } else if (val == 2) {
      r = 0;
      g = 200;
      b = 0;
    } else if (val == 3) {
      r = 0;
      g = 0;
      b = 200;
    } 
  }
  noStroke();
  fill(r, g, b);
  rect(0, 0, 600, 600);
}

Below is my Arduino code:

int speedPot = 0;
int potVal = 0;

unsigned long lastStepTime = 0;
int count = 0;
int totalCount = 4;
int tempo = 0;

int ledPins[4] = { 8, 9, 10, 11};
int totalLeds = 4;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 4; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  speedPot = analogRead(A13);
  tempo = map(speedPot, 0, 1023, 100, 1000);
  sequenceForward();
}

void countUp() {
  count++;
  if (count == totalCount) {
    count = 0;
  }
}

void sequenceForward() {
  if (millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    digitalWrite(ledPins[count], LOW);
    countUp();
    digitalWrite(ledPins[count], HIGH);

    Serial.write(count);
  }
}





Leave a comment

Design a site like this with WordPress.com
Get started