Go Down

Topic: Analog Input with Processing (Read 783 times) previous topic - next topic

Jaaagman

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.

PaulS

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?

Jaaagman

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.

cyclegadget

#3
May 25, 2012, 12:26 am Last Edit: May 25, 2012, 03:17 pm by cyclegadget Reason: 1
 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: [Select]
arduinoPort = new Serial(this, Serial.list()[0], 115200);// [0] is the indicator for the port could be 0 ,1,2,3 etc.



Code: [Select]
/////////////////////////////////////////
//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.

PaulS

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.

Go Up