• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Stoper - Stopwatch i utrzymanie wyniku
#1
Witam.
Złożyłem układ wg tego schematu:

filmik z działaniem : film

KOD:
Kod:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


int ledPin = 13;                    // LED connected to digital pin 13
int buttonPin = 8;                  // button on pin 2

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
int blinking;                       // condition for blinking - timer is timing
int frameRate = 100;                // the frame rate (frames per second) at which the stopwatch runs - Change to suit
long interval = (1000/frameRate);   // blink interval
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of Frames
int fractionalSecs;                 // variable used to store fractional part of Seconds
int fractionalMins;                 // variable used to store fractional part of Minutes
int elapsedFrames;                  // elapsed frames for stop watch
int elapsedSeconds;                 // elapsed seconds for stop watch
int elapsedMinutes;                 // elapsed Minutes for stop watch
char buf[10];                       // string buffer for itoa function

void setup()
{
 lcd.begin(16, 2);                // intialise the LCD.
 pinMode(ledPin, OUTPUT);         // sets the digital pin as output
 pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
 digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.
lcd.setCursor(0,0);
 lcd.print("Subscribe at");
 lcd.setCursor(0,1);
 lcd.print("TutorialsArduino");
}

void loop(){

 digitalWrite(ledPin, LOW);            // Initiate LED and Step Pin States

 buttonState = digitalRead(buttonPin); // Check for button press, read the button state and store

// check for a high to low transition if true then found a new button press while clock is not running - start the clock    
  if (buttonState == LOW && lastButtonState == HIGH  &&  blinking == false){
   startTime = millis();                               // store the start time
     blinking = true;                                  // turn on blinking while timing
     delay(10);                                         // short delay to debounce switch
     lastButtonState = buttonState;                    // store buttonState in lastButtonState, to compare next time
  }

// check for a high to low transition if true then found a new button press while clock is running - stop the clock and report
  else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){
  blinking = false;                                    // turn off blinking, all done timing
  lastButtonState = buttonState;                       // store buttonState in lastButtonState, to compare next time

// Routine to report elapsed time            
  elapsedTime =   millis() - startTime;                // store elapsed time
  elapsedMinutes = (elapsedTime / 60000L);
  elapsedSeconds = (elapsedTime / 1000L);              // divide by 1000 to convert to seconds - then cast to an int to print
  elapsedFrames = (elapsedTime / interval);            // divide by 100 to convert to 1/100 of a second - then cast to an int to print
  fractional = (int)(elapsedFrames % frameRate);       // use modulo operator to get fractional part of 100 Seconds
  fractionalSecs = (int)(elapsedSeconds % 60L);        // use modulo operator to get fractional part of 60 Seconds
  fractionalMins = (int)(elapsedMinutes % 60L);        // use modulo operator to get fractional part of 60 Minutes
  lcd.clear();                                         // clear the LDC

if (fractionalMins < 10){                            // pad in leading zeros
     lcd.print("0");                                 // add a zero
     }

   lcd.print(itoa(fractionalMins, buf, 10));       // convert the int to a string and print a fractional part of 60 Minutes to the LCD
     lcd.print(":");                                 //print a colan.

if (fractionalSecs < 10){                            // pad in leading zeros
     lcd.print("0");                                 // add a zero
     }

lcd.print(itoa(fractionalSecs, buf, 10));          // convert the int to a string and print a fractional part of 60 Seconds to the LCD
  lcd.print(":");                                    //print a colan.

if (fractional < 10){                                // pad in leading zeros
     lcd.print("0");                                 // add a zero
     }    

lcd.print(itoa(fractional, buf, 10));              // convert the int to a string and print a fractional part of 25 Frames to the LCD
  }

else{
     lastButtonState = buttonState;                  // store buttonState in lastButtonState, to compare next time
  }

// run commands at the specified time interval
// blink routine - blink the LED while timing
// check to see if it's time to blink the LED; that is, the difference
// between the current time and last time we blinked the LED is larger than
// the interval at which we want to blink the LED.

