Permalänk
Medlem

hjälp med arduino kodning

HEJ

hur kodar jag så att jag få 3 tidverk att starta i en display på mitt arduino och är det äns möjligt ?

TID 1 och TID 2 skall starta med Pin 8
TID 1 stoppa på Pin 9
TID 3 starta Pin10
TID 2och 3 stoppa på Pin11

och hur kan jag räkna om tiden som jag får på TID3 tex 5meter delat med TID3 och multiplicerat med 3.6?

i dagsläget är det kodat så att Tid 1,2,3 startar på pin 8 och stoppar på Pin 9

bifogar koden och hur jag kopplat .

Mvh

här är bifogad bild på kopplingen
https://imgi.se/image/FPh

Här nedan följer koden .

#include <LiquidCrystal.h> LiquidCrystal lcd(2, 3, 4, 5, 6, 7); void setup() { lcd.begin(16, 2); lcd.clear(); Serial.begin(9600); pinMode(8, INPUT); digitalWrite(8, HIGH); pinMode(9, INPUT); digitalWrite(9, HIGH); pinMode(10, INPUT); digitalWrite(10, HIGH); pinMode(11, INPUT); digitalWrite(11, HIGH); } double i = 0; double a = millis(); double c ; void loop() { lcd.setCursor(0,0); lcd.print("TID1 "); lcd.setCursor(6,0); lcd.print("TID2 "); lcd.setCursor(12,0); lcd.print("TID3 "); delay(100); if(digitalRead(8) == LOW) { a = millis(); while(digitalRead(9) == HIGH) { c = millis(); i = (c - a) / 1000; lcd.setCursor(0,1); lcd.print(i); lcd.setCursor(6,1); lcd.print(i); lcd.setCursor(12,1); lcd.print(i); delay(100); } if(digitalRead(9) == LOW) { while(digitalRead(8) == HIGH) { lcd.setCursor(0,0); lcd.print("TID1 "); lcd.setCursor(6,0); lcd.print("TID2 "); lcd.setCursor(12,0); lcd.print("TID3 "); lcd.setCursor(0,1); lcd.print(i); lcd.setCursor(6,1); lcd.print(i); lcd.setCursor(12,1); lcd.print(i); delay(100); } } } }

Visa signatur

ska man ha sånt oxå

Permalänk

Du är lite oklar i specarna så jag vet inte riktigt vad du vill göra, men här får du lite att tänka på:

Du behöver inte göra allt jobbet när du kollar om pinnarna är hög eller låga. Du får nog klippa isär avkänningen av pinnarna och logiken som beräknar tidera. Exempelvis skulle du kunna hålla reda på senast kända tillstånd (HI/LOW) hos pinne 8 till 11 och spara tidpunkten (timeX) när pinne X bytte tillstånd. Då blir

tid1 = time9 - time8
tid2 = time11 - time8
tid3 = time11 - time10

Jag gissar att du vill se tiderna ticka upp på displayen så logiken blir lite krångligare, men du förstår vad jag menar när jag säger att du måste dela upp avkänningen och räknandet.

Permalänk
Medlem

Som @Ingetledigtnamn skriver så är det något oklart vad det är du vill åstadkomma. Vill du kunna starta/stoppa tre olika tidräknare individuellt? Om så, hade jag kikat på interrupts.

Visa signatur

Quidquid latine dictum sit, altum videtur.

Permalänk
Medlem

Ursäkta min otydlighet det stämmer bra att det är tidräknare jag vill starta

Det skall vara pushbuttons på pinsen som jag angivit och dom skall starta och stoppa så som jag beskrev förrut att

TID 1 och TID 2 skall starta med pushbutton som är kopplad på Pin 8
TID 1 stoppa på med Pin 9
TID 3 starta Pin10
TID 2 och 3 stoppa på Pin11

och sedan tiden som står kvar på TID3 efter man stannat klockan skall användas i en ekvation sträcka delat på tid för att få ut snitt hastighet som visas istället för TID3

Visa signatur

ska man ha sånt oxå

Permalänk
Medlem
Skrivet av RicoA3:

Ursäkta min otydlighet det stämmer bra att det är tidräknare jag vill starta

