Pages: [1]   Go Down
Author Topic: summing sensor readings  (Read 621 times)
0 Members and 1 Guest are viewing this topic.
kansas city, MO
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I am working on my 1st photovore and it works if I just do an analogread from the left and right sensors, then compare the values of those variables to write to the outputs going to the servos.  But I wanted to see if I could improve low light performance by summing 2 or 3 consecutive reading together, defining them as a variable, then comparing those values (essentially as a way to average out noise).

But it's exhibiting strange behavior - when I look at the values of the summed sensor readings the left sensor doesn't seem to work, all the values are those coming from the right sensor.  It's not the hardware, going back to the simpler programs still works.  Here is my code, what am I doing wrong?  I should say I'm also new to C++ so it could be something about how I'm using variables.  Thanks!

Code:
#include <Servo.h>     // include servo library

int analogPinR = 0;    // right light sensor to analog pin 0
int analogPinL = 2;    // left  light sensor to analog pin 1              
Servo Rservo;          // define right wheel servo
Servo Lservo;          // define left wheel servo
char spacer = 'A';
char spacerb = 'B';
float Lsensf = 0;       // assign left sensor final variable
int Lsensor1 = 0;       // assign left light sensor variable
int Lsensor2 = 0;       // assign left light sensor variable
//int Lsensor3 = 0;       // assign left light sensor variable
float Rsensf = 0;       // assign right sensor final variable
int Rsensor1 = 0;       // right light sensor variable
int Rsensor2 = 0;       // right light sensor variable
//int Rsensor3 = 0;       // right light sensor variable
float RsensM = 0;       // summed sensor values after multiplying
float LsensM = 0;       //summed sensor values after multiplying

void setup()
{
  analogReference(EXTERNAL);
  Serial.begin(9600);          //  setup serial
  Rservo.attach(3);
  Lservo.attach(4);
  Rservo.write(90);
  Lservo.write(90);
}

void loop()
{


  Rsensor1 = analogRead(analogPinR);    // read the input pin for right sensor
  delay(20);
  Rsensor2 = analogRead(analogPinR);
  delay(20);
  //Rsensor3 = analogRead(analogPinR);
  //delay(30);
  
  Rsensf =  Rsensor2 + Rsensor1;
  
  Lsensor1 = analogRead(analogPinR);    // read the input pin for right sensor
  delay(20);
  Lsensor2 = analogRead(analogPinR);
  delay(20);
  //Lsensor3 = analogRead(analogPinR);
  //delay(30);
  
  Lsensf = Lsensor2 + Lsensor1;
  
  LsensM = Lsensf * 1.1;    //sets threshold
  
  RsensM = Rsensf * 1.1;   //sets threshold
  
  Serial.println(Rsensor1);  // print values
  Serial.println(Rsensor2);
  //Serial.println(Rsensor3);
  Serial.println(Rsensf);
  Serial.println(spacer);
  Serial.println(Lsensor1);
  Serial.println(Lsensor2);
  //Serial.println(Lsensor3);
  Serial.println(Lsensf);
  Serial.println(spacerb);
  Serial.println(spacerb);
  
  if (Rsensf > LsensM)
{
  //Rservo.write(90);  // right servo stop
  //Lservo.write(0);   //left servo forward
}
else if (Lsensf > RsensM)
{
  //Rservo.write(180);  // right servo forward
  //Lservo.write(90);   // left servo stop
}
else
{
  //Rservo.write(180);  // right servo forward
  //Lservo.write(0);    // left servo forward
}

float RsensM = 0;  //reset these to zero
float LsensM = 0;
float Lsensf = 0;      
float Rsensf = 0;
  
  delay(500);
  
}
« Last Edit: November 22, 2011, 09:40:29 pm by andrewbox » Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 27
Posts: 2573
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are reading the same pin for right and left:
Lsensor1 = analogRead(analogPinR);   

BTW, select your code and hit a button #
Logged

kansas city, MO
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ha!  I've been poring over it for a couple days looking for complicated explanations, it's always the simple things that get ya. 

What do you mean "select your code and press a button #"?

Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 27
Posts: 2573
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I mean there is a button # in the bar, it adds up code] /code] around your code in order to make it easier read / navigate and prevent distortion
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6143
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean "select your code and press a button #"?

First, edit your post. Then wrap your code in code tags, the "#" button in the editor's toolbar.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

kansas city, MO
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, got it.  Thanks.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All of you analogRead() statements reference the same pin number. It's not surprising, then, that all the readings appear to come from the same sensor. That's because they do.

Why are you adding two ints and storing the result in a float?
Logged

kansas city, MO
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ya, that was the problem.  It works fine now after changing one to read the left and the other the right sensor, I must have copied/pasted that part. 

As for the floats, I'm multiplying the summed sensor values by a decimal so I tried using floats for those variables when I was troubleshooting, but it's probably not necessary.  A rounded integer would probably work about the same. 

The averaging didn't help performance that much really, I think it's not so much a problem of noise but low light sensitivity.  The photodiodes I'm using are supposed to produce a current down to ~10 lux.  It works fine in bright light or shining a flashlight in front of it but not dim room lights.  I think I'll try better photodiodes, something like this:

http://www.thorlabs.com/thorProduct.cfm?partNumber=FDS100

Or, just for kicks I thought it might be cool to try and run photomultiplier tubes so it could drive around it VERY low light (e.g. moonlight coming in the window), of course you'd have to be careful not to cook them in high light conditions so probably not really worth the trouble.

This stuff is fun.
Logged

Pages: [1]   Go Up
Jump to: