Video

Homework 05 – Arduino & Processing – Kierra Thomas

14 Nov

I used the potentiometer to control 3 servo motors as well as the processing sketch.

#include

Servo myservo; // create servo object to control a servo
Servo myservo2;
Servo myservo3;

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup() {

myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10);
myservo3.attach(11);

Serial.begin(9600);
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
myservo2.write(val);
myservo3.write(val);
delay(15); // waits for the servo to get there

delay(10);
int input = analogRead (potpin);
Serial.println (input);
}

import processing.serial.*;

Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph

void setup () {
// set the window size:
size(1000, 300);

println(Serial.list());// List all the available serial ports
myPort = new Serial(this, Serial.list()[9], 9600); // don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);

background(0); // set inital background:
}
void draw () {

}

void serialEvent (Serial myPort) {// get the ASCII string:

String inString = myPort.readString();

if (inString != null) { // trim off any whitespace:

inString = trim(inString);// convert to an int and map to the screen height:

float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);//map incomming value to height of screen

stroke(650, 444 ,20 );// draw the line:
line(xPos, height, xPos, height – inByte);

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s