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:
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?
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.
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:
arduinoPort = new Serial(this, Serial.list()[0], 115200);// [0] is the indicator for the port could be 0 ,1,2,3 etc.
/////////////////////////////////////////
//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.
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.
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);
}
}
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
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.