Week16

Reflect and Processing

1DD750CD03201CF2A54B865D03F68C70

The board used serial protocol to connect the Computer.

I use a software called Processing,and this is my processing code.

Processing

1
import processing.serial.*;
Serial myPort;
int lf = 10;

void setup(){
  size(300,300);
  //fill(0,255,0);
  //ellipse(100,100,100,100);
  //myPort = new Serial(this,"/dev/cu.usbmodem1421",9600);
  myPort = new Serial(this,"/dev/tty.usbserial",9600);
}

void draw(){
  while(myPort.available() > 0){
    String str = myPort.readStringUntil(lf);
    //println(str);
    if(str!=null){
      int value = Integer.parseInt(trim(str));
      int nValue = (value-200)*4;
      println(nValue);
      background(nValue);
    }

  }
}

At dark

If the board is in dark,the interface will be white.

At bright light

When the board on front of light,the interface will be dark.

Arduino

1
#include "SoftwareSerial.h"

const int analogInPin =  A2;  // Analog input pin that the potentiometer is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

const int Rx = 1; // this is physical pin 7
const int Tx = 2; // this is physical pin 6
SoftwareSerial mySerial(Rx, Tx);

void setup() {
//  pinMode(3, OUTPUT);
  pinMode(Rx, INPUT);
  pinMode(Tx, OUTPUT);
  mySerial.begin(9600); // send serial data at 9600 bits/sec
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  mySerial.println(outputValue);
  delay(100);

}

Serial Output

Test Arduino and Processing together

1DD750CD03201CF2A54B865D03F68C70

File download

Code Download


This work is licensed under a Creative Commons Attribution 4.0 International License.

文章目录
  1. 1. Reflect and Processing
    1. 1.1. Processing
      1. 1.1.1. At dark
      2. 1.1.2. At bright light
    2. 1.2. Arduino
    3. 1.3. Test Arduino and Processing together
  2. 2. File download
,