Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sorting light values

fester14fester14 Posts: 3Member

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

// 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

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

");

// 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

");

// 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

");

// 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

");




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






I'm badly stumped on this.

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    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
  • fester14fester14 Posts: 3Member
    : 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



  • fester14fester14 Posts: 3Member
    : : 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?
  • stoberstober Posts: 9,765Member ✭✭✭
    : : : 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
    [code]
    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;
    }
    }
    }
    }
    [/code]
    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
Sign In or Register to comment.