Hello again, Arduino forums. I'm still chipping away at the same project, using LEDs as sensors. My latest update to the code has me trying to create an average reading for 10 sensing LEDs. I've created a two-dimensional array for the sensors but I must have gotten something wrong here. All I get back from the serial monitor is zeroes. What am I doing wrong here?
Can you explain what the nature of the emergency is?
A quick look at your code:
Make an array of the sum.
Make the datatype unsigned int or unsigned long to prevent overflow
Why do you have integer AND float averages ?. One of them should be sufficient I guess
That would allow you to change:
for(int i=0; i<(numSensors-1); i++){ //for loop for the averaging sequence. i goes from 0 to 14 here - the number of columns in the array.
if(i==0){
for(int j=0; j<=9; j++){
sum1 += senseData[j][i];
avg1 = ((sum1)/10);
}}
.... /// many lines skipped for the reader
else if(i==14){
for(int j=0; j<10; j++){
sum15 += senseData[j][i];
avg15 = ((sum15)/10);
}}
}
into something better readable
There is a "bug" by the way, you calculate the average after every element, at the end of the inner loop is enough...
changed the index to type byte as that is big enough.
Can't see anything with IE7 - switching to Safari, back soon.
Edit: No, nothing on Safari. I'll see if I've got a copy of Chrome. Maybe it is a moderator view thing...
Nope, logged out and back in. Now there's a puzzle.
You are using quite some memory - are you using MEGA or UNO?
int LEDanodes[15][16] => byte LEDanodes[15][16] and you win 250 bytes, if they don't change -> make the array const byte
I don't know what this line does.... ..
float AverageArray[15] = {avg1, avg2, avg3, avg4, avg5, avg6, avg7, avg8, avg9, avg10, avg11, avg12, avg13, avg14, avg15};
Perhaps the cause of all zero's in your output..
try printing avg1 iso Serial.print(AverageArray[k],DEC);
Serial.begin(57600); //higher baud rate for fast communications and data transfer.
use 115200 for max speed with Arduino IDE, if you use another terminal program e.g. putty.exe you can even use 230400 or 345600 (I used them succesfully)
static int numSensors = 16; // the number of sensing LEDs - 16 is the maximum that can be used on the Mega.
should that not be 15???
That's a slick solution to the messy bit of code that I was using before to calculate the averages, robtillart. Despite this change, though, I'm still getting all zeroes as my readout. I just changed the data types of my array to unsigned int, and the averages and sums are now both unsigned ints also. This too has no change on my readout.
In greater detail, this code is designed to run a set of LEDs for an LED-based imaging device. One LED emits, while all other LEDs sense light. The "firing sequence" for the sensor LEDs referenced from all of the rows in a given column of the array "LEDCathodes", with the respective anodes of each LED in their own separate firing solution in "LEDanodes". Because of variations in the sensors (obnoxious), I take ten readings per emitter light. So, A0 might be the emitter LED, and A1-A15 (this is a Mega) are sensing. They run through the sensing process ten times, each time sending data to the serial monitor in the Arduino.
My previous implementation just spat out lots of numbers - it showed all of the readings for a given emitter. I'd like to average these readings and print them instead, because it makes the rest of the processing for the sensor readings far easier.
Thanks for your inputs so far - I apologize for any mix-ups with viewing the attached code to these posts. Hopefully we can figure this out.
More good suggestions! Thanks! The problem has to do with the array - it appears to be set entirely to zero. If I do a Serial.print(avg1) at the end of the code, it will print an output value without any problems. How, then, do I bring the new values avg1, avg2, etc. into AverageArray[15]? I figured that would be a good way to compactly represent the averages at the end of each trial, but I could be wrong.
Please note that the code is hardly readable due to the amount of comments;
Please add whitespaces, proper indentation (CTRL-T in the IDE) and empty lines to make it more readable
byte SenseRoutine (int emitter, int emitterAnode, int counter, byte LEDcathodes[][16], int column) //the variable 'column' here will step the program through the different sensing patterns in each column as the program is called
{
// WHERE IS COUNTER SET TO 10 ?? IT MUST BE SET EVERY CALL TO THIS FUNCTION SO HERE IS A GOOD PLACE
counter = 10;
// now it's time to use the same process outlined in the early test code - line the LEDs up and take measurements in sequence. Do it ten times.
while (counter > 0)
{
AWOL:
Can't see anything with IE7 - switching to Safari, back soon.
Edit: No, nothing on Safari. I'll see if I've got a copy of Chrome. Maybe it is a moderator view thing...
Nope, logged out and back in. Now there's a puzzle.
When I load in your original code and (try to) compile it I get messages like this:
BRDF_code_averaging_nonfunctional:21: error: 'A13' was not declared in this scope
BRDF_code_averaging_nonfunctional:21: error: 'A13' was not declared in this scope
...
And when I try to pretty it (Ctrl-T on PC) I get this:
What board settings are you using for the compile? If you've not selected a Mega (or any other board with a bunch of analog pins), then it will tell you that high-numbered analog pins are not declared. I was working with an Uno earlier and was getting similar messages; that's why I can actually answer that question.
As for auto-formatting... I don't know. My formatting is apparently a mess (I'm new to this) and I'm going to work on cleaning it up and attempting to actually make this code function as it was intended.
A0 is wired to the breadboard. Then we have a resistor, then LED cathode, and a wire from the LED anode to a digital pin.
This occurs for all 16 Analog pins and a variety of digital pins on the board. I have not had previous problems with reading from LED sensors - I've been using this same setup for weeks with fine results. Even with the crummy sensors I have in place now, I would not get flat readings of "zero" from it. When I use a similar program to print raw data, it does not flatline at zero.
kevind2010:
What board settings are you using for the compile? If you've not selected a Mega (or any other board with a bunch of analog pins), then it will tell you that high-numbered analog pins are not declared.
That did it - and the formatting issue went with the errors (actually, the formatter still couldn't handle the number of '}' - but it compiled).
I don't have a mega so I can't answer on the hardware, but I did notice a piece of code here that seemed odd:
I reformatted it to show the levels - what you are doing is grabbing 10 items, averaging them as you read them. I think you want to do this:
if(i==0)
{
sum1 = 0; // <<< init
for(int j=0; j<=9; j++)
{
sum1 += senseData[j][i];
}
avg1 = ((sum1)/10); // do this once
}
While the avg1 calculation is fine, doing it ten times inside the loop is unnecessary - once at the end is enough. And since you do this for sum2, sum3, etc, the time savings could add up significantly (division is very time consuming). As well, I've recommend setting sum1 to zero right here so you don't wonder what its value is (as I did).
That is a very good point! I guess I missed the fact that the averaging was occurring inside the loop. Good find, sir. Unfortunately my results have not changed significantly... I now get an output that looks like this:
750, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
I have one nonzero number! Almost there... I feel like I'm doing something wrong when I write the results to the array in the first place. I think I'm going to take a break from coding for a few hours and come back when I feel a little fresher.