Anonymous Anonymous - 1 month ago
247 0

No description

C

35 2800

* Part1.c
 *
 * Created: 18/08/2017 2:14:36 PM
 * Author : s4394976
 */ 

#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include "light_ws2812.h"
#include <inttypes.h>
#include <compat/twi.h>
#include "i2cmaster.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>


#define MAXPIX 30
#define COLORLENGTH (MAXPIX/2)
#define FADE (256/COLORLENGTH)


#define Dev24C02  0xDE      // device address of EEPROM 24C02, see datasheet

void init_buzzer ( void );
void init_light_sensor( void );
void alarm_init( void );
void v_bat_init( void );
void init_usart( void );

void reset_seconds( void );
int set_hours( char hours_dec, char hours, char time_of_day, char input_error_check);
int set_minutes(char minutes_decade, char minutes);
void set_rtc_time( char input);

int set_alarm_hours( char hours_dec, char hours, char time_of_day, char input_error_check);
int set_alarm_minutes(char minutes_decade, char minutes);
void set_rtc_alarm(char input);

void display_weather(int weather );
void set_weather ( char input );

void communication_activity( void );
ISR (USART_RX_vect);
void print_to_putty(char* putty_print);

int read_light_sensor( void );

void set_leds( int light);

void get_time_rtc (void);

void splash_screen ( void );

void read_alarm( void );
void alarm_sound ( void );


struct cRGB colors[8];
struct cRGB colours[8];
struct cRGB led[28];


int gettime = 0;
int setalarm = 0;
int input_count = 0;
int brightness;
char time_input[7] = "\0";
int alarm_hour = 0;
int alarm_minutes = 0;
int hour_time = 0;
int minute_time = 0;
int getweather = 0;
int LED_ON_OFF[29];
int weather_status = 10;
int alarm_keeper[3];
int this_minute;
int last_minute;
int alarm_already_gone = 0;

void reset_seconds( void ) {
	int ret;
	ret = i2c_start(Dev24C02 + I2C_WRITE);       // set device address and write mode
	if (ret) {
		
	} else {
		i2c_write(0x00); // Seconds register
		i2c_write(0b10000000); // turn on the oscillator and set the seconds to zero
		i2c_stop();
	}
}

int set_hours( char hours_dec, char hours, char time_of_day, char input_error_check) {

	if (input_error_check != 'm') {
		return 0;
	}

	int writing_hours = 0;

	if (time_of_day == 'p') {
		writing_hours = 0b01100000;
	} else if (time_of_day == 'a') {
		writing_hours = 0b01000000;
	} else {
		return 0;
	}

	if (hours_dec == '1') {
		writing_hours = writing_hours | 0b00010000;
	} else if (hours_dec == '0' ) {
		writing_hours = writing_hours | 0b00000000;
	} else {
		return 0;
	}

	if ((hours <= 57) && (hours >= 48)) {
		writing_hours = writing_hours | (hours - '0');
	} else  {
		return 0;
	}

	int ret = i2c_start(Dev24C02 + I2C_WRITE);       // set device address and write mode

	if (ret) {
		i2c_stop();
		return 0;
	} else {
		i2c_write(0x02); // hours register
		i2c_write(writing_hours); // Set 
		i2c_stop();
	}
	return 1;
}

int set_minutes(char minutes_decade, char minutes) {
	int writing_minutes = 0;

	if ((minutes_decade <= 53) && (minutes_decade >= 48)) {
		writing_minutes = writing_minutes | (minutes_decade - '0');
	} else  {
		return 0;
	}

	writing_minutes = writing_minutes << 4;

	if ((minutes <= 57) && (minutes >= 48)) {
		writing_minutes = writing_minutes | (minutes - '0');
	} else  {
		return 0;
	}

	int ret = i2c_start(Dev24C02 + I2C_WRITE);       // set device address and write mode

	if (ret) {
		i2c_stop();
		return 0;
	} else {
		i2c_write(0x01); // hours register
		i2c_write(writing_minutes); // Set
		i2c_stop();
	}

	return 1;
}

