Issue with reading Arduino Port

I have a phototransistor and an LED, when I test in Arduino I do get readings from the phototransistor, but when trying to get the same readings in Processing I just get continuous 0’s. Can any one help? I’ve placed the code below. The phototransistor is connected to port A1. Thanks!

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

int PhotoPin1 = 1;
Arduino port;
float W= 0;
void setup(){
  port= new Arduino(this, Arduino.list()[17], 9600);
  port.pinMode(PhotoPin1, Arduino.INPUT);
}

void draw() {
  
  float w_sensorRaw1= port.analogRead(PhotoPin1);
  println(w_sensorRaw1);
  println(W);
  delay(1000);
}

@Ryan12,
possibly this is first time you use serial interface in processing,
and you started with a
arduino ( firmata )
example.

this ONLY can work if the arduino is loaded with the firmata sketch.

when you say that you see the data incoming
in the arduino IDE monitor ( terminal emulator )
you not have firmata loaded to arduino,
you have your own code sending data continuously by serial.println()
in that case you only use in processing the

import processing.serial.*;
and how to setup see for a example for that library (only )

//__________________________________
also can you tell us where the

come from?
it is good practice to first print the port list.

I had uploaded the firmata sketch to the Arduino, but I still was having the issue. The 17 comes from the list, I just had it commented out after I had it set. Also regarding using just serial, can I do that if I’m using one of the pins as an input, because I need to read it and then output what the pin’s value is?

ok you want use FIRMATA
check on what i just play here

ok thanks, I’ll take a look and see if it solves the problem!

did you try my processing code example? with your firmata arduino?

I was away from my space yesterday but back today so I’m going to try it this afternoon and I’ll let you know. Thanks so much for following up

I tried your example and I still only get 0.0 values

it looks to me that despite having the correct port connected there is 0 connection to the Arduino. Code that has worked in the past is no longer working

the reason why i made this version of code was to
allow that loop back by making a jumper cable

A0 ↔ D5

so when you click on the [ D5 ] button the D5 output should change
( what you also can verify with a voltmeter )
and via that jumper cable the A0 input jump from
0 to 5V ( what you should see in the A0 bar graph and the volt number…

//__________________
if you think that your code has worked
but now still runs, but show allways 0
there might be a hardware / or port problem
of the arduino board. But i only see burned out Douts
( from a short circuit ) or arduinos what stopped working completely.

anyhow go back to
Arduino IDE, load a blinky and a analog read example and test with the
build in monitor.

Right so when I test analogRead in Arduino and just jump between 3.3V, 5V, and Ground the serial monitor shows it properly reading these values. I also got a Hello World to work with serial, and a primitive Serial read to show changing values code below:

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

Serial myUno;

String w_sensorRaw1;

void setup() 
{ 
 println(Arduino.list());
 myUno= new Serial(this, Serial.list()[18], 9600);
 println(Arduino.list()[18]);
} 
 
void draw() 
{ 
   w_sensorRaw1= myUno.readStringUntil('\n'); 
   println(w_sensorRaw1);

delay(100);
} 

This gives me appropriate varrying outputs in Processing, but then if I simply change the commands to those associated with an Arduino I get 0 again.

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

Arduino myUno;

  int w_sensorRaw1;

void setup() 
{ 
 println(Arduino.list());
  myUno= new Arduino(this, Arduino.list()[18], 9600);
  println(Arduino.list()[18]);
} 

void draw() 
{ 
   w_sensorRaw1= myUno.analogRead(0); 
   println(w_sensorRaw1);

delay(100);
} 

Any thoughts?

yes,

  • in the first code you load the firmata lib to processing sketch but not use it,
    ( and all lines what contain the word "arduino " should be removed )
    using the serial lib only works for you / as you test it against your own arduino code?? /

  • in the second code you use the firmata lib,
    but not say that you uploaded firmata to arduino, so it can not work!

also when you tested my processing firmata example,
did you upload the standard firmata to arduino first?
like i show detailed in that many pictures blog extra for you?

Yes I did when I tested your code. Do I have to upload everytime?

and when you test your second processing code
( what also is a firmata code )
you changed arduino also from your sketch to firmata?

when your processing gives you a 0.0, did you measure voltage at that arduino pin?

//____________
anyhow i try to play with your "second code "
and yes, i got 0 too!!

now that was bad, as i just

  • loaded firmata

  • test my sketch and connect A0 to 5V and all ok

  • test your sketch and it did not work…
    so i play until i see something,
    it was the baud rate ! 57600 !
    see all arduino ( firmata ) examples have that rate and
    http://playground.arduino.cc/Interfacing/Processing
    here too, even suggest you can omit that parameter?


yes the first reading again was 0.
but then it worked fine.

and that again let me think why my code did not work in your setup?

  • did you do the jumper cable?
  • did you press the button?

Got it to work finally. Thanks for all your assistance and patience!