Assignment 7.4

posted Aug 22, 2010, 12:32 PM by Unknown user
This took a little while to get but it turned out pretty well. Sometimes the potentiometers will be a little unresponsive. 

Arduino Code:

int pot1 = 1; //delcare variables
int pot2 = 2;
int val = 0;

void setup() {
  Serial.begin(9600); //data will ne sent and received 9600 bps
}

//I will be using the punctuation method for this code

void loop() {
  val = analogRead(pot1);  //read the first potentiometer and store it in val
  Serial.print(val, DEC); // print the valuse just stored
  Serial.print(",");      //print a comma to organize data
  
  val = analogRead(pot2); //read the second potentiometer and store it in val
  Serial.println(val, DEC); //print the value just stored
}

Processing Code:

import processing.serial.*;  //import the processing library
Serial myPort;  //delcare variables
float xpos;
float ypos;

void setup() {
  size (500, 500); //set the window siae to 500x500
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n'); //store values in a buffer
  background(255); //sets the background color to white
}
void draw() {
  fill(0); //sets the fill color to black
  stroke(0);//sets the outline color to black
  ellipse(xpos, ypos, 3, 3); //draw a circle at the designated postion
}

void serialEvent(Serial myPort) {
  String myString = myPort.readStringUntil('\n'); // read the buffer
  if (myString !=null) {
    myString = trim(myString);
    int sensors[]= int(split(myString, ',')); //split string at commas
    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++){
    print ("Sensor" + sensorNum + ":" + sensors[sensorNum] + "\t"); //print values
    }
    println(); 
    if (sensors.length > 1) {
      xpos = map(sensors [1], 0, 1023, 0, 500); // set values so circles stays in window
      ypos = map(sensors [2], 0, 1023, 0, 500);
    }
  }
}
    
Comments