void set_rtc_time( char input) {

	if (input == 'T') {
		
	} else {
		time_input[input_count] = input;
		input_count++;
		if (input_count == 7) {
			for(int i = 0; i<29; i++){
				LED_ON_OFF[i] = 0;
			}
			input_count = 0;
			reset_seconds();
			int error = set_hours(time_input[0], time_input[1], time_input[5], time_input[6]);
			int error2 = set_minutes(time_input[3], time_input[4]);
			if ((!error) || (!error2)) {
				UDR0 = 'F'; _delay_ms(10); UDR0 = 'A'; _delay_ms(10); UDR0 = 'I'; _delay_ms(10); UDR0 = 'L'; _delay_ms(10); UDR0 = '\n'; _delay_ms(10);
			}
			alarm_already_gone = 0;
			for (int i = 0; i < strlen(time_input); i++) {
				UDR0 = time_input[i];
				_delay_ms(10);
			}

			strcpy(time_input, "\0");
		}
	}
}

int set_alarm_hours( char hours_dec, char hours, char time_of_day, char input_error_check) {
	int hours_keep = 0;

	if (input_error_check != 'm') {
		return 0;
	}

	int writing_hours = 0;

	if (time_of_day == 'p') {
		alarm_keeper[2] = 1;
		writing_hours = 0b01100000;
	} else if (time_of_day == 'a') {
		alarm_keeper[2] = 0;
		writing_hours = 0b01000000;
	} else {
		return 0;
	}

	if (hours_dec == '1') {
		writing_hours = writing_hours | 0b00010000;
		hours_keep = 10;
	} else if (hours_dec == '0' ) {
		hours_keep = 0;
		writing_hours = writing_hours | 0b00000000;
	} else {
		return 0;
	}

	if ((hours <= 57) && (hours >= 48)) {
		writing_hours = writing_hours | (hours - '0');
		hours_keep += (hours - '0');
	} else  {
		return 0;
	}

	int ret = i2c_start(Dev24C02 + I2C_WRITE);       // set device address and write mode
	alarm_keeper[0] = hours_keep;
	if (ret) {
		i2c_stop();
		return 0;
	} else {
		i2c_write(0x13); // hours register
		i2c_write(writing_hours); // Set
		i2c_stop();
	}
	return 1;
}

int set_alarm_minutes(char minutes_decade, char minutes) {
	int writing_minutes = 0;
	int minutes_keep = 0;

	if ((minutes_decade <= 53) && (minutes_decade >= 48)) {
		writing_minutes = writing_minutes | (minutes_decade - '0');
		minutes_keep = (minutes_decade - '0') * 10;
	} else  {
		return 0;
	}

	writing_minutes = writing_minutes << 4;

	if ((minutes <= 57) && (minutes >= 48)) {
		writing_minutes = writing_minutes | (minutes - '0');
		minutes_keep = minutes_keep + (minutes - '0');
	} else  {
		return 0;
	}

	int ret = i2c_start(Dev24C02 + I2C_WRITE);       // set device address and write mode

	if (ret) {
		i2c_stop();
		return 0;
	} else {
		i2c_write(0x0B); // hours register
		i2c_write(writing_minutes); // Set
		i2c_stop();
	}
	alarm_keeper[1] = minutes_keep;
	return 1;
}

void print_to_putty(char* putty_print) {
	for (int i = 0; i<strlen(putty_print);i++) {
		UDR0 = putty_print[i];
		_delay_ms(10);
	}

}

void set_rtc_alarm(char input) {
	
	if (input == 'A') {
		int ret = i2c_start(Dev24C02 + I2C_WRITE); 
		if (ret) {
			i2c_stop();
		} else {
			i2c_write(0x07); // Control register
			i2c_write(0b00110000); // Enable Alarm 0 and 1
			i2c_stop();
		}
	} else {
		time_input[input_count] = input;
		input_count++;
		if (input_count == 7) {
			input_count = 0;
			int error = set_alarm_hours(time_input[0], time_input[1], time_input[5], time_input[6]);
			int error2 = set_alarm_minutes(time_input[3], time_input[4]);
			if ((!error) || (!error2)) {
				print_to_putty("FAIL");
			}
			alarm_already_gone = 0;
			for (int i = 0; i < strlen(time_input); i++) {
				UDR0 = time_input[i];
				_delay_ms(10);
			}

			strcpy(time_input, "\0");
		}
	}
}

