First Program

Hello every one,

I need help setting variable called "hour" and "minute". that way I can refer back to the what value "hour" is for an if argument. the idea is when the clock reads a certain time it will start the if statements based on the current time. keep in mind this is my first program, and not be rude but I only want advice on the current issue, I don't want people telling me how I should rewrite my code. I am using the Arduino uno with AtMega 328p-pu IC.

#include <Time.h>

int led1 = 11;
int led2 = 10;
int led3 = 9;
int led4 = 6;
int hours, minutes, seconds;


void setup()
 {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  

  }

void loop()


 
{
time_t t= now();

  
while (exit == false)

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

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

if (minutes>9 && seconds>9) {
  cout << hours << ":" << minutes << ":" << seconds;}
    
if (minutes<10 && seconds<10) {
  cout << hours << ":0" << minutes << ":0" << seconds;}
  
if (minutes<10 && seconds>9) {
  cout << hours << ":0" << minutes << ":" << seconds;}

if (minutes>9 && seconds<10) {
  cout << hours << ":" << minutes << ":0" << seconds;}

sleep(1000)
seconds++;

if hours>=8 analogWrite(led1) {  
   for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=5)
   analogWrite(led1, fadeValue);
   delay(30);}
 
 if hours>=9 analogWrite(led2) {
   for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=5)
   analogWrite(led2, fadeValue);
   delay(30);}
 
 if hours>=1900 analogWrite(led3) {
   for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=5)
   analogWrite(led3, fadeValue);
   delay(30);}
 
 if hours>=2000 analogWrite(led4) {
   for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=5)
   analogWrite(led4, fadeValue);
   delay(30);}
 
 if hours<=8 && minutes<=15 analogWrite(led1) {
   for(int fadeValue = 255 ; fadeValue >=255; fadeValue -=5)
   analogWrite(led1, fadeValue);
   delay(30);}
 
 if hours<=1900 && minutes<=15 analogWrite(led2) {
   for(int fadeValue = 255 ; fadeValue >=255; fadeValue -=5)
   analogWrite(led1, fadeValue);
   delay(30);}
 
 if hours<= && minutes<=15 analogWrite(led3) {
   for(int fadeValue = 255 ; fadeValue >=255; fadeValue -=5)
   analogWrite(led1, fadeValue);
   delay(30);}
 
 if hours<=200 && minutes<=15 analogWrite(led4) {
   for(int fadeValue = 255 ; fadeValue >=255; fadeValue -=5)
   analogWrite(led1, fadeValue);
   delay(30);}

}
int hour();
int minute();
int seconds();

These aren't variables, these are function prototypes that take no arguments and return an int.

What hardware are using to keep track of the time, or do you plan on relying on the internal timers and never resetting the board?

I was going to add a small digital clock display and have two buttons to change the hour and minute. the IC I am using has a crystal oscillator so that and the user being able to change the time I think I am covered.
I need someway of keeping track of the number that is "hours" and "minutes" so that the

if hour>=8 analogWrite(led1) {
for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=5)
analogWrite(led1, fadeValue);
delay(30);}
function will fade on/off the LED

You can do it yourself, or you can just use a library:

http://playground.arduino.cc/Code/time

I'm not sure but it looks like that needs the computer time, this will be a stand alone system. I don't understand how to use it I tried the "time_t t = now();" and it gives me an error "time_t' was not declared in this scope".

I think i figured the " time_t t=now" I have to move the downloaded time.h to the library. but now I am having trouble with

if (second() > 59)
{
++minute, second = 0;
}

i need it to once 60 seconds has elapsed to reset seconds to 0 and increase minute by 1

Those are two separate actions, so put them on separate lines and use semicolons:

++minute;
second = 0;
if (second() > 59)
{
++minute, second = 0;
}

Also, is second a function or a variable? If a variable it should be:

if (second > 59)
  {
  minute++;
  second = 0;
  }

Must admit I'm not understanding what you're on about so this maybe wrong but ...

// ... somewhere in global space ...

uint8_t		seconds	= 0;	// range 0 - 255, 60 seconds in a minute
uint8_t		minutes	= 0;	// range 0 - 255, 60 minutes in an hour
uint8_t		hours	= 0;	// range 0 - 255, 24 hours in a day 
uint16_t	days	= 0;	// range 0 - 65535, (roughly) 365.25 days in a year


// ... somewhere in function, perhaps even 'loop', space ...

seconds++;
minutes += ((seconds %= 60) ? 0 : 1);
hours   += ((minutes %= 60) ? 0 : 1);
days    += ((hours   %= 24) ? 0 : 1);

Nick, that's how I first wrote it but the compiler wont take it.

if (seconds()>59){
minute++;
second = 0;
}
the red part is highlighted and I get the error "no post_increment operator for type"

This code fragment does the very same thing -

minutes += ((seconds %= 60) ? 0 : 1);

What type is 'minute'?

lloyddean, I tried your code and got "Ivalue required as left operand of assignment"

I wasn't sure if you meant

if (minute() ++ ((second() %= 60) ? 0 : 1);

and that gave me the same error

you have to declare the variables as int, not int function.

so:

int hours, minutes, seconds.

NOT

int hour();
int minute();
int second();

if (minute() ++ ((second() %= 60) ? 0 : 1);

You can't increment a function call.

Enough games. Post ALL of your code.

PaulS:
Enough games. Post ALL of your code.

Check OP. It's all there.

Check OP. It's all there.

What it started as is there. There have been changes, or OP is wasting our time.

PaulS:

Check OP. It's all there.

What it started as is there. There have been changes, or OP is wasting our time.

But noone has spelled out the changes required to the variables, which is clearly what is causing the problems, still. People mentioned it, but this one needs spoon feeding.

PaulS:
Enough games. Post ALL of your code.

I put my whole code in the first post, and i figured it would be easier if I only quoted the section of code I need help with.

PaulS:

if (minute() ++ ((second() %= 60) ? 0 : 1);

You can't increment a function call.

is it still an if argument?

aarondc:

PaulS:

Check OP. It's all there.

What it started as is there. There have been changes, or OP is wasting our time.

But noone has spelled out the changes required to the variables, which is clearly what is causing the problems, still. People mentioned it, but this one needs spoon feeding.

I'm sorry I'm not good with writing code, we all have to start somewhere. c++ is new to me and I don't understand it. if I am too much of a pain to help I can got find a more welcoming forum

joshp689:

But noone has spelled out the changes required to the variables, which is clearly what is causing the problems, still. People mentioned it, but this one needs spoon feeding.

I'm sorry I'm not good with writing code, we all have to start somewhere. c++ is new to me and I don't understand it. if I am too much of a pain to help I can got find a more welcoming forum

I posted the change you need to make to get it to work. Did you see that post?

Arrch:

int hour();

int minute();
int seconds();




These aren't variables, these are function prototypes that take no arguments and return an int.

aarondc:
you have to declare the variables as int, not int function.

so:

int hours, minutes, seconds.

NOT

int hour();
int minute();
int second();

This is the solution to your "lvalue required ..." compiler errors.