NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Multi sensors part two the code

April 01, 2010
by Ralphxyz
Ralphxyz's Avatar

I got the cabling thought out and made up (still getting a 10 degree higher

reading on 50' Cat5 but I'll try the R-C Damper.

Now I need help with the coding (really really need help).

I am just learning C and microprocessor programing. I have done some VB and C#

but mostly web page stuff.

in other words I do not know what I am dong I using cut and paste from working projects.

I added the code sample from

/thread/6 to the tempsensor project. Essentially I just commented out the

while(1) and subsituted the sample code:

/*
while(1) {
// take 100 samples and average them!
temp_avg = 0.0;
for(i=0; i<100; i++) {
  last_sample = adc_read();
  this_temp = sampleToFahrenheit(last_sample);

  // add this contribution to the average
  temp_avg = temp_avg + this_temp/100.0;
}

*/

uint16_t sensor1, sensor2;
int16_t difference;
while(1) {
// read sensor 0 (on pin PC0)
ADMUX=0;
sensor1 = adc_read();

// read sensor 1 (on pin PC1)
ADMUX=1;
sensor2 = adc_read();

difference = sensor1 - sensor2;
lcd_home();
fprintf_P(&lcd_stream, PSTR("A = %d, B = %d"), sensor1, sensor2);
lcd_line_two();
fprintf_P(&lcd_stream, PSTR("A-B = %d"), difference);
}

Here is the error I get: Multisensor

The entire code is here

Thanks for the help I need it. Ralph

April 01, 2010
by bretm
bretm's Avatar

You have a closing curly brace right before "return 0" at the end. This closes off the main() function, so it's complaining that the "return" is in the wrong place. You should just be able to delete it.

Also, adc_read actually returns the value of the previous conversion and then starts the next conversion. So your sensor1 value is from ADMUX=1 and your sensor2 value is from ADMUX=0, except for the first time through the loop. That may not be a problem as long as you're aware of the inconsistency.

April 01, 2010
by Ralphxyz
Ralphxyz's Avatar

duh, I suspected it was that but I specifically traced the curly back and thought that it was correct. I should have just deleted it and seen what would have happened.

Thank you, so much, this will really help me move on my project which is gong to be needed soon. No problem with the previous conversion I will allow for it and will be taking action only on a 15 minute delay.

This is great, ta dah

Ralph

April 02, 2010
by Ralphxyz
Ralphxyz's Avatar

I have working code BUT the readings are not what I am expecting.

I tried using the sampleToFahrenheit code from the tempsensor project but that

gives me 00 or a blank screen. So what do I need to do?

I tried using the averaging routine:

   for(i=0; i<1000; i++) {
  last_sample = adc_read();
  this_temp = sampleToFahrenheit(last_sample);

  // add this contribution to the average
  temp_avg = temp_avg + this_temp/100.0;

But that also gave me a blank screen.

With PC0 at V+ and PC1 at GRN I get A = 153 B = 00.

B is correct but A should be 492.

Yes, I barely know what I am doing so I do appreciate the help.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that first-order systems have a exponentially decaying response to step inputs? Learn more...