Analog read slow, or is my code no-good

Hi all

Bussy on IR play project and I am stuck.

The link to my posts in different section in forum: http://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB4QFjAAahUKEwiQ68vlqdnGAhVoIKYKHdr3Cf0&url=http%3A%2F%2Fforum.arduino.cc%2Findex.php%3Ftopic%3D333266.0&ei=jlKkVZDsDujAmAXa76foDw&usg=AFQjCNEdtCNGsNXRdTg84RSraXYh9KAfcw&sig2=wCnS2FadYAsFsoqaks_N4A&bvm=bv.97653015,d.dGY

This is the most recent vid of my project: http://youtu.be/dcpKH3gw_tY

This is my code for analog read , could it be made to run faster?
code is for reading 2x IR led&phototransistor sets and calculate the speed between thw two points.
Points 6 inch apart. displayed on lcd for m/s and ft/s.

[/#include <LiquidCrystal.h>;
LiquidCrystal lcd(12,11,5,4,3,2);
int firstsense =A4;
int secondsense =A5;
int firstsense_ =A3;
int secondsense_ =A2;
unsigned long time,time2,time3,time4;
float fps,elap,fps2,elap2;
int val;
int val2;
int val3;
int val4;
void setup()
{lcd.begin(16,2);
Serial.begin(9600);
pinMode (firstsense,INPUT);
pinMode (secondsense,INPUT);
Serial.println("Waiting for shot");
lcd.print("Awaiting shot ");
}
void loop()
{
// read start point phototransistor____ nothing there, keep reading sensor___but if something//
// there record clock. the commented serial reads are for trouble shooting.. see value and/or sensor//
//is working//

val = analogRead (firstsense);
//Serial.print("val  ");
//Serial.println(val);

if(val > 50) //need to run calibrate sktech to find value for sensor open
{val=analogRead (firstsense);}

else
{time= micros();}
//Serial.println(val);}
val2= analogRead (secondsense);// same as above for 2de point down path to cal speed//
//Serial.print("val2  ");
//Serial.println(val2);

if (val2 >500)
{analogRead (secondsense);}
else
{time2=micros();
//Serial.println (time2);}

elap = time2-time;
fps = 500000/elap;
if (fps > 2)
{Serial.print(fps);
Serial.println(" F/s2");
lcd.setCursor(0,1);
lcd.print(fps);
lcd.print("F/s");}
time=0;
time2=0;}

val3 = analogRead (firstsense_); // same as above for start point to cal speed//
//Serial.print("val3  ");
//Serial.println(val3);

if(val3 > 690) //need to run calibrate sktech to find value for sensor open
{val3=analogRead (firstsense_);}

else
{time3= micros();}
//Serial.println(val);}
val4= analogRead (secondsense_); // same as above for 2de point down path to cal speed//
//Serial.print("val3  ");


//Serial.print("val4  ");
//Serial.println(val4);

if (val4 >705)
{analogRead (secondsense_);}
else
{time4=micros();
//Serial.println (time2);}

elap2 = time4-time3;
fps2 = 152400/elap2;
if (fps2 > 2)
{Serial.print(fps2);
Serial.println(" M/s");
//lcd.clear();
lcd.setCursor(0,0);
lcd.print(fps2);
lcd.print("M/s");}
time3=0;
time4=0;}
}
code]

Ps vid on youtube is uploading really slow

Hi

I am working on a digital read for this project.....fumbling in the dark and I am getting this from my serial monitor " * EMRDY:1"

What on earth is * EMRDY:1

Tjaart: Hi

I am working on a digital read for this project.....fumbling in the dark and I am getting this from my serial monitor " * EMRDY:1"

What on earth is * EMRDY:1

quick google surgests that you have not selected the correct port. sounds like you are talking to modem.

gpop1: quick google surgests that you have not selected the correct port. sounds like you are talking to modem.

I have checked the port and cable. Arduino IDE normaly gives error message when uno not connected to the correct port. It still compiles the scetch and report upload complete..... but no tx flassing leds on board. Did started saying avrdude: stk500_getsync(): not in sync: resp=0x30 I am just about to start googling that

elap = time2-time;
fps = 500000/elap;
if (fps > 2)

Regardless of whether you got a good reading from either sensor, compute the speed of the (non-existent) moving object.

Why?

Please do yourself and us a favour, and use the auto-format tool in the IDE on your code.

Hi

PaulS: For some reason… I think during first sensor trigger but before second sensor input I end up with a negative “fps”, so I would get a garbage/funny readings before the expected result.

if (fps>2) solved that bug.

AWOL:

AWOL:
Please do yourself and us a favour, and use the auto-format tool in the IDE on your code.

I never claimed to be a good programmer and have no idea what “auto-format tool” is but would love to know. Yes I am going to Google it.

Hi

Forgot to mention; the * EMRDY:1 issue with the board.

IT turned out that when I have one uno board connected to a to set USB port of my laptop it works on COM3......and when I connect a different UNO board to the same USB port it works on COM10.

Just one board at a time connected to laptop.

Tjaart: AWOL: I never claimed to be a good programmer and have no idea what "auto-format tool" is but would love to know. Yes I am going to Google it.

In the Arduino IDE, look in the Tools menu.

[quote author=Nick Gammon link=msg=2316906 date=1436940015] In the Arduino IDE, look in the Tools menu. [/quote]

Thanks, appreciated.

PaulS: For some reason... I think during first sensor trigger but before second sensor input I end up with a negative "fps", so I would get a garbage/funny readings before the expected result.

Do you suppose that might be because you are dividing by 0?

My point was that you should only need to calculate speed after the second sensor is triggered.