I start by saying that 'm using both Arduino and Processing and I have a problem on how one communicate to the other.
I have 4 photoresistance, linked to the ADC so every resistance gives a voltage value to arduino. I want use those values in Processing, sending them by serial cable. Since each of the 4 values must be read once in a loop of processing, i thought to send them as an array of bits.
My idea was:
Start of loop (or draw in processing) - Arduino read first value - Send the value - Processing read the value, and store it - Then again Arduino read second value and so on till the fourth value - Processing manipulate those 4 values and make something on the screen - NOW the loop will restart.
I very quickly bump into some problems:
Arduino sketch (I think it is right)
int array[4];
int i;
void setup(){
Serial.begin(9600);
}
void loop(){
for(i=0;i<4;i++){
array[i]=analogRead(i);
}
Serial.write(array,sizeof(array));
}
But with processing the sketch isn't even compiled.. =( I can't make Processing read the four values during the loop then restart the loop. Should I save the values in an array?
While I was writing this answer you replied three times, but I post the answer as well adding this:
You'll need some sort of program listening on the serial port to be able to process the 4 values.
Do you mean that I have to write down a new function that allow me to read four values?
Previous answer
Yes sorry I made a writing error. Sure each photoresistance is attached to a Pin.
My very problem is I can't understand how to write a code that do what I want. I don't need the compiled code, i just need an algorithm like "create an array, now when arduino read the values it stores them in the array whitch is read by Processing"..
I dind't found a detailed explanation on how the functions serial.write (on arduino) and .read() (on Processing) work.. I checked on the /Reference page..
The code of Processing is this
import processing.serial.*;
Serial myPort;
void setup(){
size(400,500);
background(255,0,125);
println("Available serial ports: ");
println(Serial.list());
int[] values = new int[4];
myPort = new Serial(this,Serial.list()[0],9600);
}
void draw(){
if(myPort.available() > 0){
for( int i = 0;i <4; i++){
values[i] = myPort.read();
println(values[i]);
}
}
}
When I try to compile it it say "cannot find anything named values". Maybe I can't handle arrays, but this is another problem, for now I just want to know if my Idea is possible.
Last few words.. Do you think I should rename the post including this is also a Processing problem?
When I try to compile it it say "cannot find anything named values".
Here, in draw():
if(myPort.available() > 0){
for( int i = 0;i <4; i++){
values[i] = myPort.read();
println(values[i]);
}
}
the compiler can not see the array called values, defined in setup():
void setup(){
size(400,500);
background(255,0,125);
println("Available serial ports: ");
println(Serial.list());
int[] values = new int[4];
because the array has gone out of scope.
If values were a global array...
But, the Arduino wrote 8 bytes to the serial port, and you are reading 4 bytes. That is never going to work.
You could:
write 4 bytes (by dividing the analog reading by 4 and storing it in a byte array) and read 4 bytes
write 8 bytes and read all 8 bytes and reconstruct the integers in Processing (multiply the first byte by 256 and add the second byte to create the first int. Repeat reading for the next two bytes to create the second int)
print() the values, with delimiters and start and end of packet markers, and read the string and parse it, to reconstruct the 4 integers.
I’ve understand what you explain, and I rewrite both sketches:, but it doesn't work. I don't know if it is due to the too little variation of the voltage when the photoresistance are obscured or there is something wrong when arduino gives values to Processing.
Arduino:
byte array[4]; \\I need to save the Values of the ADC in an array of bytes
\\every byte is 4 bit and can represent a number from 0 to 255
int i; \\the counting variable
void setup(){
Serial.begin(9600); \\start the serial comunication
}
void loop(){
for(i=0;i<4;i++){
array[i]=analogRead(i)/4; \\I read every number from the ADC. Divide per 4
} \\so each value is a byte
Serial.write(array,sizeof(array)); \\send the array by serial bus
Processing
import processing.serial.*;
Serial myPort; \\create the istance of the object myPort
void setup(){
size(400,500);
background(255,0,125); \\just a few comands to try
println("Available serial ports: ");
println(Serial.list());
myPort = new Serial(this,Serial.list()[0],9600); //create the object my port
}
void draw(){
int[] values = new int[4]; \\create an array whitch will store the values
if(myPort.available() > 0){ \\sent from arduino
for( int i = 0;i <4; i++){
values[i] = myPort.read(); \\the core part: save the values from the serial bus
print("number");println(i); \\that's just for an ordinate graphic
println(values[i]); \\print values on the screen to see if they are correct
}
println("end array");
}
}
I don't know if it is due to the too little variation of the voltage when the photoresistance are obscured or there is something wrong when arduino gives values to Processing.
Since we can't see your output from Processing, we can't even hazard a guess.
Change write() to print(), in a loop, and use the Serial Monitor application to see what the Arduino is sending. If that looks reasonable, then you can work on the Processing end. If it doesn't, then it is likely a hardware issue. You have not explained how the photo-resistors are connected to the Arduino. Are there other resistors in series with them? What is the resistance of your photo-resistor in the dark? In full light? What is the value of the in-series resistor?
The voltage now is 5V because I linked them to Arduino (previously was a battery)
Where R are resistance of 10kOhm
R in a circle are the Photoresistance
Ax are the analog Pin of Arduino Board
The link of the datasheet of the photoresistance (it is the one that was in the Arduino starter kit I bought)
The screenshot of what Arduino read(In a mid operating condition, not too dark, not too bright)
Aaand some calc:
ADC 1024 bit. So if Arduino read 500 it mean that the voltage is (5V/1024)500 = 2,44 V (correct because I prove it with a tester)
The calc for the R of Photoresistance is 2.44V =9V Rx(variable)/(Rx+10kOhm) Rx = 3.7kOhm
Values are accetable, in a too dark condition they don't "sbam" at 1024.
The circuit seems working, so I'm going to focus on the code =(