Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Sorting light values Posted by fester14 on 5 Jun 2007 at 8:12 AM

I am trying to obtain the median from 60 different light level. I want need help with sorting the light levels so I can select the middle value. I am a novice at the moment regarding C. My program for the data logger is below.

#include <avr/io.h>

// global variable difinitions
unsigned char seconds;
unsigned char minutes;
unsigned char hours;


// use a temp variable so we don't overwrite the hours variable
unsigned char temp;

void ADC_init (void)
{
// Configure the ADC
// ADC Voltage Reference: AVCC pin
ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(ADLAR);

// ADC Clock frequency: 250.000 kHz
ADCSRA = _BV(ADEN) | _BV(ADPS1);
}

unsigned char read_adc(unsigned char adc_input)
{
// Start and read an ADC result
// Configure the multiplexer chanel
ADMUX = (ADMUX & 0xF0) | adc_input;

// Start the AD conversion
ADCSRA |= _BV(ADSC);

// Wait for the AD conversion to complete
do {
asm("NOP");
} while ( !(ADCSRA & _BV(ADSC)));

// Reset the conversion flag
ADCSRA |= _BV(ADIF);

// return the answer
return ADCH;
}


void ms_delay(unsigned int msdelay)
{
unsigned int i;
unsigned char j;
for (j = msdelay; j > 0; j--)
for (i = 50; i > 0; i--)
asm("NOP");
}

void sec_delay(unsigned int sec_delay)
{
unsigned int i;
unsigned char j;
for (j = sec_delay; j > 0; j--)
for (i = 49999; i > 0; i--)
asm("NOP");
}

void USART_init(void)
{
// USART initialization
// status and control register of USART
// enable double speed communications
UCSRA = _BV(U2X);

// enable the USART for receive and transmit
UCSRB = _BV(RXEN) | _BV(TXEN);

// 8 bit data characters, 1 stop bit, no parity
UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);

// USART Baud rate: 9600
// BAUD values from the datasheet lookup table
UBRRH = 0x00;
UBRRL = 0x0C;
}

void USART_Transmit (unsigned char data)
{
// Wait for empty transmit buffer
do {
asm("NOP");
} while ( !( UCSRA & _BV(UDRE)));

// Put data into buffer to send the data
UDR = data;
}

void USART_string(char * str) {
// until the NULL byte at the end of the string
while (*str) {
// keep transmitting each character
USART_Transmit(*str++);
}
}

