Pages: [1]   Go Down
Author Topic: Analog Input with Processing  (Read 747 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi so I am currently trying to attach some infrared sensors to the Analog In ports of an Uno. I have tested and verified that the circuit works in the normal Arduino software, but I can't seem to get it to work in Processing. Here is the Code:

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;
int ledPin = 13;
int value = 0;

void setup()
{
  //println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 9600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
}

void draw()
{
  value = arduino.analogRead(0);
  println(value);
}

I am aware that I have to do some sort of conversion from byte to int, but I am getting a reading of 0 in my console. Thanks.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50304
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am aware that I have to do some sort of conversion from byte to int
I have no idea what you are talking about. The analogRead() method returns an int, which you (correctly) store in an int.

That is, it returns an int IF communication with the Arduino was successful. Otherwise, it returns a 0.

That you consistently get 0, but not when using the serial monitor and a different sketch, implies that either the wrong sketch is loaded on the Arduino OR that your Arduino is not connected to the first serial port in the list.

You have commented out the line that prints the list of serial ports. Are you POSITIVE that the Arduino is connected to the first port?

What sketch do you have on the Arduino?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the code for the Arduino sketch:

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
{
  Serial.begin(9600);          //  setup serial
}

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value
}

I used COM3 on the basis that in Arduino, it says Arduino Uno on COM3 so I assumed that was what I should use. I am starting to think, however, that might not be the case since I get the same message without the Uno plugged in.
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  Here is a processing sketch I found on the web that creates a bar graph based on serial input from your arduino. The Arduino must send a byte with a value of 0 to 255. Run the sketch and look at the text in the area that reports errors, that will tell you what number goes in this part:

Code:
arduinoPort = new Serial(this, Serial.list()[0], 115200);// [0] is the indicator for the port could be 0 ,1,2,3 etc.



Code:
/////////////////////////////////////////
//This example reads in a single byte value from 0 to 255 and graphs it.
//Basic serial communication code
//by Chang Soo Lee
//ITP, NYU
//Created 11/27/2005
/////////////////////////////////////////


import processing.serial.*;
Serial arduinoPort;
int serial = 1;
PFont font;
int numH = 370;


void setup()
{
   size(270, 440);
  println(Serial.list());
  arduinoPort = new Serial(this, Serial.list()[0], 115200);
  // Load the font. Fonts must be placed within the data
  // directory of your sketch. Use Tools > Create Font
  // to create a distributable bitmap font.
  // For vector fonts, use the createFont() function.
  println("ready for use");
  font = loadFont("ArialMT-48.vlw");
  smooth();
 
}

void draw () {
  background(255);

  if (arduinoPort.available() > 0) {
    serial = arduinoPort.read();
    
  }
  line(70, 70, 70, 370);  
    line(70, 370, 200, 370);
    fill(0);
    textFont(font, 11);
    text("Sensor\nValue", 22, 80);
    text("Analog Input", 95, 390);
      text(serial, 25, 110);
    fill(100 - serial, 0, 255 - serial * 2);
    rect(120, numH-serial * 3, 20, serial * 3);
  
}

EDIT: Made a mistake with code tags, message was confusing.
« Last Edit: May 25, 2012, 08:17:19 am by cyclegadget » Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50304
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hi so I am currently trying to attach some infrared sensors to the Analog In ports of an Uno. I have tested and verified that the circuit works in the normal Arduino software, but I can't seem to get it to work in Processing.
That's because that Processing sketch expects you to be running one of the Firmata sketches.

Quote
Here is the code for the Arduino sketch:
Which looks nothing like a Firmata sketch.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The second bit of code I posted was used in Arduino 1.0. I have been playing around with the code and I've found this:

   

// Example by Tom Igoe

import processing.serial.*;

Serial myPort;  // The serial port

void setup() {
  // 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  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  while (myPort.available() > 0) {
    int inByte = myPort.read();
    println(inByte);
  }
}

I seem to get repeating values of 53 52 13 10.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50304
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I seem to get repeating values of 53 52 13 10.
Time to consult an ASCII table.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Even if it is coded in ASCII, shouldn't it change based on the values of the analog input? I tried using readStringUntil('\n') instead, and I am getting values of ~253
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50304
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Even if it is coded in ASCII, shouldn't it change based on the values of the analog input?
Close Processing. Open the Serial Monitor, instead. Do you see changing values there?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In Serial Monitor, I am getting around 160 on a reflective surface (white) and 16 on a black surface. I also tried flashing StandardFirmata sketch and tried Serial Monitor again but I don't get any readings. I do get a reading from my own sketch though.
« Last Edit: May 25, 2012, 03:16:06 pm by Jaaagman » Logged

Pages: [1]   Go Up
Jump to: