Timer

Arduino specifieke Software
Berichten: 8
Geregistreerd: 14 Apr 2016, 17:29

Timer

Berichtdoor greenfield » 25 Okt 2016, 18:07

]Ik kom er niet uit!

Hoe kan ik in de onderstaande code een timer krijgen? (in millis)
Die begint te tellen als die low is.
En de tijd die daar tussen zit totdat die high is.
Zodat ik die tijd kan gaan gebruiken.
Denk het ongeveer te weten maar geen resultaat. (huidige en vorige milles)
cpp code
/*
State change detection (edge detection)

Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.

This example shows how to detect when a button or button changes from off to on
and on to off.

The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
most Arduino boards)

created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/ButtonStateChange

*/

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}


void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;


// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

}

Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Timer

Berichtdoor nicoverduin » 25 Okt 2016, 19:08

Als de button LOW is EN hij loopt nog niet (= 0) DAN zet je een timer op millis() [Nu loopt hij dus wel]
Als de button HIGH is EN de timer loopt DAN kun je het verschil meten en de timer op 0 zetten.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 8
Geregistreerd: 14 Apr 2016, 17:29

Re: Timer

Berichtdoor greenfield » 25 Okt 2016, 19:29

zo je misschien een voorbeeld kunnen geven in de bovenstaande code???
zodat bij mij het kwartje eindelijk eens gaat vallen.
ik snap dat het voor jou heel eenvoudig is, maar ik zit al een week te kloten.

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: Timer

Berichtdoor Koepel » 25 Okt 2016, 23:32

Als jij zegt dat je al een week zit te kloten, dat is millis().

In pseudocode:
Code: Alles selecteren
if( dit_moment - 18_oktober >= 1_week )
{
  newTopic( "Arduinoforum.nl");
}


millis() is het aantal milliseconden vanaf het moment dat de Arduino is gestart of na een reset.
Meer is het niet, gewoon de tijd in millisconden.

Het gebruik van millis() is echter wel lastig, omdat millis() een 32-bit unsigned long is, die na 50 dagen doordraait naar nul en dus weer opnieuw begint.
Dat heeft een paar consequenties. Je mag bijvoorbeeld geen waarde bij millis() optellen. Je mag wel het huidige moment onthouden. Ik noem dat vaak de 'timestamp' nemen. Je hebt als het ware een stempel met de tijd, die je met een flinke knal op papier zet, om zo de tijd te onthouden. In een sketch is dat vaak 'previousMillis'.

Vervolgens zie je vaak dit: millis() - previousMillis
Dat is dus altijd de huidige tijd min de timestamp uit het verleden.
Als 'previousMillis' net als millis() ook nog 'unsigned long' is, dan gaat het altijd goed. Hoe dat rekenkundig werkt kun je met deze sketch uitproberen: http://playground.arduino.cc/Code/TimingRollover#arithmetic

Hieronder jouw sketch, maar dan wat ingekort en met millis() er bij.
Code: Alles selecteren
// Using the State change detection (edge detection)
// https://www.arduino.cc/en/Tutorial/StateChangeDetection


const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Assuming HIGH is the inactive state.
int lastButtonState = HIGH;  // previous state of the button

unsigned long previousMillis;

void setup()
{
  Serial.begin( 9600);
  Serial.println( "Started");
 
  pinMode(buttonPin, INPUT_PULLUP);             // use internal pullup resistor
  pinMode(ledPin, OUTPUT);
}


void loop()
{
  int buttonState = digitalRead( buttonPin);

  if( buttonState != lastButtonState)
  {
    // if the state has changed
    if( buttonState == HIGH)
    {
      // This is the very moment that the input turned HIGH.
      Serial.println( "on");

      unsigned long timeLow = millis() - previousMillis;   // Hier gebeurt het !
      Serial.print( "Dat was ");
      Serial.print( timeLow / 1000UL);
      Serial.print( " seconden en ");
      Serial.print( timeLow % 1000UL);
      Serial.print( " milliseconden");
      Serial.println();
    }
    else
    {
      // This is the very moment that the input turned LOW.
      Serial.println( "off");

      // Timestamp this moment.
      previousMillis = millis();
    }

    // remember this new state of the button.
    lastButtonState = buttonState;
  }

  delay( 10);      // slow down the loop()
}

P.S.: Ik ben gewend om de regel "lastButtonState = buttonState" op een andere plaats te zetten, dat is een eigenaardigheidje van mij.

Berichten: 8
Geregistreerd: 14 Apr 2016, 17:29

Re: Timer

Berichtdoor greenfield » 26 Okt 2016, 21:47

super!!!
dank je wel :D

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 86 gasten