Hi,
I am trying to implement your code into mine. Here's my attempt.
Ardunio:
- int ledTX = 12; //Receives InfraRed signal
- int photodiodePin = 2; // Photodiode connected to digital pin 2
- int lastState;
- unsigned long startTime;
- unsigned long elapsedTime;
- void setup() {
- Serial.begin(9600);
- pinMode(ledTX, OUTPUT);
- pinMode(photodiodePin, INPUT); // sets the digital pin as input to read photodiode
- }
- void loop() {
- byte currentState = digitalRead(photodiodePin);
- digitalWrite(ledTX, HIGH); // turn the TX Infrared LED on
- if(currentState == LOW && lastState == HIGH){ //rising edge
- startTime = millis();
- }
- if(currentState == HIGH && lastState == LOW){ //sinking edge
- elapsedTime = millis() - startTime;
- Serial.println(elapsedTime);
- }
- lastState = currentState;
- }
Processing:
- import processing.serial.*;
- Serial port;
- int elapsedTime;
- float value;
- void setup() {
- size(200, 200);
- background(204);
- noStroke();
- port = new Serial(this, Serial.list()[0], 9600);
- }
- void draw() {
- /*if (0 < port.available()) {
- elapsedTime = port.read();
- }*/
- //println(elapsedTime);
- }
- void serialEvent(Serial p) {
- // get message till line break (ASCII > 13)
- String message = port.readStringUntil(13);
- if(message != null){
- value = float(message);
- println(value);
- }
- }
I can see the Arduino sketch is working and I am getting what seem to be correct value in the serial monitor.
In the Arduino sketch what does the 5 in Serial.println(elapsedTime, 5); mean? When I put it in the values of time from the photogate are too large to be correct.
There seems to be a problem with the Processing sketch line 9,
port = new Serial(this, Serial.list()[0], 9600);
Is giving an error,
Error inside Serial.<init>0
Thanks,
Shane
Fritzing of circuit
Photogate.fzz