Range within a range?

Hello I am programming a project for school and I have collected some data yesterday using photocells and the sun.

my problem is that the values that I get from the photocells overlap each other.

for example here is my code:

if ((average > 316 && (average < 386)))
{ Serial.println ("1 pm");
}
if ((average > 387 && (average < 488)))
{
Serial.println("2pm");
}

if ((average > 330 && (average < 375)))
{
Serial.println("1.30 PM");
}

the 1:30 PM values kind of go inside the 1 PM value range? I am trying to display this; however I am not able to get get the serial monitor to show the 1:30 PM even though I get the value of photocell in that range, It only shows 1 PM. PLEASE HELP!

Suppose your average comes in at 386. Then:

 if ((average > 316 && (average < 386)))              // False, won't go here
               { Serial.println ("1 pm");
               }
                if ((average > 387 && (average < 488)))   // False, won't go here either
               {
                 Serial.println("2pm");
               }
            
              if ((average > 330 && (average < 375)))     // False, won't go here either
             {
               Serial.println("1.30 PM");
             }
                                                                 // THerefore, it can never be 386

leogsd1:
my problem is that the values that I get from the photocells overlap each other.

First, please use code tags around your posted code. Second, please post entire, compilable code.

the 1:30 PM values kind of go inside the 1 PM value range? I am trying to display this; however I am not able to get get the serial monitor to show the 1:30 PM even though I get the value of photocell in that range, It only shows 1 PM. PLEASE HELP!

Hmm.. looks OK to me, but then I don't have the entire code, so what you posted has no context.

Here's your code, but wrapped in a for loop, and with a few added Serial.print statements.

void setup() {
  Serial.begin(115200);

  for (int(average) = 328; average < 390; average++) {
    Serial.print("Avg: ");
    Serial.print(average);
    Serial.print(" ");

    if ((average > 316 && (average < 386)))
    { 
      Serial.print ("1 pm ");
    }
    if ((average > 387 && (average < 488)))
    {
      Serial.print("2pm ");
    }

    if ((average > 330 && (average < 375)))
    {
      Serial.print("1.30 PM ");
    }
    
    Serial.println();
  }
}

void loop() {
}

If you start a new sketch, paste this into it, and run it, you'll see that it works just as you seem to want it.
Here's a bit of the output:

Avg: 328, 1 pm
Avg: 329, 1 pm
Avg: 330, 1 pm
Avg: 331, 1 pm 1.30 PM
Avg: 332, 1 pm 1.30 PM
...
Avg: 373, 1 pm 1.30 PM
Avg: 374, 1 pm 1.30 PM
Avg: 375, 1 pm
Avg: 376, 1 pm
Avg: 377, 1 pm
Avg: 378, 1 pm
Avg: 379, 1 pm
Avg: 380, 1 pm
Avg: 381, 1 pm
Avg: 382, 1 pm
Avg: 383, 1 pm
Avg: 384, 1 pm
Avg: 385, 1 pm
Avg: 386,
Avg: 387,
Avg: 388, 2pm
Avg: 389, 2pm

Note the output for averages of 386 and 387. You probably want to adjust your if conditions.

Nooo, what i posted is just an example, I realize that the average can never go to 386....but what I am having a problem with it is that, Lets say average for 1 PM falls somewhere between 100 and 300. the average for 2 pm falls between 125 and 175. So when I try to simulate this, even though i get the value of say 150, it is showing 1 PM, when i need it to show 2 PM. Get it? its tricky to explain.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int analogPin0 = 0;
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;
int analogPin4 = 4;

int sensVal;
int analogreading = 0;
int analogreading1 = 1;
int analogreading2 = 2;
int analogreading3 = 3;
int analogreading4 = 4;

void setup()

{

Serial.begin(9600);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("I Work, but no");
lcd.setCursor(0,1);
lcd.print("clouds please!");
delay(3000);
lcd.clear();

}

void loop ()

{

analogreading = analogRead(analogPin0);
Serial.print("Pin0=");
Serial.println(analogreading);

analogreading1 = analogRead(analogPin1);
Serial.print("Pin1=");
Serial.println(analogreading1);

analogreading2 = analogRead(analogPin2);
Serial.print("Pin2=");
Serial.println(analogreading2);

analogreading3 = analogRead(analogPin3);
Serial.print("Pin3=");
Serial.println(analogreading3);

analogreading4 = analogRead(analogPin4);
Serial.print("Pin4=");
Serial.println(analogreading4);

int list[] = { analogreading, analogreading1, analogreading2, analogreading3, analogreading4 };

for (int i = 0; i < 4; i++)
{
for (int j = i; j < 5; j++)
{
if (list[j] < list*)*

  • {*
  • int temp = list[j];*
    _ list[j] = list*;_
    _ list = temp;
    }
    }
    }*_

* float total = 0.0f;*
* int count = 0;*
* for (int k = 2; k<5; k++)*
* {*
* total += list[k];*
* count ++;*
* }*

* float average = (total/count);*
* Serial.print("avg=");*
* Serial.println(average);*
* // lcd.setCursor(0,0);*
* // lcd.print("Avg= ");*
* // lcd.setCursor(5,0);*
* // lcd.print(average);*
* //delay(1500);*
* //lcd.clear();*

* if ((average > 316 && (average < 386)))*
* { Serial.println ("1 pm");*
* }*
* if ((average > 387 && (average < 488)))*
* {*
* Serial.println("2pm");*
* }*

* if ((average > 330 && (average < 375)))*
* {*
* Serial.println("1.30 PM");*
* }*
* // lcd.setCursor(0,0);*
* // lcd.print("Avg= ");*
* // lcd.setCursor(5,0);*
* // lcd.print(average);*
* //delay(1500);*
* //lcd.clear();*

* delay(2000);*
* }*
[/quote]
here is the full code

