Pages: [1]   Go Down
Author Topic: [Solved] simple Timer question  (Read 339 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

while timer(1000,0) gives a high or a low for every second. I just want to use this function to add 1 to a sec variable. I have tried simpler code to do that, but it doesn't work. This long and stupid code is what I have figured out that works. So my question is : how can I use the return of "timer(1000,0) " to add 1 to sec in a smarter and simpler way?

Code:
void loop (){        //Loop =============================
  
int timecount0= timer(1000,0);
lcd.setCursor(0,1);
lcd.print (timecount0);

if (timecount0 ==HIGH) {
  if(timecount0 != pstimecount0)  sec++;
}

if (timecount0 ==LOW) {
  if(timecount0 != pstimecount0)  sec++;
}
   pstimecount0=timecount0;
  
lcd.setCursor(3,1);
lcd.print (sec);



}                   //Loop =============================



int timer (int sec, int index){  // timer funcion ===================
// setting coretimer    

 unsigned long coretime[]= {0,0,0,0,0,0,0,0,0,0};
 coretime[index]= millis();
 if(coretime[index]-pscoretime[index] > sec){
   pscoretime[index]  = coretime[index];
   timecount[index] = !timecount[index];
 }  
 
 return timecount[index];
}                               // timer funcion ===================

Solved=============================================
figured this out after reading state change detection example once again.
Code:
int timecount0= timer(1000,0);
if (timecount0 != pstimecount0) {
  if (timecount0 ==HIGH) {
     sec++;}
  else sec++;
    }
  pstimecount0=timecount0;

refined to this after a little bit thinking.

Code:
int timecount1= timer(500,1);
if (timecount1 != timecount1) {
  if (timecount1 ==HIGH) {
     sec++;}
    }


================

This is my first try. it just keep adding 1 to sec rapidly.
Code:
int timecount0= timer(1000,0);
if (timecount0 != timecount0) {
     sec++;
    }
« Last Edit: May 01, 2013, 05:43:19 pm by arduinomagbit » Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 6119
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what about something like this (not tested)

Code:
void setup() {
   unsigned long lastMillis = millis();
   unsigned long curMillis = lastMillis;
   int secs = 0;
   // other setup stuff
}

void loop() {
   curMillis = millis();
   if (curMillis - lastMillis) > 1000 {
   secs ++;
   lastMillis = curMillis;
   
  // other loop stuff
   }
}

...R
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 102
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is kind same as my timer function. it should work, but I want to learn how to use a signal to add 1 to sec instead of this.

what about something like this (not tested)

Code:
void setup() {
   unsigned long lastMillis = millis();
   unsigned long curMillis = lastMillis;
   int secs = 0;
   // other setup stuff
}

void loop() {
   curMillis = millis();
   if (curMillis - lastMillis) > 1000 {
   secs ++;
   lastMillis = curMillis;
   
  // other loop stuff
   }
}

...R
Logged

Pages: [1]   Go Up
Jump to: