Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Cant work out a programming procedure

Status
Not open for further replies.

skmdmasud

Member
Hi..
I am making a timer clock with on and off timing setting using RTC DS1307 and Atmel atmega8. So far every thing work fine, i can read time, display it in 7 segment, store on and off time in RTC memory and recall it.

My problem is i cannot figure out how to match the ON and OFF time with current time and turn a relay on or off accordingly.

Lets say I have my current time in variable Min and Hr,
My on time in ONMin and ONHr and
off time in OFFMin and OFFHr.

The uC may loose power for several hour and when power is restored it should check with RTC and set its pin status accordingly.

What will be best approach to do the checking and set pin status.

Regards.
 
For simplicity of the discussion lets say the time is stored in as some type time.

The problem is that the clock rolls over from 23:59 to 00:00 and we do not know if on or off comes first.

The answer is to take the time at wake up and count backwards to find if the last thing that should have been done is a ON or OFF.

I would do this by writing a function that computes the diff between a past time and the current time.

Code:
time timeDiff(time curTime, time eventTime)
{
  if (cutTime >= eventTime)
  {
    return (curTime - eventTime);
  }
  else
  {
    return(24Hours - (eventTime-curTime));
  }
}

Call timeDiff ON and OFF to find which has the lowest value. Then set the pin to that state.

This code can not be directly used as we have not defined the type time and the if statment will not work with defined types.

HTH
 
For simplicity of the discussion lets say the time is stored in as some type time.

The problem is that the clock rolls over from 23:59 to 00:00 and we do not know if on or off comes first.

The answer is to take the time at wake up and count backwards to find if the last thing that should have been done is a ON or OFF.

I would do this by writing a function that computes the diff between a past time and the current time.

Code:
time timeDiff(time curTime, time eventTime)
{
  if (cutTime >= eventTime)
  {
    return (curTime - eventTime);
  }
  else
  {
    return(24Hours - (eventTime-curTime));
  }
}

Call timeDiff ON and OFF to find which has the lowest value. Then set the pin to that state.

This code can not be directly used as we have not defined the type time and the if statment will not work with defined types.

HTH

Hi 3v0
thanks for your method. All my values are in int format. do i have to convert my values to time format? in your function evenTime will be ontime first and then offtime and then we will have to see which one is smaller and set the pin accordingly.

lets say curTime = 5:00 'last status should have been ON because its been 6hr since ON'
onTime = 23:00
ofTime = 6:00

first we pass curtime, ontime
we get 24 - 18 = 6:00

then we pass curtime, offtime
we get 24 - 1 = 23:00

when we compare we get ontime to be smaller so it will be ON...Great wow its was like magic when i solved it...great solution.

THanks. I will try it out.

Regards..
 
The only function here is diffTime which returns the difference between two times which you pass in.

You can represent time anyway you like so long as it works and is consistent. Why not specify all time in minutes so 10:30 would be (10*60)+30 = 6030. It will make time manipulation a lot easier.

Code:
#define time unsigned int
 
The only function here is diffTime which returns the difference between two times which you pass in.

You can represent time anyway you like so long as it works and is consistent. Why not specify all time in minutes so 10:30 would be (10*60)+30 = 6030. It will make time manipulation a lot easier.

Code:
#define time unsigned int

Thanks, I will make the all time into min as you have explained.
Regards.
 
Hi 3v0
i had some free time today so i tried to work with my timer project. My programming skills are very basic, i couldn't make your code work, it gives error "conflicting types for timeDiff"
here is the code. i tried it with many different ways
Code:
relaystatus() //turns on off portC3
{
	unsigned int totalon = (onhr*60)+onmin; //adding hr and min
	unsigned int totaltime = (hr2*60)+min2;  // adding current time
	unsigned int totaloff = (offhr*60)+offmin; // adding off time
	
//	time totalon = (onhr*60)+onmin; //adding hr and min
//	time totaltime = (hr2*60)+min2;  // adding current time
//	time totaloff = (offhr*60)+offmin; // adding off time
	
	unsigned int jj,mm;//storing time difference
	
	jj = timeDiff(totaltime,totalon);  //passing ontime and storing it  // ERROR IN THIS LINE
	mm = timeDiff(totaltime,totaloff); //passing offtime and storing it // ERROR IN THIS LINE
	
	if (jj < mm) //turn relay on if on time is smaller
	{
		PORTC |= 1<<PINC3;
	}
	
	if(mm < jj)
	{
			PORTC &= ~(1<<PINC3); //turn relay off if off time is smaller
	}
} //end off relaystatus

time timeDiff(time curTime, time eventTime)  // time difference for relay function https://www.electro-tech-online.com/threads/cant-work-out-a-programming-procedure.131810/#post1096916 
{
	if (curTime >= eventTime)
	{
		return (curTime - eventTime);
	}
	else
	{
		return(24 - (eventTime - curTime));
	}
}
 
I think your Problem ist the variable defination.

General "C" Language knows global and local variables.
They could have the same names, but that are different variables.
C uses local variables before global variabes when defined both.

At variable defining i would never use blank characters.
Better is to use the underscore " time_curTime ".

You make 2 local variables with:
time timeDiff(time curTime, time eventTime)

but what kind of variable time curTime and time eventTime are?

The earlyer definations of this variables in Global area have no effect to these local variables.

So you have to define:
unsigned int timeDiff(unsigned int time curTime,unsigned int time eventTime)

The order of the programm parts is wrong too.

Functions that will be called by other functiond must be defined before.

You can do this by locate that in an earlyer part of programm code, or by defining an function Prototype in the header of the programm.
e.g.
unsigned int timeDiff(unsigned int time curTime,unsigned int time eventTime);
in the header of the programm.

Description:
The Compiler works from top to bottom.
When there is a function called, that is defined later into the programm code, the compiler doesn't know that and create an error message.
When defining a function prototype, the compiler knows that function and the error message will not appear.
 
Hi WKRUG
thanks for the great lesson. I didnt know you have to put functions before deceleration, after reordering my functions the code worked. I also made changes to some variable deceleration as per your advice.

Thanks I learned few important things.

Regards..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top