Det skall vara pushbuttons på pinsen som jag angivit och dom skall starta och stoppa så som jag beskrev förrut att

TID 1 och TID 2 skall starta med pushbutton som är kopplad på Pin 8
TID 1 stoppa på med Pin 9
TID 3 starta Pin10
TID 2 och 3 stoppa på Pin11

och sedan tiden som står kvar på TID3 efter man stannat klockan skall användas i en ekvation sträcka delat på tid för att få ut snitt hastighet som visas istället för TID3

Inte helt glasklart, men ger mig på ett försök.
Pseudokod(Antaget att knapparna är aktivt höga):

[Med reservationer för att jag helt missförstått dig] //Globala variabler Tid1_start = 0 Tid2_start = 0 Tid3_start = 0 //Icke globala Tid1_total = 0 Tid2_total = 0 Tid3_total = 0 Tid1_runs = False Tid2_runs = False Tid3_runs = False Ditt_resultat = 0 while True{ if pin8 == HIGH and Tid1_runs == False and Tid2_runs == False{ Tid1_start = millis() //Spara tiden när knappen trycktes in Tid2_start = millis() Tid1_runs = True //Dessa gör att tidräkningen inte nollställs om vi trycker två gånger på pin8 Tid2_runs = True *AKTIVERA INTERRUPTS* } if pin10 == HIGH{ Tid3_start = millis() Tid3_runs = True *AKTIVERA INTERRUPTS* } if pin9 == HIGH and Tid1_runs == True{ Tid1_total = millis() - Tid1_start //Skillnaden mellan just nu och när knappen trycktes in. I ms. Tid1_runs = False // Så vi kan starta igen } if pin11 == HIGH{ Tid2_total = millis() - Tid2_start Tid3_total = millis() - Tid3_start Tid2_runs = False Tid3_runs = False Ditt_resultat = *Din ekvation med Tid3_total* *Avaktivera interrupts* *flytta cursorn* lcd.print(Ditt_resultat) } } //http://www.instructables.com/id/Arduino-Timer-Interrupts/ ISR{ //För att uppdatera displayen, kolla på timerinterrupts. Håll koden så kort och effektiv som möjligt här. Du behöver //Rimligt tidsintervall är väl var 100ms *Sätt cursorn på lämpligt ställe* lcd.print(millis() - tid1_start) // Skriv ut hur lång tid det tagit sen timern startades *Flytta cursorn* lcd.print(millis() - tid2_start) *Flytta cursorn* lcd.print(millis() - tid3_start) }

Timerinterrupts kan verka lite knepigt till en början, men läser du igenom tutorialen i lugn och ro så släpper det nog.

Ett tips för framtiden är att beskriva vad det är du vill göra med manicken. Att du vill ha knappar på en viss pinne ger inte så mycket information till den som försöker hjälpa dig. I det är fallet är det väldigt oklart varför du vill starta timer1 och 2 samtidigt, men när du stoppar så ska 2 och 3 stanna på samma knapp. Försök förklara vad det är du vill åstadkomma istället.

Din beskrivning av logiken har du tänkt ut med information av systemet som helhet, något vi inte har i det här fallet och gör det väldigt svårt att förstå vad problemet egentligen är.

Lycka till!

Visa signatur

Quidquid latine dictum sit, altum videtur.

Permalänk
Medlem

tack för hjälpen skall prova detta skall ta läsa lite mer angående arduino kodning har precis gett mig in i denna värld och jag skall tänka på hur jag förklarar problemet vid nästa gång . så som jag tänkt är att det är tidmätning på en uppmätt sträcka och anledningen att det skall stannas och stoppas olika är olika avstånd för knapparna, testar just nu med knappar tills jag får tidtagning att fungera sedan byter jag ut knapparna mot fotoceller. kanske skulle tänkt på det från början med kodning .

Tid 1 och tid 2 startar från 0 meter tid 1 stannar vid tex 3 meter medans tid 2 på 30 meter som är totala sträckan uppmätt på tid tid 3 startar på tex 27 meter och kommer användas för att mäta hastighet på ett fordon där av den matematiska ekvationen