if ( (millis() - previousMillis > interval) ) {

   if (blinking == true){
      previousMillis = millis();                    // remember the last time we blinked the LED

      digitalWrite(ledPin, HIGH);                   // Pulse the LED for Visual Feedback

      elapsedTime =   millis() - startTime;         // store elapsed time
        elapsedMinutes = (elapsedTime / 60000L);      // divide by 60000 to convert to minutes - then cast to an int to print
        elapsedSeconds = (elapsedTime / 1000L);       // divide by 1000 to convert to seconds - then cast to an int to print
        elapsedFrames = (elapsedTime / interval);     // divide by 40 to convert to 1/25 of a second - then cast to an int to print
        fractional = (int)(elapsedFrames % frameRate);// use modulo operator to get fractional part of 25 Frames
        fractionalSecs = (int)(elapsedSeconds % 60L); // use modulo operator to get fractional part of 60 Seconds
        fractionalMins = (int)(elapsedMinutes % 60L); // use modulo operator to get fractional part of 60 Minutes
        lcd.clear();                                  // clear the LDC

      if (fractionalMins < 10){                     // pad in leading zeros
        lcd.print("0");                             // add a zero
        }

      lcd.print(itoa(fractionalMins, buf, 10));   // convert the int to a string and print a fractional part of 60 Minutes to the LCD
        lcd.print(":");                             //print a colan.

      if (fractionalSecs < 10){                     // pad in leading zeros
        lcd.print("0");                             // add a zero
        }

      lcd.print(itoa(fractionalSecs, buf, 10));   // convert the int to a string and print a fractional part of 60 Seconds to the LCD
        lcd.print(":");                             //print a colan.

      if (fractional < 10){                         // pad in leading zeros
        lcd.print("0");                             // add a zero
        }
         lcd.print(itoa((fractional), buf, 10));  // convert the int to a string and print a fractional part of 25 Frames to the LCD
        }

   else{
         digitalWrite(ledPin, LOW);                 // turn off LED when not blinking
         }
}

}

 Moje pytanie: Czy mógłby ktoś mi podpowiedzieć czy da się tak zrobić żeby po naciśnięciu przycisku wartość stopera/timera była wyświetlana linijkę niżej(na stałe) i po naciśnięciu go kolejny raz stoper się zeruje(górna linijka LCD) jak na filmiku a wynik dalej widnieje w dolnej linii? Natomiast po naciśnięciu go kolejny raz stary wynik czasu w dolnej linii LCD zostaje zastąpiony nowym? Huh


Proszę o pomoc.

ps. może ktoś ma gotowca - kod stoper który liczy np okrążenia (taki jak dla biegaczy) wykorzystujący funkcję "millis" ??

edit: poprawione
 
Odpowiedź
#2
popraw link z fotką
tak da się można nawet zapisywać wyniki ale najprościej można zrobić ze ostatni pomiar będzie w Lini 2 gdy byś mia np 4x20 można było my wyświetlić 3 pomiary i stoper
 
Odpowiedź
#3
przekopałem spory kawałek googl'a i pare for.... właśnie chodzi mi o te rozwiązanie z linijką nr 2 Smile wiesz może jakiej komendy użyć żeby wynik został w dolnej linii ? Wiem ,że lcd.setCursor(0,1); lcd.print("?????"); skąd odczytać wartość?
 
Odpowiedź
#4
czekaj to ci pomogę ale chwile musisz poczekać muszę zbadać bibliotek

i muszę lcd podłączyć
 
Odpowiedź
#5
oki Smile
 
Odpowiedź
#6
Kod:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  

}

void loop() {
  // Turn off the display:
  
   lcd.setCursor(0, 0);
   lcd.print("1");
  
   lcd.setCursor(0, 1);
   lcd.print("2");
  
  

}
masz przyklad
 
Odpowiedź
#7
dzięki za szybką reakcje zaraz go przemiele i sprawdzę czy będzie działać Smile
 
Odpowiedź
#8
a wytłumaczyć zapomnałem

lcd.setCursor(x, y);

x-to początek wyświetlania w Lini
y- to linia
pamiętaj ze w programowaniu początkiem bitu nie jest 1 tylko 0

nie ma za co po to to forum powstał i po to my tu jesteśmy by pomagać
 
Odpowiedź
#9
wiem jak napisać tekst w dwóch linijkach a teraz jak to się ma do tego stopera ? jak mam skopiować wynik stopera górnej linii w dolną linijkę skąd odczytać zmienną? lcd.setCursor(0, 1); lcd.print("2"); ???

tak na marginesie...
BTW- chce wykonać projekt tego typu: czujnik np fotorezystor +stoper który będzie mi pokazywał ile czasu minęło miedzy różnymi wartościami światła np co 20%
uruchamiam stoper.... natężenie swiatła osiąga 20% stoper stop - wynik do odczytu spada na dolną linijke ale stoper dalej mierzy czas aż fotorezystor osiągnie 40% itd. potem 60%...
 
Odpowiedź
#10
musisz przepisac go do inych zmienych np
Kod:
cyfra11=stope1;
cyfra22=stope2;
cyfra33=stope3;


albo tablicowo

int cyfra[2],stoper[2];
fot(wiesz co tu ma byc 0 do 2){
cyfra[i]=stoper[i];
}
lcd.setCursor(0, 0)
lcd.print(stoper[0], stoper[1], stoper[2]);

lcd.setCursor(0, 1)
lcd.print(cyfra[0], cyfra[1], cyfra[2]);
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości