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.