Visa signatur

ska man ha sånt oxå

Permalänk
Medlem

Varför behöver du tre tidur? Om du har två mätstationer (knappar eller fotoceller) separerade med ett visst avstånd kan du starta en klocka vid den första stationen och stoppa vid den andra. Sen tar du den kända sträckan delat med uppmätt tid för att få medelhastigheten.

Eller är det något annat du inte berättat? Försök beskriva uppgiften från början så att du inte "målar in dig" i fel lösning.

Utifrån det du skrivit låter detta som ett klockrent XY-problem:

  • User wants to do X.

  • User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.

  • User doesn't know how to do Y either.

  • User asks for help with Y.

  • Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.

  • After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.

Permalänk
Medlem

jag medger att min kod inte är korrekt och nej jag kanske inte kan Y eller X men jag försöker lära mig där av att jag ber om hjälp och försöker förklara så tydligt jag kan ur mitt perspektiv

varför jag behöver 3 urverk jo för att jag vill ha tid från 0-3 meter = tid1 och 0-30meter = tid 2 och tid 3 använder jag bara som en fartfälla för att mäta hastighet sen kanske jag borde formulerat mig tydligare från början det medger jag det slutgiltiga syftet är att jag skall ha det till en bilbana i skala 1;8

Visa signatur

ska man ha sånt oxå

Permalänk
Medlem

hej igen nu tror jag mig ha löst det mesta utom att konvertera den sista tiden till hastighet för den som undrar så blev det 3st lcds istället för en bifogar koden nedan :

sen är ju frågan om det är vettigt att ha 500 rader kod och kan jag förenkla det på något sätt

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd (12, 2, 7, 8, 9, 10);
LiquidCrystal lcd2 (12, 3, 7, 8, 9, 10);
LiquidCrystal lcd3 (12, 4, 7, 8, 9, 10);

#define buttonPin 5 // button on pin 5
#define buttonPin2 6 // button on pin 6
#define buttonPin3 11 // button on pin 11
#define ledPin 13 // LED connected to digital pin 13
#define thresholdPercent 8 // The percentage points below 100% of initial light level to trigger the counter start and stop
#define thresholdPercent1 8 // The percentage points below 100% of initial light level to trigger the counter start and stop
#define thresholdPercent2 8 // The percentage points below 100% of initial light level to trigger the counter start and stop

int lightPin1 = 0; // Define a pin for start Photo resistor
int lightPin2 = 1; // Define a pin for the stop photo resistor
int lightPin3 = 2; // Define a pin for start Photo resistor
int lightPin4 = 3; // Define a pin for the stop photo resistor

int lightLevel1 = 0; // Place to store light level value from first photo resistor
int lightLevel2 = 0; // Place to store light level value from second photo resistor
int lightLevel3 = 0; // Place to store light level value from first photo resistor
int lightLevel4 = 0; // Place to store light level value from second photo resistor

int threshold1 = 0; // Calculated level to trigger counter start
int threshold2 = 0; // Calculated level to trigger counter stop
int threshold3 = 0; // Calculated level to trigger counter start
int threshold4 = 0; // Calculated level to trigger counter stop

int lastLightLevel1 = 0; // Used to store previous lightlevel value to debounce the start trigger
int lastLightLevel2 = 0; // Used to store previous lightlevel value to debounce the stop trigger
int lastLightLevel3 = 0; // Used to store previous lightlevel value to debounce the start trigger
int lastLightLevel4 = 0; // Used to store previous lightlevel value to debounce the stop trigger

int value = LOW; // previous value of the LED
int buttonState = false; // variable to store button state
int lastButtonState = false; // variable to store last button state
int blinking = false; // condition for blinking - timer is timing
long interval = 100; // blink interval - change to suit
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 time

int value2 = LOW; // previous value of the LED
int buttonState2 = false; // variable to store button state
int lastButtonState2 = false; // variable to store last button state
int blinking2 = false; // condition for blinking - timer is timing
long interval2 = 100; // blink interval - change to suit
long previousMillis2 = 0; // variable to store last time LED was updated
long startTime2 ; // start time for stop watch
long elapsedTime2 ; // elapsed time for stop watch
int fractional2; // variable used to store fractional part of time