void windy(int count) {
	LED_ON_OFF[12 - count] = 2;
	LED_ON_OFF[11 - count] = 0;
	for (int j = 12; j < 17; j++) {
		LED_ON_OFF[j] = 0;
	}
	if ((count == 12) || (count == 11)) {
		LED_ON_OFF[12] = 2;
		LED_ON_OFF[20] = 2;
		LED_ON_OFF[24] = 2;
		LED_ON_OFF[28] = 2;
	} else if ((count == 10) || (count == 9)) {
		LED_ON_OFF[13] = 2;
		LED_ON_OFF[20] = 2;
		LED_ON_OFF[24] = 2;
		LED_ON_OFF[28] = 2;
	} else if ((count == 8) || (count == 7) || (count == 6)) {
		LED_ON_OFF[14] = 2;
		LED_ON_OFF[19] = 2;
		LED_ON_OFF[23] = 2;
		LED_ON_OFF[27] = 2;
	} else if ((count == 5) || (count == 4)) {
		LED_ON_OFF[15] = 2;
		LED_ON_OFF[18] = 2;
		LED_ON_OFF[22] = 2;
		LED_ON_OFF[26] = 2;
	} else if ((count == 3) || (count == 2)) {
		LED_ON_OFF[16] = 2;
		LED_ON_OFF[17] = 2;
		LED_ON_OFF[21] = 2;
		LED_ON_OFF[25] = 2;
	}	
}

void cloudy( void ){
	LED_ON_OFF[0] = 4;
	LED_ON_OFF[1] = 4;
	LED_ON_OFF[2] = 4;
	LED_ON_OFF[12] = 4;
	LED_ON_OFF[11] = 4;
	LED_ON_OFF[10] = 4;
	LED_ON_OFF[9] = 4;
	LED_ON_OFF[8] = 4;
	LED_ON_OFF[16] = 4;
	for(int i = 17; i<25; i++){
		LED_ON_OFF[i] = 4;
	}
}


void cloud_rain( int count) {
	for(int i = 0; i<29; i++){
		LED_ON_OFF[i] = 0;
	}
	LED_ON_OFF[0] = 4;
	LED_ON_OFF[1] = 4;
	LED_ON_OFF[12] = 4;
	LED_ON_OFF[11] = 4;
	LED_ON_OFF[10] = 4;
	LED_ON_OFF[9] = 4;
	LED_ON_OFF[16] = 4;
	for(int i = 17; i<21; i++){
		LED_ON_OFF[i] = 4;
	}
	if (count%2) {
		LED_ON_OFF[13] = 2;
		LED_ON_OFF[15] = 2;
		LED_ON_OFF[22] = 2;
		LED_ON_OFF[24] = 2;
		LED_ON_OFF[25] = 2;
		LED_ON_OFF[27] = 2;
		LED_ON_OFF[7] = 2;
		LED_ON_OFF[3] = 2;
		LED_ON_OFF[5] = 2;
	} else {
		LED_ON_OFF[8] = 2;
		LED_ON_OFF[21] = 2;
		LED_ON_OFF[23] = 2;
		LED_ON_OFF[2] = 2;
		LED_ON_OFF[26] = 2;
		LED_ON_OFF[28] = 2;
		LED_ON_OFF[6] = 2;
		LED_ON_OFF[4] = 2;
		LED_ON_OFF[14] = 2;
	}
}

