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 » Multiple Sensors (Temp Sensor)

February 28, 2010
by Ralphxyz
Ralphxyz's Avatar

I have seen in other threads that people were trying to use multiple sensors. Has the final code been posted anywhere?

I need to have four temperature sensors.

There is a chunk of [code] (http://www.nerdkits.com/forum/thread/6/) that the intent does what I need but how do I implement it?

I am limited in C (I do know programming just not a lot of C).

I took the code chunk and appended it to the Temperature Sensor project, but do not know if it is actually working or how to clean it up.

What do I do with the sample code?

In the Temperature Sensor project an averaging loop is run wich seems like a good idea how would I average sensor2? Sensor one I can just use the Temperature Sensor code but what about sensor2.

Here is what I have done, this does compile but the LCD display is obviously messed up and I really do not know it is what it is actually showing, PC0 is input grom a LM35, PC1 is at ground:

        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;
                }

            // write message to LCD
            lcd_home();
            lcd_write_string(PSTR("ADC: "));
            lcd_write_int16(last_sample);
            lcd_write_string(PSTR(" of 1024   "));
            lcd_line_two();
            fprintf_P(&lcd_stream, PSTR("Temperature: %.2f"), temp_avg);
            lcd_write_data(0xdf);
            lcd_write_string(PSTR("F      "));

            //write message to serial port  
            printf_P(PSTR("%.2f degrees F\r\n"), temp_avg);
            lcd_write_string(PSTR(" "));

            //Two Sensors

            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);
                    lcd_write_string(PSTR(" "));    
                }
        }

return 0;

I am building a solar water heater and I need a differential temperature controller to turn a circulator on and off. If the array water temperature is 30 degrees >the tank temperature turn the circulator on else leave it off or turn it off.

sorry about the length of this post my head is sort of swimming around with so much code and such.

Thanks, Ralph

February 28, 2010
by BobaMosfet
BobaMosfet's Avatar

Set ADMUX to the channel you want to read, then reinit it. Other code is the same except how you interpret the result (based on what you've got inputting to the ADC).

BM

February 28, 2010
by Ralphxyz
Ralphxyz's Avatar

Thanks, I almost understand, I have seen some examples of setting the channel. Any samples or working projects?

Ralph

March 01, 2010
by Ralphxyz
Ralphxyz's Avatar

re: "Set ADMUX to the channel you want to read" isn't that what: ADMUX = 0 and ADMUX = 1 does?

Ralph

March 01, 2010
by pbfy0
pbfy0's Avatar

well, actually ADMUX = XXX works where XXX is is the pin you want to use.

October 12, 2010
by zrouse
zrouse's Avatar

So I was looking at the code above and I have been trying to use it to have 3 leds to light up on different pins.

I have 2 sensors, I want to use if then statements to do 3 different steps. First if sensor1 < sensor2

then turn on pinB1
     turn off pinB2
     turn off pinb3

Second if sensor1 > sensor2

then turn off pinB1
     turn on pinB2
     turn off pinb3

Third (else) sensor1 = sensor2

then turn off pinB1
 turn off pinB2
 turn on pinb3

I have tried, and looked at different forums to find my answer to program this code, I just don't know how to write what else to get this started

I know that you have to turn on the DDRB, then PORTB = PORTB | (1<<PBx) to have lights come on and PORT = PORTB & ~(PBx) for lights to go off

I just need an example of how to get started then I will be good to go

Thanks,

October 13, 2010
by bretm
bretm's Avatar

There are a few ways to do it. If you're not using PORTB for anything else, you can do

if (sensor1 < sensor2)
    PORTB = 1 << PB1;
else if (sensor1 > sensor2)
    PORTB = 1 << PB2;
else
    PORTB = 1 << PB3;

If you need to preserve the other PORTB pins for other uses, you could do something like

#define MASK ((1<<PB7)|(1<<PB6)|(1<<PB5)|(1<<PB4)|(1<<PB0))

if (sensor1 < sensor2)
    PORTB = (PORTB & MASK) | (1 << PB1);
else if (sensor1 > sensor2)
    PORTB = (PORTB & MASK) | (1 << PB2);
else
    PORTB = (PORTB & MASK) | (1 << PB3);
October 13, 2010
by zrouse
zrouse's Avatar

I just tried the code, with playing around with it a little I began to understand my mistakes and am 1 step further on my project.

Thanks Bretm

October 14, 2010
by Ralphxyz
Ralphxyz's Avatar

I posted my code for multiple temp sensors on the Pastebin site:

Ralph

Post a Reply

Please log in to post a reply.

Did you know that you can input numbers in binary via a DIP switch, and output them to the LCD? Learn more...