I think that your fundamental assumption, that light level correlates to time, is flawed.

It could work if you were using the shadow of a gnomon and the photo cells were placed around it.

PaulS:
I think that your fundamental assumption, that light level correlates to time, is flawed.

I just put that 1 pm and 2 pm there as a test, I am not trying to correlate light with time, I am trying to map an average value of my photocells to an elevation of the sun, that is my problem but my problem is that the value i get a 10 AM over lap with the values i get at 2 PM, so how i do i display this seperately? like example i get 500 at 10 am which has an angle of say 50 then i get 500 again at 2 PM but an an angle at 80. get it?

leogsd1:

PaulS:
I think that your fundamental assumption, that light level correlates to time, is flawed.

I just put that 1 pm and 2 pm there as a test, I am not trying to correlate light with time, I am trying to map an average value of my photocells to an elevation of the sun, that is my problem but my problem is that the value i get a 10 AM over lap with the values i get at 2 PM, so how i do i display this seperately? like example i get 500 at 10 am which has an angle of say 50 then i get 500 again at 2 PM but an an angle at 80. get it?

  1. That's CODE tags, not QUOTE tags.
  2. That code will not compile. WTF is
      {
        int temp = list[j];
        list[j] = list;
        list = temp;
      }

lar3ry:

leogsd1:

PaulS:
I think that your fundamental assumption, that light level correlates to time, is flawed.

I just put that 1 pm and 2 pm there as a test, I am not trying to correlate light with time, I am trying to map an average value of my photocells to an elevation of the sun, that is my problem but my problem is that the value i get a 10 AM over lap with the values i get at 2 PM, so how i do i display this seperately? like example i get 500 at 10 am which has an angle of say 50 then i get 500 again at 2 PM but an an angle at 80. get it?

  1. That's CODE tags, not QUOTE tags.
  2. That code will not compile. WTF is
      {

int temp = list[j];
        list[j] = list;
        list = temp;
      }

code complies just fine. that is part of my sorting algorithm to move the top three photocell values to the top and average them

I am not trying to correlate light with time

Really?

my problem is that the value i get a 10 AM over lap with the values i get at 2 PM

Then you ARE trying to correlate time and light. And that is not a valid correlation.

That code will not compile. WTF is

That's what happens when you don't use code tags.
Note the italics.

What do you do on a partly cloudy day? Use a watch? how do you correct for seasonal variations? and the point that one set of va lues is a subset of another larger set of values... seems most contradictory on the surface....
Besides it's a most poor method. Perhaps you could start a timer triggered at the first occurrence of the "inside" set of values.
Might be easier to start a timer to detect first light and add a table of first light vs season Times for first light... that approach might have a chance of working... after a "fashion" you could use the timer to report the time of day... although an RTC just Might be the wisest choice... your approach seems similar to using a liter measure to accurately measure half liter quantities...

Doc

Docedison:
What do you do on a partly cloudy day? Use a watch? how do you correct for seasonal variations? and the point that one set of va lues is a subset of another larger set of values... seems most contradictory on the surface....
Besides it's a most poor method. Perhaps you could start a timer triggered at the first occurrence of the "inside" set of values.
Might be easier to start a timer to detect first light and add a table of first light vs season Times for first light... that approach might have a chance of working... after a "fashion" you could use the timer to report the time of day... although an RTC just Might be the wisest choice... your approach seems similar to using a liter measure to accurately measure half liter quantities...

Doc

The device wont function on a partly cloudy day, Look, I know its a flawed design. Lets just say that you would only use it on an ideal clear sky day.

it is a box placed with 5 photocells on each side, now depending on where the sun is in the sky, the top three photocells will be averaged giving me a value, I have different value resistors on the left and right photocell. so if the measurement is being done before 12 pm, i will get a different range of values, than what i would get after 12 pm, therefore, i would be able to use the numbers i get to map them to the azimuth and elevation of the sun. however, my problem is that i am getting an overlap of numbers. i understand that the design is flawed. however all i want it to do is distinguish the range of values when it is within a range. however, i dont thinks its possible. anyways. thank you for your help. And im new here. i couldnt fidn the code tags. sorry.

leogsd1:

  1. That's CODE tags, not QUOTE tags.
  2. That code will not compile. WTF is
      {

int temp = list[j];
       list[j] = list;
       list = temp;
     }

code complies just fine. that is part of my sorting algorithm to move the top three photocell values to the top and average them

It won't compile because you put it into quote tags, and I am not about to screw around with it to make it compile.
If you want help, read the stickies at the top of the forum and follow the guidelines.

leogsd1:
Nooo, what i posted is just an example, I realize that the average can never go to 386....but what I am having a problem with it is that, Lets say average for 1 PM falls somewhere between 100 and 300. the average for 2 pm falls between 125 and 175. So when I try to simulate this, even though i get the value of say 150, it is showing 1 PM, when i need it to show 2 PM. Get it? its tricky to explain.

The code I posted, using your code and some serial prints, does what you say you want. If you don't want that, tell us what you DO want. Show an example of the output you want, if you can't say it in words.