int value3 = LOW; // previous value of the LED
int buttonState3 = false; // variable to store button state
int lastButtonState3 = false; // variable to store last button state
int blinking3 = false; // condition for blinking - timer is timing
long interval3 = 100; // blink interval - change to suit
long previousMillis3 = 0; // variable to store last time LED was updated
long startTime3 ; // start time for stop watch
long elapsedTime3 ; // elapsed time for stop watch
int fractional3; // variable used to store fractional part of time

void setup()
{
//LCD 1
//initialize pin i/o states
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(buttonPin, INPUT); // not really necessary, pins default to INPUT anyway

//Begin the LCD interface
lcd.begin(16,2);

//Print my name
lcd.setCursor(0,0);
lcd.print("T1");

//Initialize start button
pinMode(buttonPin, INPUT); // not really necessary, pins default to INPUT anyway
digitalWrite(buttonPin, LOW); // turn on pullup resistors. Wire button so that press shorts pin to ground.

//Calculate the optical sensor trigger levels for the counter
lightLevel1 = analogRead(lightPin1);
lightLevel2 = analogRead(lightPin2);

threshold1 = (lightLevel1*((100-thresholdPercent)/100.0));
threshold2 = (lightLevel2*((100-thresholdPercent)/100.0));

//Print out the threshold values on row 2 of the LCD
lcd.setCursor(9,1);
lcd.print(threshold1);
lcd.print(",");
lcd.print(threshold2);

// LCD 2
//initialize pin i/o states
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(buttonPin2, INPUT); // not really necessary, pins default to INPUT anyway

//Begin the LCD interface
lcd2.begin(16,2);

//Print my name
lcd2.setCursor(0,0);
lcd2.print("T2");

//Initialize start button
pinMode(buttonPin2, INPUT); // not really necessary, pins default to INPUT anyway
digitalWrite(buttonPin2, LOW); // turn on pullup resistors. Wire button so that press shorts pin to ground.

//Calculate the optical sensor trigger levels for the counter
lightLevel1 = analogRead(lightPin1);
lightLevel4 = analogRead(lightPin4);

threshold1 = (lightLevel1*((100-thresholdPercent)/100.0));
threshold4 = (lightLevel4*((100-thresholdPercent1)/100.0));

//Print out the threshold values on row 2 of the LCD
lcd2.setCursor(9,1);
lcd2.print(threshold1);
lcd2.print(",");
lcd2.print(threshold4);

// LCD 3
//initialize pin i/o states
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(buttonPin3, INPUT); // not really necessary, pins default to INPUT anyway

//Begin the LCD interface
lcd3.begin(16,2);

//Print my name
lcd3.setCursor(0,0);
lcd3.print("T3");

//Initialize start button
pinMode(buttonPin3, INPUT); // not really necessary, pins default to INPUT anyway
digitalWrite(buttonPin3, LOW); // turn on pullup resistors. Wire button so that press shorts pin to ground.

//Calculate the optical sensor trigger levels for the counter
lightLevel3 = analogRead(lightPin3);
lightLevel4 = analogRead(lightPin4);

threshold3 = (lightLevel3*((100-thresholdPercent2)/100.0));
threshold4 = (lightLevel4*((100-thresholdPercent2)/100.0));

//Print out the threshold values on row 2 of the LCD
lcd3.setCursor(9,1);
lcd3.print(threshold3);
lcd3.print(",");
lcd3.print(threshold4);
// END
}

