Class exercises

Here are the 3 examples that Minh and I did from our in class work.

Problem 1

const int sensorPin = A1;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
}

void loop() {
// put your main code here, to run repeatedly:

//Read stuff from processing

while (Serial.available()) {
Serial.read() == '\n';

//Write to processing
int sensor = analogRead(sensorPin);
delay(1);

Serial.print(sensor);
Serial.print('\n');
}
}

//Processing code

//Move a ellipse on screen horizontally from arduino sensor

//import processing.serial.*;
//Serial myPort;
//int xPos = 0;
//
//void setup() {
// size(960, 720);
// printArray(Serial.list());
// String portname = Serial.list()[1];
// println(portname);
// myPort = new Serial(this, portname, 9600);
// myPort.clear();
// myPort.bufferUntil('\n');
//}
//
//void draw() {
// background(255);
// ellipse(xPos, height/2, 30, 30);
//}
//
//
////Wont do anything until there is something in serial buffer
//void serialEvent(Serial myPort) {
// String s = myPort.readStringUntil('\n');
// s = trim(s);
// if (s!=null) {
// println(s);
// int val = int(s);
// xPos = (int)map(val, 0, 1023, 0, width);
// }
// myPort.write('\n');
//}

 

Problem 2

const int ledPin = 11;
int brightness = 0;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(ledPin, OUTPUT);
}

void loop() {
while (Serial.available()) {
brightness = Serial.parseInt();
if (Serial.read() == '\n') {
analogWrite(ledPin, brightness);
// int sensor = analogRead(sensorPin);
delay(1);
Serial.println(brightness);
}
}
}

//Processing code

//Light an LED on the Arduino with varying brightness depending on how far left or right the mouse is

//import processing.serial.*;
//Serial myPort;
//int xPos = 0;
//int brightness = 0;
//
//void setup() {
// size(960, 720);
// printArray(Serial.list());
// String portname = Serial.list()[1];
// println(portname);
// myPort = new Serial(this, portname, 9600);
// myPort.clear();
// myPort.bufferUntil('\n');
//}
//
//void draw() {
// background(255);
// xPos = mouseX;
// ellipse(xPos, height/2, 30, 30);
// brightness = int(map(xPos, 0, width, 0, 255));
//}
//
//
////Wont do anything until there is something in serial buffer
//void serialEvent(Serial myPort) {
// String s=myPort.readStringUntil('\n');
// s=trim(s);
// int val = parseInt(s);
// if (s!=null){
// println("Brightness: " + val);
// }
// myPort.write(brightness+"\n");
//}

 

Problem 3

const int ledPin = 11;
int brightness = 0;
const int sensorPin = A0;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(ledPin, OUTPUT);
}

void loop() {
while (Serial.available()) {
int onOff = Serial.parseInt();
if (Serial.read() == '\n') {
digitalWrite(ledPin, onOff);
int sensor = analogRead(sensorPin);
delay(1);
Serial.println(sensor);
}
}
}

//Processing code

//import processing.serial.*;
//Serial myPort;
//int xPos=0;
//int yPos=0;
//boolean onOff=false;
//boolean onOff2=false;
//PVector velocity;
//PVector gravity;
//PVector position;
//PVector acceleration;
//PVector wind;
//float drag = 0.99;
//float mass = 50;
//float hDampening;
//
//void setup(){
// size(960,720);
// printArray(Serial.list());
// String portname=Serial.list()[1];
// println(portname);
// myPort = new Serial(this,portname,9600);
// myPort.clear();
// myPort.bufferUntil('\n');
// noFill();
// position = new PVector(width/2, 0);
// velocity = new PVector(0,0);
// acceleration = new PVector(0,0);
// gravity = new PVector(0, 0.5*mass);
// wind = new PVector(0,0);
// hDampening=map(mass,15,80,.98,.96);
//}
//
//void draw(){
// background(255);
//
// velocity.x*=hDampening;
//
// applyForce(wind);
// applyForce(gravity);
// velocity.add(acceleration);
// velocity.mult(drag);
// position.add(velocity);
// acceleration.mult(0);
// ellipse(position.x,position.y,mass,mass);
// if (position.y > height-mass/2) {
// velocity.y *= -0.9; // A little dampening when hitting the bottom
// position.y = height-mass/2;
// onOff = true;
// }
// else{
// onOff = false;
// }
//}
//
//void serialEvent(Serial myPort){
// String s=myPort.readStringUntil('\n');
// s=trim(s);
// int val = parseInt(s);
// if (s!=null){
// wind.x=map(val,0,1023,-2, 2); //2 instead of 1 to make it stronger
// }
// myPort.write(int(onOff)+"\n");
//}
//
//void applyForce(PVector force){
// // Newton's 2nd law: F = M * A
// // or A = F / M
// PVector f = PVector.div(force, mass);
// acceleration.add(f);
//}
//
//void keyPressed(){
// if (keyCode==LEFT){
// wind.x=-1;
// }
// if (keyCode==RIGHT){
// wind.x=1;
// }
// if (key==' '){
// mass=random(15,80);
// position.y=-mass;
// velocity.mult(0);
// }
//}

 

 

Leave a Reply