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);
}
}