[Solved] Time elapsed since event triggered


i having trouble measuring amount of time has passed since event has occurred.  i thought understood how use millis() function measure elapsed time, not.

i trying measure amount of time has passed since voltage value strain gauge greater "activationvoltage" (which determined using potentiometer). if elapsed time greater specified interval value, want event occur.

please @ code below , let me know doing wrong. thank you.

code: [select]
// transmitter code

#include <virtualwire.h>

int elapsedtime;  
int interval = 1000;
unsigned long previousmillis;

void setup()
{
   serial.begin(9600);  // debugging only
   serial.println("setup");

   // initialise io , isr
   vw_set_ptt_inverted(true); // required dr3100
   vw_setup(2000); // bits per sec
   vw_set_tx_pin(3);
       
   pinmode(8, input);
  }

void loop()
{
 char *msg;
 int straingauge = analogread(a1); //read input strain gauge
 int potvalue = analogread(a0); //read input potentiometer
   
 //convert analog reading (which goes 0 - 1023) voltage (0 - 5v):
 float activationvoltage = potvalue * (5.0 / 1023.0);
 float activationweight = (1.8745*activationvoltage)-0.0744;
 
 //print out activation weight value:
 //serial.print("activation weight: ");
 //serial.print(activationweight);
 //serial.println("lbs");
 
 //convert analog reading (which goes 0 - 1023) voltage (0 - 5v):
 float voltage = straingauge * (5.0 / 1023.0);
   
 //convert voltage weight
 float weight = (1.8745*voltage)-0.0744;
 
 //print out actual weight value:
 //serial.print("actual weight: ");
 //serial.print(weight);
 //serial.println(" lbs");
 //serial.println();

 unsigned long currentmillis = millis();

 if(voltage >= activationvoltage){
   
   previousmillis = currentmillis;
   char *msg = "1";
   vw_send((uint8_t *)msg, strlen(msg));
   vw_wait_tx(); // wait until whole message gone
   
   serial.println(currentmillis);
   serial.println(previousmillis);
   serial.println(currentmillis-previousmillis);
   serial.println();
 
   if ((unsigned long)(currentmillis - previousmillis) >= (interval))
       {
           char *msg = "3";
           vw_send((uint8_t *)msg, strlen(msg));
           vw_wait_tx(); // wait until whole message gone
           //previousmillis = currentmillis;            
       }
   }  
   
 if(voltage < activationvoltage)
   {
   char *msg = "2";
   vw_send((uint8_t *)msg, strlen(msg));
   vw_wait_tx(); // wait until whole message gone
   }
 
}
 

do have dr3100? if not, take line out:

vw_set_ptt_inverted(true); // required dr3100

declare time related variables unsigned long in pre-setup area, don't redeclare them in loop code:
currentmillis, previousmillis, interval


Arduino Forum > Using Arduino > Programming Questions > [Solved] Time elapsed since event triggered


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support