Go Down

Topic: summing sensor readings (Read 1 time) previous topic - next topic


Nov 23, 2011, 02:29 am Last Edit: Nov 23, 2011, 03:40 am by andrewbox Reason: 1
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: [Select]
#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()
 Serial.begin(9600);          //  setup serial

void loop()

 Rsensor1 = analogRead(analogPinR);    // read the input pin for right sensor
 Rsensor2 = analogRead(analogPinR);
 //Rsensor3 = analogRead(analogPinR);
 Rsensf =  Rsensor2 + Rsensor1;
 Lsensor1 = analogRead(analogPinR);    // read the input pin for right sensor
 Lsensor2 = analogRead(analogPinR);
 //Lsensor3 = analogRead(analogPinR);
 Lsensf = Lsensor2 + Lsensor1;
 LsensM = Lsensf * 1.1;    //sets threshold
 RsensM = Rsensf * 1.1;   //sets threshold
 Serial.println(Rsensor1);  // print values
 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
 //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;


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

BTW, select your code and hit a button #


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 #"?


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


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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com



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?
The art of getting good answers lies in asking good questions.


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:


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.

Go Up