Arduino Stopwatch hjälp sökes :)
Postat: 31 januari 2018, 13:48:18
Hej, jag är helt novis på det här med programmering men vill göra en timer med två brytare, en som startar timern och en som stoppar.
Hittade ett projekt som var bygg på en Uno och en LCD shield, den använde då bara en knapp för att starta och stoppa tiden och den knappen satt på LCD´n. Lödde fast en kabel på A1 och en extern brytare, vilket funkade. Men nu om jag vill ha en till knapp på tex A2 hur ändrar jag den här koden?
/*
Standalone Arduino StopWatch
By Conor M - 11/05/15
*/
// call the necessary libraries
#include <SPI.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); // these are the pins used on the shield for this sketch
unsigned long start, finished, elapsed; // variables used on more than 1 function need to be declared here
void setup()
{
lcd.begin(16, 2); // inicialize the lcd (16 chars, 2 lines)
// a little introduction
// lcd.setCursor(0,0); // set the cursor to first character on line 1 - NOT needed (it sets automatically on lcd.begin()
lcd.print(" The Arduino ");
lcd.setCursor(0,1); // set the cursor to first character on line 2
lcd.print(" StopWatch ");
delay(5000); // wait 5 seconds
lcd.clear(); // clear the display
// lcd.setCursor(0,0); // set the cursor to first character on line 1 - again, not needed, lcd.clear(); sets it
lcd.print("LFT - Start/Rst");
lcd.setCursor(0,1);
lcd.print("SEL - Elap. time");
}
void displayResult()
{
// declare variables
float h, m, s, ms;
unsigned long over;
// MATH time!!!
elapsed = finished - start;
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
lcd.setCursor(0,0);
lcd.print("Elapsed time: ");
// display the results
lcd.setCursor(0,1);
lcd.print(h, 0); // display variable 'h' - the 0 after it is the number of algorithms after a comma (ex: lcd.print(h, 2); would print 0,00
lcd.print("h "); // and the letter 'h' after it
lcd.print(m, 0);
lcd.print("m ");
lcd.print(s, 0);
lcd.print("s ");
lcd.print(ms, 0);
lcd.print("ms");
}
void loop()
{
int x; // declare variables
x = analogRead (0); // assign 'x' to the Arduino's AnalogueInputs (Shield's buttons)
if (x < 600 && x > 400) // if the button is LEFT
{
start = millis(); // saves start time to calculate the elapsed time
delay(200); // for debounce
lcd.clear();
// lcd.setCursor(0,1); // print on line 1 - NOT needed
lcd.print("Started...");
}
else if (x < 800 && x > 600) // if the button is SELECT
{
finished = millis(); // saves stop time to calculate the elapsed time
delay(200); // for debounce
lcd.clear();
// lcd.setCursor(0,0); // NOT needed
displayResult(); // display the results on the function
}
}
Hittade ett projekt som var bygg på en Uno och en LCD shield, den använde då bara en knapp för att starta och stoppa tiden och den knappen satt på LCD´n. Lödde fast en kabel på A1 och en extern brytare, vilket funkade. Men nu om jag vill ha en till knapp på tex A2 hur ändrar jag den här koden?
/*
Standalone Arduino StopWatch
By Conor M - 11/05/15
*/
// call the necessary libraries
#include <SPI.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); // these are the pins used on the shield for this sketch
unsigned long start, finished, elapsed; // variables used on more than 1 function need to be declared here
void setup()
{
lcd.begin(16, 2); // inicialize the lcd (16 chars, 2 lines)
// a little introduction

// lcd.setCursor(0,0); // set the cursor to first character on line 1 - NOT needed (it sets automatically on lcd.begin()
lcd.print(" The Arduino ");
lcd.setCursor(0,1); // set the cursor to first character on line 2
lcd.print(" StopWatch ");
delay(5000); // wait 5 seconds
lcd.clear(); // clear the display
// lcd.setCursor(0,0); // set the cursor to first character on line 1 - again, not needed, lcd.clear(); sets it
lcd.print("LFT - Start/Rst");
lcd.setCursor(0,1);
lcd.print("SEL - Elap. time");
}
void displayResult()
{
// declare variables
float h, m, s, ms;
unsigned long over;
// MATH time!!!
elapsed = finished - start;
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
lcd.setCursor(0,0);
lcd.print("Elapsed time: ");
// display the results
lcd.setCursor(0,1);
lcd.print(h, 0); // display variable 'h' - the 0 after it is the number of algorithms after a comma (ex: lcd.print(h, 2); would print 0,00
lcd.print("h "); // and the letter 'h' after it
lcd.print(m, 0);
lcd.print("m ");
lcd.print(s, 0);
lcd.print("s ");
lcd.print(ms, 0);
lcd.print("ms");
}
void loop()
{
int x; // declare variables
x = analogRead (0); // assign 'x' to the Arduino's AnalogueInputs (Shield's buttons)
if (x < 600 && x > 400) // if the button is LEFT
{
start = millis(); // saves start time to calculate the elapsed time
delay(200); // for debounce
lcd.clear();
// lcd.setCursor(0,1); // print on line 1 - NOT needed
lcd.print("Started...");
}
else if (x < 800 && x > 600) // if the button is SELECT
{
finished = millis(); // saves stop time to calculate the elapsed time
delay(200); // for debounce
lcd.clear();
// lcd.setCursor(0,0); // NOT needed
displayResult(); // display the results on the function
}
}