void storm(int count) {
	LED_ON_OFF[0] = 4;
	LED_ON_OFF[1] = 4;
	LED_ON_OFF[12] = 4;
	LED_ON_OFF[11] = 4;
	LED_ON_OFF[10] = 4;
	LED_ON_OFF[9] = 4;
	LED_ON_OFF[16] = 4;
	if (count%2) {
		LED_ON_OFF[17] = 7;
		LED_ON_OFF[21] = 7;
		LED_ON_OFF[22] = 7;
		LED_ON_OFF[26] = 7;
	} else {
		LED_ON_OFF[19] = 7;
		LED_ON_OFF[23] = 7;
		LED_ON_OFF[24] = 7;
		LED_ON_OFF[28] = 7;
	}
}
void raining_wind(int count) {
	if (count%2) {
		LED_ON_OFF[17] = 2;
		LED_ON_OFF[19] = 2;
		LED_ON_OFF[22] = 2;
		LED_ON_OFF[24] = 2;
		LED_ON_OFF[25] = 2;
		LED_ON_OFF[27] = 2;
	} else {
		LED_ON_OFF[18] = 2;
		LED_ON_OFF[20] = 2;
		LED_ON_OFF[21] = 2;
		LED_ON_OFF[23] = 2;
		LED_ON_OFF[26] = 2;
		LED_ON_OFF[28] = 2;
	}
}

void raining (int count) {
	if (count%2) {
		LED_ON_OFF[0] = 2;
		for(int i = 0; i<29; i++){
			if (!(i%2)) {
				LED_ON_OFF[i] = 2;
			}
		}
	} else {
		for(int i = 0; i<29; i++){
				LED_ON_OFF[i] = 2;
		}
		LED_ON_OFF[0] = 0;
		for(int i = 0; i<29; i++){
			if (!(i%2)) {
				LED_ON_OFF[i] = 0;
			}
		}
	}
}

void storm_wind(int count) {
	windy(count);
	for(int i = 17; i<21; i++){
		LED_ON_OFF[i] = 4;
	}
	for(int i = 21; i<29; i++){
		LED_ON_OFF[i] = 0;
	}
	if (count%2) {
		LED_ON_OFF[21] = 7;
		LED_ON_OFF[22] = 7;
		LED_ON_OFF[26] = 7;
	} else {
		LED_ON_OFF[23] = 7;
		LED_ON_OFF[24] = 7;
		LED_ON_OFF[28] = 7;
	}
}