void loop()
{
// LCD 1
//Move the cursor
lcd.setCursor(0,1);

//Clear the line
//lcd.print(" ");

lcd.setCursor(13,0);
lcd.print("B:");
lcd.print(digitalRead(buttonPin));

//Send the value measured on Analog pin 0 to the serial port & print to the LCD screen
lightLevel1 = analogRead(lightPin1);
lightLevel2 = analogRead(lightPin2);

//lightLevel1 = constrain(lightLevel1,800,950);
//lightLevel1 = map(lightLevel1,400,900,0,100);
//lightLevel2 = map(lightLevel2,800,1000,0,100);
//Serial.print(lightLevel1); //Write the value of the photoresistor to the serial monitor
//Serial.print(", ");
//Serial.println(lightLevel2);
lcd.setCursor(4,0);
lcd.print(" ");
lcd.setCursor(4,0);
lcd.print(lightLevel1);
lcd.print(",");
lcd.print(lightLevel2);

//delay(50);

//pasted in timer and LED blinking routines from Stopwatch sketch
// check for button press
buttonState = digitalRead(buttonPin); // read the button state and store

if (((buttonState == LOW && lastButtonState == HIGH) || (lightLevel1 <= threshold1)) && blinking == false){ // check for a high to low transition
// if true then found a new button press while clock is not running - start the clock

startTime = millis(); // store the start time
blinking = true; // turn on blinking while timing
delay(5); // short delay to debounce switch
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lastLightLevel1 = lightLevel1; // store lightlevel in lastLightLevel, to compare next time
lastLightLevel2 = lightLevel2;

//Turn on timing indicator
lcd.setCursor(12,0);
lcd.print("+");
}

else if (((buttonState == LOW && lastButtonState == HIGH) || (lightLevel2 <= threshold2)) && blinking == true){ // check for a high to low transition
// if true then found a new button press while clock is running - stop the clock and report

elapsedTime = millis() - startTime; // store elapsed time
blinking = false; // turn off blinking, all done timing
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time

//Turn off timing indicator
lcd.setCursor(12,0);
lcd.print(" ");

//Clear previous time
lcd.setCursor(0,1);
lcd.print(" ");

// routine to report elapsed time
Serial.print( (int)(elapsedTime / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
Serial.print("."); // print decimal point
lcd.setCursor(0,1);
lcd.print((int)(elapsedTime / 1000L));
lcd.print(".");

// use modulo operator to get fractional part of time
fractional = (int)(elapsedTime % 1000L);

// pad in leading zeros - wouldn't it be nice if
// Arduino language had a flag for this?
if (fractional == 0)
lcd.print("000"); // add three zero's
else if (fractional < 10) // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
lcd.print("00"); // add two zeros
else if (fractional < 100)
lcd.print("0"); // add one zero

lcd.print(fractional); // print fractional part of time

}

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

}

// 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

// if the LED is off turn it on and vice-versa.
if (value == LOW)
value = HIGH;
else
value = LOW;
digitalWrite(ledPin, value);
}
else{
digitalWrite(ledPin, LOW); // turn off LED when not blinking
}

// Print out the value of the blinking variable in the last character of the 2nd row on the LCD
// lcd.setCursor(15,1);
// lcd.print(blinking);
}
// LCD 2

//Move the cursor
lcd2.setCursor(0,1);

//Clear the line
//lcd2.print(" ");

lcd2.setCursor(13,0);
lcd2.print("B:");
lcd2.print(digitalRead(buttonPin2));

//Send the value measured on Analog pin 0 to the serial port & print to the LCD screen
lightLevel1 = analogRead(lightPin1);
lightLevel4 = analogRead(lightPin4);

//lightLevel1 = constrain(lightLevel,800,950);
//lightLevel1 = map(lightLevel1,400,900,0,100);
//lightLevel4 = map(lightLevel4,800,1000,0,100);
//Serial.print(lightLevel1); //Write the value of the photoresistor to the serial monitor
//Serial.print(", ");
//Serial.println(lightLevel4);
lcd2.setCursor(4,0);
lcd2.print(" ");
lcd2.setCursor(4,0);
lcd2.print(lightLevel1);
lcd2.print(",");
lcd2.print(lightLevel4);

//delay(50);

//pasted in timer and LED blinking routines from Stopwatch sketch
// check for button press
buttonState = digitalRead(buttonPin); // read the button state and store

if (((buttonState == LOW && lastButtonState == HIGH) || (lightLevel1 <= threshold1)) && blinking2 == false){ // check for a high to low transition
// if true then found a new button press while clock is not running - start the clock

startTime2 = millis(); // store the start time
blinking2 = true; // turn on blinking while timing
delay(5); // short delay to debounce switch
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
lastLightLevel1 = lightLevel1; // store lightlevel in lastLightLevel, to compare next time
lastLightLevel4 = lightLevel4;

//Turn on timing indicator
lcd2.setCursor(12,0);
lcd2.print("+");
}