int main(void)
{
// declare the variables
unsigned int light;
// TOTAL declared
unsigned int TOTAL =0;
unsigned int min =255;
unsigned int max = 0;
unsigned int median = 0;
// configure the USART
USART_init();
// configure the ADC
ADC_init();
// PORTB output for LEDs
DDRB = 0xFF;


// read LDR every 10 seconds

do
{
sec_delay(1);
light = read_adc(PIN0);
PORTB = ~light;
TOTAL = TOTAL + light;

// logic for min & max values

if (light > max)
max = light;

if (light < min)
min = light;

// logic for median values




USART_string("TIME ");

// clock function
seconds=seconds+10;

if(seconds>=60)
{
seconds = 0;
minutes++;

if(minutes>=60)
{
minutes = 0;
hours++;
if (hours >= 24)
{
hours = 0;
}
}
}



// communication with USART
// transmit the tens of hours
USART_Transmit (hours / 10 + '0');
// strip off the tens of hours to leave the unirs of hours
temp = hours - (hours / 10) * 10;
USART_Transmit (temp + '0');
USART_string("hr/");

// transmit the minutes
USART_Transmit(minutes / 10 + '0');
// transmt thens of minutes to leave units of minutes
temp = minutes - (minutes / 10) * 10;
USART_Transmit (temp + '0');
USART_string("mins/");

// transmit the tens of the seconds
USART_Transmit (seconds / 10 + '0');
// strip off the tens of seconds to leave the units of seconds
temp = seconds - (seconds / 10) * 10;
USART_Transmit (temp + '0');
USART_string("secs LIGHT VALUE ");

// data logging light values
light = light * 100 / 51;
USART_Transmit (light / 100 + '0');
USART_Transmit ('.');
light = light - (light / 100) * 100;
USART_Transmit (light / 10 + '0');
light = light - (light / 10) * 10;
USART_Transmit (light + '0');

// transmit word ohmns
USART_string("ohms\n\r");
} while (minutes < 10);

// Obtaining mean values

USART_Transmit (' ');
USART_Transmit ('M');
USART_Transmit ('E');
USART_Transmit ('A');
USART_Transmit ('N');
USART_Transmit (' ');
TOTAL = TOTAL / 60;
TOTAL = TOTAL * 100 / 51;
USART_Transmit (TOTAL / 100 + '0');
USART_Transmit ('.');
TOTAL = TOTAL - (TOTAL / 100) * 100;
USART_Transmit (TOTAL / 10 + '0');
TOTAL = TOTAL - (TOTAL / 10) * 10;
USART_Transmit (TOTAL + '0');
USART_string("ohms\n\r");

// transmit min value

USART_Transmit ('m');
USART_Transmit ('i');
USART_Transmit ('n');
USART_Transmit (' ');
min = min * 100 / 51;
USART_Transmit (min / 100 + '0');
USART_Transmit ('.');
min = min - (min / 100) * 100;
USART_Transmit (min / 10 + '0');
min = min - (min / 10) * 10;
USART_Transmit (min + '0');
USART_string("ohms\n\r");

// transmit max value

USART_Transmit ('m');
USART_Transmit ('a');
USART_Transmit ('x');
USART_Transmit (' ');
max = max * 100 / 51;
USART_Transmit (max / 100 + '0');
USART_Transmit ('.');
max = max - (max / 100) * 100;
USART_Transmit (max / 10 + '0');
max = max - (max / 10) * 10;
USART_Transmit (max + '0');
USART_string("ohms\n\r");

// transmit median value

USART_string ("median");
median = median / 2;
median = median * 100 / 51;
USART_Transmit (median / 100 + '0');
USART_Transmit ('.');
median = median - (median / 100) * 100;
USART_Transmit (median / 10 + '0');
median = median - (median / 10) * 10;
USART_Transmit (median + '0');
USART_string("ohms\n\r");




// indefinite loop
while(1)
asm("NOP");
}






I'm badly stumped on this.
Report
Re: Sorting light values Posted by BitByBit_Thor on 5 Jun 2007 at 9:44 AM
I don't know how it's stored in your code, but mean = Sum / n
where Sum is the sum of all elements and n the amount of elements

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Sorting light values Posted by fester14 on 5 Jun 2007 at 10:29 AM
: I don't know how it's stored in your code, but mean = Sum / n
: where Sum is the sum of all elements and n the amount of elements
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

I'm looking to sort Total. I'm looking the median. The datalogger stores 60 readings in 10 minutes in total thats why I divided the total by 60 to get the mean. I am after the median though



Report
Re: Sorting light values Posted by fester14 on 6 Jun 2007 at 3:06 AM
: : I don't know how it's stored in your code, but mean = Sum / n
: : where Sum is the sum of all elements and n the amount of elements
: :
: : Best Regards,
: : Richard
: :
: : The way I see it... Well, it's all pretty blurry
:
: I'm looking to sort Total. I'm looking the median. The datalogger
: stores 60 readings in 10 minutes in total thats why I divided the
: total by 60 to get the mean. I am after the median though
:
:
:
:
I need help with this?
Report
Re: Sorting light values Posted by stober on 6 Jun 2007 at 4:10 AM
: : : I don't know how it's stored in your code, but mean = Sum / n
: : : where Sum is the sum of all elements and n the amount of elements
: : :
: : : Best Regards,
: : : Richard
: : :
: : : The way I see it... Well, it's all pretty blurry
: :
: : I'm looking to sort Total. I'm looking the median. The datalogger
: : stores 60 readings in 10 minutes in total thats why I divided the
: : total by 60 to get the mean. I am after the median though
: :
: :
: :
: :
: I need help with this?
:

There are lots of sort algorithms -- just google for "sort algorithms" and you will find them. The Bubble sort is the easiest to code but the slowest to run but should run ok with only the 60 integers in your code. Here is an example
void BubbleSort(int* array, int nItems)
{
   for(int i = 0; i < (nItems-1) ; ++i)
   {
       for(j = i+1; j < nItems; ++j)
       {
           if( array[i] > array[j])
           {
              // swap the values
              int temp = array[i];
              array[i] = array[j];
              array[j] = temp;
            }
         }
     }
}

=============================================
never lie -- the government doesn't like the competition. (Author unknown)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.