Arduino Analog Input

by craig_fahner @ 4:16 am October 17, 2011

Breadboard view:

Arduino Code:

int sensorValue0;
int sensorValue1;
 
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}
 
void loop() {
  sensorValue0 = analogRead(A0);
  sensorValue1 = analogRead(A1);
  Serial.print(sensorValue0);
  Serial.print(",");
  Serial.println(sensorValue1);
 
  delay(10);
}

Processing code:

import processing.serial.*;
 
int sensor0 = 0;
int sensor1 = 0;
 
Serial myPort;
 
void setup() {
  size(200, 200);
 
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
}
 
void draw() {
  // do your stuff here!
}
 
void serialEvent(Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
 
  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // split the string on the commas and convert the
    // resulting substrings into an integer array:
    int[] sensors = int(split(inString, ","));
    // if the array has at least two elements, you know
    // you got the whole thing.  Put the numbers in the
    // sensor variables:
    if (sensors.length >=2) {
      sensor0 = sensors[0];
      sensor1 = sensors[1];
    }
  }
}

»

TrackBack URI

Leave a comment

You must be logged in to post a comment.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2017 CMU Electronic Media Studio II, Fall 2011, Section A | powered by WordPress with Barecity