Press Duration

Well, you may have seen my post in the Syntax & Programs section. This is the finished result that came out of that.

First off, I have to give a huge thanks to Kevin Darrah. He helped me create, modify, and debug this code. He kept pushing me when I was about to give up. Thanks Kevin!

The first one is just a button press duration timer. The second one is the same as the first one, but it also serial prints the output to the serial monitor. It also displays 0 milleseconds and 0 seconds when you let go of the button. When you press the button again, it will only print if the milleseconds is 1 or more, so you don't get repeat 0's.

int ledPin = 13;
int buttonPin = 12;

int firsttime = 1;
unsigned long startTime;
unsigned long pressTime;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}

void loop()
{
 if(digitalRead(buttonPin) == LOW){
  if(firsttime == 1){
   startTime = millis();
   firsttime=0;
  }
  pressTime = millis()- startTime;
 }
 else if(firsttime == 0){
  firsttime = 1;
 }
}
int ledPin = 13;
int buttonPin = 12;

int firsttime = 1;
unsigned long startTime;
unsigned long pressTime;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);

Serial.begin(9600);
}

void loop()
{
 if(digitalRead(buttonPin) == LOW){
  if(firsttime == 1){
   startTime = millis();
   firsttime=0;
  }
  pressTime = millis()- startTime;
  if(pressTime >= 1){
   Serial.print("Time: ");
   Serial.print(pressTime);
   Serial.print(" milliseconds ");
   Serial.print(int(pressTime/1000));
   Serial.println(" seconds");
  }
  if(pressTime>3000){
   digitalWrite(ledPin, HIGH); 
  }
 }
 else if(firsttime == 0){
  firsttime = 1;
  Serial.println("Time: 0 milleseconds; 0 seconds");
  digitalWrite(ledPin, LOW);
 }
}

No love for my new project?

A lack of posts does not mean a lack of love. Sometimes folks just don't have anything to say.

For what it's worth, I appreciate every post in this section. I love reading about other people's projects.

Yeah, I get you. It's just a little discouraging when nobody comments on my porjects... This isn't the first time either... Like 5 of my projects have gone competely unnoticed...

what are you using the button duration timer for?

just curious.

Curiosity strikes us all. I am using it to tell the difference between a press and a hold. That is to set the time on a binary clock that I am making. A press will encrese the time by 1 and a hold will increase the time steadily by 1 about every 1/2 or 1 second.

Yeah, I get you. It's just a little discouraging when nobody comments on my porjects... This isn't the first time either... Like 5 of my projects have gone competely unnoticed...

Not gone unnoticed, over 150 reads. However few comments. But then you didn't ask for comments or offer any questions. It's all in the marketing. ;)

Lefty

Here some clock code from my code junkyard. It takes into account the arduino clock rollover (once millis gets too high it rolls back over to 0.) It will print out the time that has passed to the serial monitor (seconds, minutes, hours,days). You can also hook up 2 LEDs that will blink out the number of minutes and hours.

/* Millis Rollover
 * 
 * Example of counting how many times the millis() function has rolled over
 *  by Rob Faludi http://www.faludi.com
 *
 * for the ATMEGA168 with Arduino 0010 the max value is 34359737 or about 9 hours and 32 mintues
 * for more information see: http://www.arduino.cc/en/Reference/Millis
 *
 */

// The LEDs on pins 13 and 10 will blink out the number of minutes that have passed and the number of hours.  

#define HourLed 13 // light for status output

int days = 0;
int halfday = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;

unsigned long then;  
  
  int counter = 0;
  int hourcounter = 0;

unsigned long secondtag = 0;
  unsigned long minutetag = 0;
  
  int on = 1;
  
void setup() {
  Serial.begin(9600); // start serial output
 pinMode (13, OUTPUT);
 pinMode (10, OUTPUT);
}