void display_weather(int weather ) {
	int count = 12;
	switch(weather) {
		case 0:
			print_to_putty("SUNNY");
			for(int i = 0; i<29; i++){
				LED_ON_OFF[i] = 3;
			}
			brightness = read_light_sensor();
			set_leds(brightness);
			_delay_ms(3000);
			break;
		case 1:
			print_to_putty("SUNNY AND WINDY");
			while(count--) {
				windy(count);
				for(int i = 17; i<29; i++){
					LED_ON_OFF[i] = 3;
				}
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		case 2:
			print_to_putty("CLOUDY AND WINDY");
			while(count--) {
				windy(count);
				for(int i = 17; i<29; i++){
					LED_ON_OFF[i] = 4;
				}
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		case 3:
			print_to_putty("CLOUDY");
			for(int i = 0; i<29; i++){
				LED_ON_OFF[i] = 0;
			}
			cloudy();
			brightness = read_light_sensor();
			set_leds(brightness);
			_delay_ms(3000);
			break;
		case 4:
			print_to_putty("CLOUDY AND RAIN");
			while(count--) {
				cloud_rain(count);
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		case 5:
			print_to_putty("RAIN");
			while(count--) {
				for(int i = 0; i<29; i++){
					LED_ON_OFF[i] = 0;
				}
				raining(count);
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		case 6:
			print_to_putty("WINDY");
			while(count--) {
				for(int i = 0; i<29; i++){
					LED_ON_OFF[i] = 0;
				}
				windy(count);
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		case 7:
			print_to_putty("RAIN AND WINDY");
			while(count--) {
				windy(count);
				for(int i = 17; i<29; i++){
					LED_ON_OFF[i] = 0;
				}
				raining_wind(count);
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		case 8:
			print_to_putty("STORM");
			while(count--) {
				for(int i = 0; i<29; i++){
					LED_ON_OFF[i] = 0;
				}
				storm(count);
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		case 9:
			print_to_putty("STORM AND WINDY");
			while(count--) {
				storm_wind(count);
				brightness = read_light_sensor();
				set_leds(brightness);
				_delay_ms(250);
			}
			break;
		default:
			print_to_putty("FAIL");
			break;
	}
}

void set_weather ( char input ) {

	if (input == 'W') {
		
	} else if (input == '.') {
		getweather = 0;
	} else { 
		if  (input == '0')  {
			weather_status = 0;
		} else if (input == '1') {
			weather_status = 1;
		} else if  (input == '2') {
			weather_status = 2;
		} else if  (input == '3'){
			weather_status = 3;
		} else if  (input == '4') {
			weather_status = 4;
		} else if  (input == '5') {
			weather_status = 5;
		} else if  (input == '6') {
			weather_status = 6;
		} else if  (input == '7') {
			weather_status = 7;
		} else if  (input == '8') {
			weather_status = 8;
		} else if  (input == '9') {
			weather_status = 9;
		} else {
			weather_status = 10;
			print_to_putty("FAIL");
		}
	}
}

void communication_activity( void ) {
	get_time_rtc();
	LED_ON_OFF[26] = 4;
	LED_ON_OFF[27] = 4;
	LED_ON_OFF[28] = 4;
	brightness =  read_light_sensor();
	set_leds(brightness);
	_delay_ms(1000);
	get_time_rtc();
	LED_ON_OFF[26] = 0;
	LED_ON_OFF[27] = 0;
	LED_ON_OFF[28] = 0;
	brightness =  read_light_sensor();
	set_leds(brightness);
	_delay_ms(1);
}

ISR (USART_RX_vect) {
	// A char has been recieved -  we will read it. If it is lower case, we convet it and send back
	int input = UDR0;
	
	if ( input == '.') {
		gettime = 0;
		time_input[0] = '\0';
		getweather = 0;
		setalarm = 0;
		communication_activity();
	}
		
	else if ((input == 'T') || (gettime == 1)) {
		gettime = 1;
		getweather = 0;
		setalarm = 0;
		set_rtc_time(input);
	} 
	
	else if ((input == 'A') || (setalarm == 1)) {
		setalarm = 1;
		set_rtc_alarm(input);
	}
	
	else if ((input == 'W') || (getweather = 1)) {
		getweather = 1;
		set_weather(input);
	}

	else if (input == 'Z') {
		splash_screen();
	}
	
}

void init_buzzer ( void ) {
	DDRD |= (1 << 6); // Setting PortD Pin 6 Output
	TCCR0A |=  (1<<COM0A0) | (1<<WGM01); // Clear Timer on compare match. Toggle OCOA on Compare Match
	TCCR0B |= (1<<CS01); //
	OCR0A = 0; // Set to value, output pin will toggle depending on set value
}

void init_light_sensor( void ) {
	ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));    //Prescaler at 128 so we have an 125Khz clock source

	ADMUX |= (1<<REFS0); // Set to AVcc
	ADMUX |= (1<< MUX1) | (1 << MUX0);
	ADMUX |= (1 << ADLAR); //Left adjust
		
	ADCSRA |= (1<<ADEN); //Power up the ADC
	ADCSRA |= (1 << ADSC);
}

int read_light_sensor( void ) {
	ADCSRA |= (1 << ADSC);
	brightness = ADCH * 2;
	return brightness;
}

void init_usart( void ) {
	// Set baud rate under 1000
	UBRR0 = 499;
	
	// enable transmission and recieving via UART and also enable the recieve complete interrupt
	UCSR0B = ( 1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0);
	//No need to se UCSR0C -  we wjust want the defualt value
	
	// Enable Interrupts
	sei();
}

void set_leds( int light) {
	for(int i=0; i<29;i++){
		if (LED_ON_OFF[i] == 0) {
			led[i].r =0;led[i].g=0;led[i].b=0; //off
		} else if (LED_ON_OFF[i] == 1) {
			led[i].r = light;led[i].g=0;led[i].b=0; //red
		} else if (LED_ON_OFF[i] == 2) {
			led[i].r =0;led[i].g=0;led[i].b=light; //blue
		} else if (LED_ON_OFF[i] == 3) {
			led[i].r =light;led[i].g=light/3;led[i].b=0; // orange
		} else if (LED_ON_OFF[i] == 4) {
			led[i].r =light/3;led[i].g=light/3;led[i].b=light/3; //white
		} else if (LED_ON_OFF[i] == 5) {
			led[i].r =light/3;led[i].g=0;led[i].b= light; //violet
		} else if (LED_ON_OFF[i] == 6) {
			led[i].r =0;led[i].g=light;led[i].b=0; //green
		} else if (LED_ON_OFF[i] == 7) {
		led[i].r =light;led[i].g=light;led[i].b=0; // yellow
		}
	}

	ws2812_sendarray((uint8_t *)led,MAXPIX*4);
	_delay_ms(1);
}

void alarm_init( void ) {
	i2c_start_wait(Dev24C02+I2C_WRITE); 
	i2c_write(0x0D); // ALARM 0 
	i2c_write(0b00010000); // Set to minutes
	i2c_stop();
	i2c_start_wait(Dev24C02+I2C_WRITE);
	i2c_write(0x14); // Alarm 1
	i2c_write(0b00100000); // Set to hours
	i2c_stop();
}


void get_time_rtc (void) {
	for (int i = 0; i < 29; i++) {
		LED_ON_OFF[i] = 0;
	}
	int check_alarm = 0;
	int no_alarm = 0;
	int hours_alarm = 0;
	int time_of_day = 1;
	int yo, back;
	char buffer[9];
	i2c_start_wait(Dev24C02+I2C_WRITE);     // set device address and write mode
	i2c_write(0x02);                        // write address = 5
	i2c_rep_start(Dev24C02+I2C_READ);       // set device address and read mode
	yo = i2c_readNak();
	back = yo >> 4;
	time_of_day = yo >> 5;
	
	if((yo & 0b0100000) == 0) {
		buffer[5] = 'A';
		time_of_day = 1;
		if (alarm_keeper[2] == 0) {
			check_alarm = 1;
		} else {
			no_alarm = 1;
		}
	}else{
		time_of_day = 2;
		buffer[5] = 'P';
		if (alarm_keeper[2] == 1) {
			check_alarm = 1;
		} else {
			no_alarm = 1;
		}
	}
	buffer[6] = 'M';
	
	yo = yo & 0b00001111;
	back = back & 0b00000001;
	if (back == 1) {
		LED_ON_OFF[9 + yo] = time_of_day;
	} else {
		LED_ON_OFF[yo - 1] = time_of_day;
	}
	
	buffer[0] = '0' + back;
	buffer[1] = '0' + yo;

	hours_alarm = back + yo;
	if (alarm_keeper[0] == (back + yo)) {
		check_alarm = 1;
	} else {
		no_alarm = 1;
	}
	back = 0; 
	yo = 0;
	i2c_stop();
		
	buffer[2] = ':';
	
	int minute_keep = 0;

	i2c_start_wait(Dev24C02+I2C_WRITE);     // set device address and write mode
	i2c_write(0x01);                        // write address = 5
	i2c_rep_start(Dev24C02+I2C_READ);       // set device address and read mode
	yo = i2c_readNak();                    // read one byte
	back = yo >> 4;
	buffer[3] = '0' + (back & 0b00000111);
	back = back & 0b00000111;
	minute_keep += (back * 10);
	if (back == 0) {
	} else {
		LED_ON_OFF[11 + back] = time_of_day;
	}

	buffer[4] = '0' + (yo & 0b00001111);
	
	yo = yo & 0b00001111;
	minute_keep += yo;
	for (int i = 17; i < yo + 17; i++) {
		LED_ON_OFF[i] = time_of_day;
	}

	if (alarm_keeper[1] == minute_keep) {
		check_alarm = 1;
	} else {
		no_alarm = 1;
	}
	this_minute = minute_keep;
	if ((minute_keep == 0) && (hours_alarm == 0)) {
		no_alarm = 1;
	}
	if (((check_alarm == 1) && (no_alarm == 0)) && (alarm_already_gone == 0)) {
		alarm_sound();
	}
	buffer[7] = 9;
	buffer[8] = '\0';
	print_to_putty(buffer);
}

void splash_screen ( void ) {
	 int on = 1;
	 int count = 28;
	 while (count--) {
		for (int i = 17; i < 29; i++) {
			LED_ON_OFF[i] = on;
		}
		brightness = read_light_sensor();
		OCR0A = 70 + (on * 10);
		set_leds(brightness);
		_delay_ms(100);
		for (int i = 17; i < 29; i++) {
			LED_ON_OFF[i] = 0;
		}
		set_leds(255);
		for (int i = 0; i < 17; i++) {
			LED_ON_OFF[i] = on;
		}
		brightness = read_light_sensor();
		OCR0A = 70;
		set_leds(brightness);
		_delay_ms(100);
		for (int i = 0; i < 29; i++) {
			LED_ON_OFF[i] = 0;
		}
		set_leds(255);
		on = on%7;
		on++;
		if (on == 0) {
			on++;
		}
	}
	OCR0A = 0;
}


void read_alarm( void ) {
	alarm_already_gone = 0;
	char buffer[9];
	int yo, back;
	i2c_start_wait(Dev24C02+I2C_WRITE);     // set device address and write mode
	i2c_write(0x13);                        // write address = 5
	i2c_rep_start(Dev24C02+I2C_READ);       // set device address and read mode
	yo = i2c_readNak();
	back = yo >> 4;
	
	if((yo & 0b0100000) == 0) {
		buffer[5] = 'A';
		alarm_keeper[2] = 0; // Am
	} else {
		buffer[5] = 'P';
		alarm_keeper[2] = 1; // Pm
	}
	buffer[6] = 'M';

	yo = yo & 0b00001111;
	buffer[1] = '0' + yo;
	back = back & 0b00000001;
	buffer[0] = '0' + back;
	yo = yo + (back * 10);

	alarm_keeper[0] = yo;
	
	buffer[2] = ':';

	back = 0;
	yo = 0;
	i2c_stop();
	
	i2c_start_wait(Dev24C02+I2C_WRITE);     // set device address and write mode
	i2c_write(0x0B);                        // write address = 5
	i2c_rep_start(Dev24C02+I2C_READ);       // set device address and read mode
	yo = i2c_readNak();                    // read one byte
	back = yo >> 4;

	yo = yo & 0b00001111;
	buffer[4] = '0' + yo; 
	yo = yo + back;
	buffer[3] = '0' + back;
	alarm_keeper[1] = yo;
	buffer[7] = 9;
	buffer[8] = '\0';
	print_to_putty(buffer);
	i2c_stop();
	alarm_already_gone = 0;
}


void v_bat_init( void ) {
	i2c_start_wait(Dev24C02+I2C_WRITE);
	i2c_write(0x03); // ALARM 0
	i2c_write(0b00001000); // Set to minutes
	i2c_stop();
}


void alarm_sound ( void ) {
	int count = 26;
	int on = 1;
	while(count--) {
		for (int i = 0; i < 29; i++) {
			LED_ON_OFF[i] = on;
			//OCR0A = 50 + (i * 3);
			brightness = read_light_sensor();
			set_leds(brightness);
			_delay_ms(10);
		}
		on++;
		on = on%8;
	}
	alarm_already_gone = 1;
	OCR0A = 0;
}

int main(void) {
	for(int i = 0; i<29; i++){
		LED_ON_OFF[i] = 0;
	}
	weather_status =  10;
	brightness = 60;
	init_buzzer();
	init_light_sensor();
	init_usart(); 
	splash_screen();
	i2c_init();   
	alarm_init();                             // init I2C interface
	read_alarm();
	v_bat_init();
	for(int i = 0; i<29; i++){
		LED_ON_OFF[i] = 0;
	}
	set_leds(brightness);
	_delay_ms(10);
	get_time_rtc();
	last_minute = this_minute;
	while (1) {
		brightness =  read_light_sensor();
		get_time_rtc();
		set_leds(brightness);
		if (last_minute != this_minute) {
			brightness =  read_light_sensor();
			display_weather(weather_status);
			last_minute = this_minute;
		}
	}
}