Arduino Stopwatch hjälp sökes :)

C, C++, Pascal, Assembly, Raspberry, Java, Matlab, Python, BASIC, SQL, PHP, etc.
kenneth1
Inlägg: 2
Blev medlem: 31 januari 2018, 13:29:10

Arduino Stopwatch hjälp sökes :)

Inlägg av kenneth1 »

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
}
}
Användarvisningsbild
Lennart Aspenryd
Tidigare Lasp
Inlägg: 12607
Blev medlem: 1 juli 2011, 19:09:09
Ort: Helsingborg

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av Lennart Aspenryd »

Hej och välkommen till forumet!
Du har alltså en knapp till! Bra.
Men du säger inte vad den skall göra!
Användarvisningsbild
GeekJoan
Admin
Inlägg: 10642
Blev medlem: 26 maj 2003, 15:59:27
Ort: Solna

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av GeekJoan »

En knapp ska starta och en annan ska stoppa säger han ju.
Användarvisningsbild
Lennart Aspenryd
Tidigare Lasp
Inlägg: 12607
Blev medlem: 1 juli 2011, 19:09:09
Ort: Helsingborg

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av Lennart Aspenryd »

Synsvag! Igen ;-( Förlåt.
Användarvisningsbild
Magnus_K
EF Sponsor
Inlägg: 5854
Blev medlem: 4 januari 2010, 17:53:25
Ort: Skogen mellan Uppsala-Gävle

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av Magnus_K »

Du får nog vara lite mer specifik med vad du har för hårdvara. Är det tex en_sån_här?
Om det är en sån, så blir det lite problematiskt än "vanligt". MEn allt går att lösa :)
I det fallet har dom valt att spara på IO:s på kortet genom att läsa av vilken knapp som är tryckt genom spänningsdelare. På så sätt används enbart 1 analog ingång istället för kanske 6 digitala.

Berätta mer vad du har för grejer och använd gärna också "code-knappen" när du läger in kod i ett inlägg. Betydligt lättare och läsa samt att man inte behöver scrolla så mycket på sidan.
Senast redigerad av Magnus_K 31 januari 2018, 23:02:47, redigerad totalt 1 gång.
Användarvisningsbild
GeekJoan
Admin
Inlägg: 10642
Blev medlem: 26 maj 2003, 15:59:27
Ort: Solna

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av GeekJoan »

En till som inte kan läsa. Koden kom från ett projekt som använde en display.
Han satte en knapp direkt till A1
Användarvisningsbild
Retroperra
Inlägg: 728
Blev medlem: 8 juli 2012, 14:34:12
Ort: Sundsvall
Kontakt:

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av Retroperra »

else if (x < 800 osv
ändras till
else if (analougeInput(1)==True eller nåt ditåt

Jag har bara telefon nu så det blir yxigt att skriva

Input 0 ör alltså A1, 1 är A2
Användarvisningsbild
Retroperra
Inlägg: 728
Blev medlem: 8 juli 2012, 14:34:12
Ort: Sundsvall
Kontakt:

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av Retroperra »

Här är en idé. Men det saknas koll mot att man trycker start två ggr.
Här är A1 och A2 tänkt att sättas höga (5V) men du kan ju skriva om så att man kopplar dessa mot 0 och att de hålls höga internt via pullup.

Kod: Markera allt

void loop() {
	int x; // declare variables
	int y; // declare variables
	x = analogRead (0); // startknapp
	y = analogRead (1); // stopknapp

if (x == true) //  A1 is high  {
	start = millis(); // saves start time to calculate the elapsed time
	delay(200); // for debounce
	lcd.clear();
	lcd.print("Started...");
}
else if (y == true) //  A2 is high {
	finished = millis(); // saves stop time to calculate the elapsed time
	delay(200); // for debounce
	lcd.clear();
	displayResult(); // display the results on the function
}
}
kenneth1
Inlägg: 2
Blev medlem: 31 januari 2018, 13:29:10

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av kenneth1 »

Tack för hjälpen och tipsen, har försökt med koden som du skrev Retroperra men får felmeddelandet "exit status 1
expected unqualified-id before 'else'
"

Är det något jag gjort fel?

Kod: Markera allt

/*

  Standalone Arduino StopWatch

  By Conor M - 11/05/15

  Modified by Elac - 12/05/15

  Modified By Conor M - 12/05/15

*/

// call the necessary libraries
#include <SPI.h>
#include <LiquidCrystal.h>
// these are the pins used on the shield for this sketch
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
// variables used on more than 1 function need to be declared here
unsigned long start, finished, elapsed;
boolean r = false;
// Variables for button debounce time
long lastButtonPressTime = 50; // the last time the button was pressed
long debounceDelay = 100; // the debounce time; keep this as low as possible