else if (((buttonState == LOW && lastButtonState == HIGH) || (lightLevel4 <= threshold4)) && blinking2 == true){ // check for a high to low transition
// if true then found a new button press while clock is running - stop the clock and report

elapsedTime2 = millis() - startTime2; // store elapsed time
blinking2 = false; // turn off blinking, all done timing
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time

//Turn off timing indicator
lcd2.setCursor(12,0);
lcd2.print(" ");

//Clear previous time
lcd2.setCursor(0,1);
lcd2.print(" ");

// routine to report elapsed time
Serial.print( (int)(elapsedTime2 / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
Serial.print("."); // print decimal point
lcd2.setCursor(0,1);
lcd2.print((int)(elapsedTime2 / 1000L));
lcd2.print(".");

// use modulo operator to get fractional part of time
fractional2 = (int)(elapsedTime2 % 1000L);

// pad in leading zeros - wouldn't it be nice if
// Arduino language had a flag for this?
if (fractional2 == 0)
lcd2.print("000"); // add three zero's
else if (fractional2 < 10) // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
lcd2.print("00"); // add two zeros
else if (fractional2 < 100)
lcd2.print("0"); // add one zero

lcd2.print(fractional2); // print fractional part of time

}

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

}

// 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() - previousMillis2 > interval2) ) {

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

// if the LED is off turn it on and vice-versa.
if (value2 == LOW)
value2 = HIGH;
else
value2 = LOW;
digitalWrite(ledPin, value);
}
else{
digitalWrite(ledPin, LOW); // turn off LED when not blinking
}

// Print out the value of the blinking variable in the last character of the 2nd row on the LCD
// lcd.setCursor(15,1);
// lcd.print(blinking);
}
// LCD 3

//Move the cursor
lcd3.setCursor(0,1);

//Clear the line
//lcd2.print(" ");

lcd3.setCursor(13,0);
lcd3.print("B:");
lcd3.print(digitalRead(buttonPin3));

//Send the value measured on Analog pin 0 to the serial port & print to the LCD screen
lightLevel3 = analogRead(lightPin3);
lightLevel4 = analogRead(lightPin4);

//lightLevel3 = constrain(lightLevel3,800,950);
//lightLevel3 = map(lightLevel3,400,900,0,100);
//lightLevel4 = map(lightLevel4,800,1000,0,100);
//Serial.print(lightLevel3); //Write the value of the photoresistor to the serial monitor
//Serial.print(", ");
//Serial.println(lightLevel4);
lcd3.setCursor(4,0);
lcd3.print(" ");
lcd3.setCursor(4,0);
lcd3.print(lightLevel3);
lcd3.print(",");
lcd3.print(lightLevel4);

//delay(50);

//pasted in timer and LED blinking routines from Stopwatch sketch
// check for button press
buttonState3 = digitalRead(buttonPin3); // read the button state and store

if (((buttonState3 == LOW && lastButtonState3 == HIGH) || (lightLevel3 <= threshold3)) && blinking3 == false){ // check for a high to low transition
// if true then found a new button press while clock is not running - start the clock

startTime3 = millis(); // store the start time
blinking3 = true; // turn on blinking while timing
delay(5); // short delay to debounce switch
lastButtonState3 = buttonState3; // store buttonState in lastButtonState, to compare next time
lastLightLevel3 = lightLevel3; // store lightlevel in lastLightLevel, to compare next time
lastLightLevel4 = lightLevel4;

//Turn on timing indicator
lcd3.setCursor(12,0);
lcd3.print("+");
}

