Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.

Reset a timer to zero
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Reset a timer to zero

by prayag2010 on Tue Nov 01, 2011 4:48 pm

I am building a project for my car in which a character LCD ( will display the current temperature of the car using LM35 temp. sensor and we can set the current temperature and a servo will control the speed of the AC in the car (using an arduino). I want to take the code to a next level by programming the arduino in a way that if the temp. that we set is higher than the current temperature for more than five minutes, it will move the AC knob to level four(there are total four levels to set the power of the AC) and if it is lower for five minutes, the servo will turn it to level one. It will normally stay at level two or three. I can start the two times (refer the code) but I can't reset it back to zero after the servo moves to level one or four. Please help me since I am a complete newbie.......
Here is the code (I have used the serial communication function so that I can check on each step weather I am on the right track and I have a delay of 500 milliseconds for debouncing because such delay would not affect me or my project) -

[Edit - moderator - use code button when submitting code]
Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>
#include <LiquidCrystal.h>

Servo myservo;

const int buttonpin1 = 2;
const int buttonpin2 = 13;

int buttonstate1 = 0;
int buttonstate2 = 0;
int sensorPin = 0;
int val1;
int val2;
int buttonPresses1 = 0;
int buttonPresses2 = 0;

unsigned long time;
unsigned long mtime;

float setTemp = 27.0;
float lcdTemp = 27.0;
float currentTemp;

//LiquidCrystal lcd (12,11,5,4,3,2);

void setup()
  pinMode (buttonpin1, INPUT);
  pinMode (buttonpin2, INPUT);
  buttonstate1 = digitalRead(buttonpin1);
  buttonstate2 = digitalRead(buttonpin2);

void loop()
  int reading = analogRead(sensorPin);
  float voltage = reading*5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage*100);
  Serial.print("temperatureC "); Serial.println(temperatureC);
  currentTemp = (voltage*100);
  //Serial.print("currentTemp "); Serial.println(currentTemp); 
  //Serial.print("button 1 "); Serial.println(buttonstate1);
  //Serial.print("button 2 "); Serial.println(buttonstate2);
  val1 = digitalRead(buttonpin1);
  val2 = digitalRead(buttonpin2);
  if (val1 != buttonstate1)
    if (val1==LOW)
  buttonstate1 = val1;
  if (val2 != buttonstate2)
    if (val2 == LOW)
  buttonstate2 = val2;
  if (lcdTemp < currentTemp)
    time = millis();
  if (lcdTemp >=currentTemp)
    mtime = millis();
  if (time >= 5000)
  if (time < 5000)
    time == 0;
  if (mtime >= 5000)
  if (mtime < 5000)
    mtime == 0;
  Serial.print("Temperature is set to");


Please help me to reset the timer !!!
Posts: 22
Joined: Thu Apr 29, 2010 8:44 am

Re: Reset a timer to zero

by abqlewis on Wed Nov 02, 2011 3:35 am

OK, I've got a soft spot for people trying to create an actual cool little project, so here are a few hints:

1) Dude! .. don't just dump your code on a forum and basically say "Please debug my code!" If this were the AVR Freaks forum, they would roast you alive (and I'm only kinda kidding!)!
2) When you do include (a little bit of) code, use the code button at the top of the window, because DAMN this is some bad looking code. If you wrote your code with no indention at all, there may be no hope. And if you didn't, don't make others try to read it that way.
3) The "//" characters are NOT just for temporarily removing code. You should include more comments to let people know what you are doing, just in case you have to ask for help.
4) You need a some modularity in your code. Break it up in to more than just the loop procedure. I think myservo.write(170); turns the AC to max, but if you put it in a procedure and call it something Set_AC_MAX, we know what it means. And reading the buttons and the temp should all be different functions.

I state the above issues because you have to help others so they that help you.

What I do like - You made a pretty good first draft at a specification, and a good first hack at some code. I can understand your project and I think it's cool. There may be some logic problems, and it may not be a robust feedback system, but it's a cool starting experiment.

Now if you can accept 1-4 above as some constructive criticism, here is my input :

1) You specify "for more than 5 minutes" which tells me that an event occurs and then you need to time for 5 minutes. But you only compare the lcdtemp to currentTemp and keep setting time (or mtime) each time through the loop. You might store the current temp as the previous temp at the end of your processing loop and not only compare the current temp but the previous temp to determine when a transition (from lcdTemp > currentTemp to lcdTemp < currentTemp) has occurred. Only then do you record an absolute event time as a "start" for the timer. Also the lcdTemp could be modified (with the buttons) to a value that causes a transition, so you might want to save the previous lcdTemp, or just save the previous difference between the two. The key is to make an event to start a timer, and not just continually update the "time" variable if lcdTemp < currentTemp. This also means you don't need 2 timer values, just 1 timer value that starts at the last transition event.

2) You say 5 minutes, but i only see a comparison to what would be 5000 milliseconds of absolute time. This is just 5 seconds from the time the code started. You need to compare the DIFFERENCE from the event absolute time (time) to the current absolute time (millis()). When that difference is > 1000 * 60 * 5 milliseconds then change the setting to MAX or MIN, based on the sign of the difference. If you then set "time" to 0, and only check for 5 minutes when "time" is not 0, you effectively zero your timer. In your code you only set time to 0 when time (which is read from millis, which is the absolute number of milliseconds from power-on) is less than 5000 milliseconds. This only occurs in the first 5 seconds of power. That's why you never zero.

OK, that's my 2 cents. You can/should ask questions, but please try to narrow them down a little.


Posts: 71
Joined: Fri Dec 11, 2009 2:19 am

Please be positive and constructive with your questions and comments.