Go Down

Topic: [Solved] simple Timer question (Read 405 times) previous topic - next topic

arduinomagbit

May 01, 2013, 03:51 pm Last Edit: May 02, 2013, 12:43 am by arduinomagbit Reason: 1
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: [Select]

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: [Select]

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: [Select]

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: [Select]

int timecount0= timer(1000,0);
if (timecount0 != timecount0) {
     sec++;
    }

Robin2

what about something like this (not tested)

Code: [Select]
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

arduinomagbit

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: [Select]
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

Go Up