else if (((buttonState3 == LOW && lastButtonState3 == HIGH) || (lightLevel4 <= threshold4)) && blinking3 == true){ // check for a high to low transition
// if true then found a new button press while clock is running - stop the clock and report

elapsedTime3 = millis() - startTime3; // store elapsed time
blinking3 = false; // turn off blinking, all done timing
lastButtonState3 = buttonState3; // store buttonState in lastButtonState, to compare next time

//Turn off timing indicator
lcd3.setCursor(12,0);
lcd3.print(" ");

//Clear previous time
lcd3.setCursor(0,1);
lcd3.print(" ");

// routine to report elapsed time
Serial.print( (int)(elapsedTime3 / 1000L)); // divide by 1000 to convert to seconds - then cast to an int to print
Serial.print("."); // print decimal point
lcd3.setCursor(0,1);
lcd3.print((int)(elapsedTime3 / 1000L));
lcd3.print(".");

// use modulo operator to get fractional part of time
fractional3 = (int)(elapsedTime3 % 1000L);

// pad in leading zeros - wouldn't it be nice if
// Arduino language had a flag for this?
if (fractional3 == 0)
lcd3.print("000"); // add three zero's
else if (fractional3 < 10) // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
lcd3.print("00"); // add two zeros
else if (fractional3 < 100)
lcd3.print("0"); // add one zero

lcd3.print(fractional3); // print fractional part of time

}

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

}

// 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() - previousMillis3 > interval3) ) {

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

// if the LED is off turn it on and vice-versa.
if (value3 == LOW)
value3 = HIGH;
else
value3 = LOW;
digitalWrite(ledPin, value);
}
else{
digitalWrite(ledPin, LOW); // turn off LED when not blinking
}

// Print out the value of the blinking variable in the last character of the 2nd row on the LCD
// lcd.setCursor(15,1);
// lcd.print(blinking);
}
}

Visa signatur

ska man ha sånt oxå

Permalänk

Nu har jag inte lusläst alla 500 raderna, men det såg ut som om det var tre likadana sektioner som bara varierade i om variablerna hette x, x1 eller x2. Du borde kunna göra en struct/class med dina variabler

struct Timer { // Timer känns inte helt rätt, vad kallar du en av dessa "enheter"? LiquidCrystal lcd; int value; int buttonState; int lastButtonState; int blinking; long interval; long previousMillis; long startTime; long elapsedTime; int fractional; };

och bryta ut de gemensamma delarna till en funktion som opererar på ett Timer-objekt.

Permalänk
Medlem
Skrivet av RicoA3:

jag medger att min kod inte är korrekt och nej jag kanske inte kan Y eller X men jag försöker lära mig där av att jag ber om hjälp och försöker förklara så tydligt jag kan ur mitt perspektiv

varför jag behöver 3 urverk jo för att jag vill ha tid från 0-3 meter = tid1 och 0-30meter = tid 2 och tid 3 använder jag bara som en fartfälla för att mäta hastighet sen kanske jag borde formulerat mig tydligare från början det medger jag det slutgiltiga syftet är att jag skall ha det till en bilbana i skala 1;8

Din lösning på 500rader är absolut inte nödvändig. Du behöver inte starta 3 olika tidräknare, starta bara en och utgå från den och spara undan värden när sensorer triggas. Det du ska göra är att:

En tidräknare startas när sensor 1 triggas.
När sensor 2 triggas sparas tidräknarens värde till en variabel (variable A).
När sensor 3 triggas så sparas tidräknarens värde till en annan variabel (variable B).

Sen antar jag att du vill visa detta på en display (har inte läst allt så noga)

Tid1: Första tiden
Tid2: Totala
Tid3: Skillnaden

När sensor 1 triggas och innan sensor 2 triggats så är displayens output
Tid1: Tidräknarens aktuella värde
Tid2: Tidräknarens aktuella värde
Tid3: Empty

När sensor 2 triggas och innan sensor 3 triggats så är displayens output
Tid1: Variable A
Tid2: Variable B
Tid3: Variable B - Variable A

När sensor 3 triggats så är
Tid1: Variable A
Tid2: Tidräknarens aktuella värde
Tid3: (Tidräknarens aktuella värde) - variable A

Visa signatur

VR: Oculus Rift CV1 + Touch
Dator: Ryzen 2600X | 16GB 2933 MHz | RTX 2070 FE | 2 x Samung 850 SSD 250GB | Corsair SF600 | Louqe Ghost S1 | Noctua L12
Server: HP ProLiant MicroServer G7 N54L