void loop() {
   int x; // declare variables
   int y; // declare variables
   x = analogRead (0); // startknapp
   y = analogRead (1); // stopknapp

if (x == true) //  A1 is high  {
   start = millis(); // saves start time to calculate the elapsed time
   delay(200); // for debounce
   lcd.clear();
   lcd.print("Started...");
}
else if (y == true) //  A2 is high {
   finished = millis(); // saves stop time to calculate the elapsed time
   delay(200); // for debounce
   lcd.clear();
   displayResult(); // display the results on the function
}
}
{
  lcd.begin(16, 2); // inicialize the lcd (16 chars, 2 lines)

  // a little introduction :)
  lcd.setCursor(5, 0); // set the cursor to first character on line 1 - NOT needed (it sets automatically on lcd.begin()
  lcd.print("Stoppur");
  delay(500); // wait 0,5 seconds
  lcd.clear(); // clear the display
  lcd.setCursor(5, 1);
  lcd.print("UGF AG");
  lcd.setCursor(1, 1);
  lcd.print("");
  delay(1000);
  lcd.clear();
  lcd.print("Deltagaren REDO");
  lcd.setCursor(5, 1); // set the cursor to 3rd character on line 2
  lcd.print("SPRINT");
}

void loop() 

void DisplayResult()
{
  if (r == true)
  {
    finished = millis(); // saves stop time to calculate the elapsed time
    // 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;
    // 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 ");
    if (h < 10)
    {
      lcd.print(ms, 0);
      lcd.print("ms ");
    }
  }
}
Användarvisningsbild
ffredrik
Inlägg: 340
Blev medlem: 20 oktober 2009, 17:52:18
Ort: Göinge

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av ffredrik »

På tre ställen ligger ett { efter // på raden, vilket innebär att det ignoreras.

Korr: två ställen !
Användarvisningsbild
Retroperra
Inlägg: 728
Blev medlem: 8 juli 2012, 14:34:12
Ort: Sundsvall
Kontakt:

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av Retroperra »

Jag gjorde ju bara en snabb killgissning ; ) men nånting åt det hållet är det allafall
MN71
Inlägg: 1
Blev medlem: 3 februari 2018, 14:27:47

Re: Arduino Stopwatch hjälp sökes :)

Inlägg av MN71 »

Jag justerade den källkod du postat senast i tråden kenneth1.
Den här versionen ger inga felmeddelanden när jag testar den i TinkerCAD:
Jag satte även r = true i deklarationen, annars skulle funktionen DisplayResults inte göra något.

Kod: Markera allt

/*

  Standalone Arduino StopWatch

  By Conor M - 11/05/15

  Modified by Elac - 12/05/15

  Modified By Conor M - 12/05/15

*/

// call the necessary libraries
#include <SPI.h>
#include <LiquidCrystal.h>
// these are the pins used on the shield for this sketch
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
// variables used on more than 1 function need to be declared here
unsigned long start, finished, elapsed;
boolean r = true;// false;
// Variables for button debounce time
long lastButtonPressTime = 50; // the last time the button was pressed
long debounceDelay = 100; // the debounce time; keep this as low as possible

void setup(){
  lcd.begin(16, 2); // initialize the lcd (16 chars, 2 lines)

  // a little introduction :)
  lcd.setCursor(5, 0); // set the cursor to first character on line 1 - NOT needed (it sets automatically on lcd.begin()
  lcd.print("Stoppur");
  delay(500); // wait 0,5 seconds
  lcd.clear(); // clear the display
  lcd.setCursor(5, 1);
  lcd.print("UGF AG");
  lcd.setCursor(1, 1);
  lcd.print("");
  delay(1000);
  lcd.clear();
  lcd.print("Deltagaren REDO");
  lcd.setCursor(5, 1); // set the cursor to 3rd character on line 2
  lcd.print("SPRINT");
}

void loop() {
   int x; // declare variables
   int y; // declare variables
   x = analogRead (0); // startknapp A0, knappen ska sluta en krets från 5V till GND
   y = analogRead (1); // stoppknapp A1, knappen ska sluta en krets från 5V till GND

  if (x == true){ //  A1 is high  
    start = millis(); // saves start time to calculate the elapsed time
    delay(200); // for debounce
    lcd.clear();
    lcd.print("Started...");
  }
  else if (y == true){ //  A2 is high 
    finished = millis(); // saves stop time to calculate the elapsed time
    delay(200); // for debounce
    lcd.clear();
    DisplayResult(); // display the results on the function
  }
}


void DisplayResult()
{
  if (r == true)
  {
    finished = millis(); // saves stop time to calculate the elapsed time
    // 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;
    // 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 ");
    if (h < 10)
    {
      lcd.print(ms, 0);
      lcd.print("ms ");
    }
  }
}
Man kan koppla flera knappar till en analog ingång.
http://www.instructables.com/id/How-to- ... ino-input/
Det är så knapparna på din LCD är kopplade.

Men som novis kanske man vill börja med att använda en ingång per knapp.
Skriv svar