void loop() {
  int rollovers = millisRollover(); // get the number of rollovers so far

unsigned long present = millis(); // the time right now

if (present - then >= 1000){
Serial.print("Hours: ");
  Serial.println(hours);
Serial.print("Minutes: ");
  Serial.println(minutes);
  Serial.print("Seconds: ");
  Serial.println(seconds);
//Serial.print("Days: ");
//  Serial.println(days);




//  Serial.print("Rollovers: "); // show the number of rollovers so far
//  Serial.println(rollovers,DEC); // 
  Serial.println();
}





/* MAX millis = 34359736
Which is 9.5443711 hours.  
which is 9 hours and 32.662266 minutes 

9.5 hours is 32400000 milliseconds
*/

if (present - then >= 1000){
  seconds ++;
  then = present;
}


/*
THIS TAKES INTO ACCOUNT THE ROLLOVER, NEED TO ADJUST "THEN" FOR THE ROLLOVER DELAY
//How can 'Then' be larger than 'Now?'  Even though 'Now' has reset back to 0, "Then" will still be 
the previous value, like 34359701.  This will mess up our other if statements, so 
the 'then' variable needs to be reset so that it is less than 'now.'  
*/

// need to change then to a positive number?  

if (then > present){
 then = (then - 34359736) - 1; // 1 is the missing millisecond from the rollover delay????? just a guess

//the new then is a negative number, when this negative number is subtracted in the normal 'seconds' if statement
//it will be added and everything will work out fine.
}

if (seconds >= 60){
  minutes++;
  seconds = 0;
}

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

if (hours == 12){
 halfday++;
  hours = 0;
}

if (halfday == 2){
days++;
halfday = 0;
}


// this function blinks the an LED light as many times as requested
// --Rob Faludi http://rob.faludi.com
//

/*
  for (int i=0; i<hours; i++) {
    digitalWrite(13, HIGH);   // sets the LED on
    delay(2000);
    digitalWrite(13, LOW);    // sets the LED off
delay(2000);
  }

*/
  //---------------------------------------------
  //    BLINKING THE MINUTES
  
  if (counter <= minutes){  // blink every half second

   if (present - secondtag > 400){

if (on == 0){
  digitalWrite(13, LOW);
  on = 1;
} else if (on == 1){
      digitalWrite(13, HIGH);
      on = 0;
      counter++;
}
   
 secondtag = present;
 minutetag = present;

 }
   
}


  if (counter > minutes){  // wait 2 seconds
 digitalWrite(13, LOW);
 // Serial.println(counter);
if (present - minutetag > 2000){  
  counter = 0;  
  secondtag = 0;
   
}
}

   
//-------------------------------------------------------------------

//    BLINKING THE HOURS
  
  if (hourcounter <= hours){  // blink every half second

   if (present - secondtag > 400){

if (on == 0){
  digitalWrite(10, LOW);
  on = 1;
} else if (on == 1){
      digitalWrite(10, HIGH);
      on = 0;
      hourcounter++;
}
   
 secondtag = present;
 minutetag = present;

 }
   
}


  if (hourcounter > hours){  // wait 2 seconds
 digitalWrite(10, LOW);
 // Serial.println(counter);
if (present - minutetag > 2000){  
  hourcounter = 0;  
  secondtag = 0;
   
}
}













/*
//Days Array:
DayArray[6] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

Serial.println(DayArray[days]);


Months Array:
MonthArray[11] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}


Serial.println(MonthArray[months]);

*/

/*
// This will turn on 2 servos once every 24 hours
CONTINUOUS MOTOR CODING

if (hours == 0 && minutes == 1 && seconds < 3){ // < 3 works w/ servo?
  servo1control = 2035;
  servo2control = 4035;
  delay(3000);         // is the delay necessary?  check it
  if (seconds >= 3){    // is this necessary?  Check it.
   seconds = seconds - 3; 
  } else ...
}
*/




}

//----------------------------------------------------------------------------------
int millisRollover() {
  // get the current millis() value for how long the microcontroller has been running
  //
  // To avoid any possiblity of missing the rollover, we use a boolean toggle that gets flipped
  //   off any time during the first half of the total millis period and
  //   then on during the second half of the total millis period.
  // This would work even if the function were only run once every 4.5 hours, though typically,
  //   the function should be called as frequently as possible to capture the actual moment of rollover.
  // The rollover counter is good for over 35 years of runtime. --Rob Faludi http://rob.faludi.com
  //
  static int numRollovers=0; // variable that permanently holds the number of rollovers since startup
  static boolean readyToRoll = false; // tracks whether we've made it halfway to rollover
  unsigned long now = millis(); // the time right now
  unsigned long halfwayMillis = 17179868; // this is halfway to the max millis value (17179868)

  if (now > halfwayMillis) { // as long as the value is greater than halfway to the max
    readyToRoll = true; // you are ready to roll over
  }

  if (readyToRoll == true && now < halfwayMillis) {
    // if we've previously made it to halfway
    // and the current millis() value is now _less_ than the halfway mark
    // then we have rolled over
    numRollovers = numRollovers++; // add one to the count the number of rollovers
    readyToRoll = false; // we're no longer past halfway
  } 
  return numRollovers;
}

I will soon be adding a clock function to my current project. Maybe I will use your code.

Thanks :)

Cool! I always like to give people a boost on their projects.