|
楼主 |
发表于 2013-10-23 11:10
|
显示全部楼层
程序在下面,传感器用的是KEYENCE的FU-E40。计时的显示部分没有问题了,但是传感器的部分根本没起到作用。现在两个按键可以分别控制计时器的开关,但我需要的是按下第一个键后被传感器感应到才开始计时,按下第二个键后被感应到再停止计时。有解决的办法吗?
谢啦!
[mw_shl_code=cpp,true]
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int ledPin = 13; // LED connected to digital pin 13
int button1Pin = 2; // button1 on pin 2
int button2Pin = 3; // button1 on pin 3
int sensorPin = 4;
int sensorvalue= 0;
int value = LOW; // previous value of the LED
int button1State; // variable to store button state
int button2State;
int sensorState;
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(button1Pin, INPUT); // not really necessary, pins default to INPUT anyway
pinMode(button2Pin, INPUT);
pinMode(sensorPin, INPUT);
// sensorvalue=digitalRead(sensorPin);
delay(10);
digitalWrite(button1Pin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground.
digitalWrite(button2Pin, HIGH);
// Print a message to the LCD.
lcd.setCursor(0, 1);
lcd.print("StopWatch: ");
}
void loop(){
digitalWrite(ledPin, LOW); // Initiate LED and Step Pin States
button1State = digitalRead(button1Pin); // Check for button1 press, read the button state and store
button2State = digitalRead(button2Pin);
// check for a high to low transition if true then found a new button press while clock is not running - start the clock
if (button1State == LOW && button2State == HIGH ){
sensorState = digitalRead(sensorPin);
if (sensorState == 0)
startTime = millis(); // store the start time
blinking = true; // turn on blinking while timing
delay(10); // short delay to debounce switch
}
else if (button2State == LOW && button1State == HIGH && blinking == true){
sensorState = digitalRead(sensorPin);
if (sensorState == 0)
blinking = false; // turn off blinking, all done timing
// 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
}
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
}
}
[/mw_shl_code]
|
|