Assignment2
unknown
plain_text
a year ago
2.7 kB
3
Indexable
Never
#include <LiquidCrystal.h> LiquidCrystal lcd = LiquidCrystal(10,9,8,7,6,5); // Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7): const int trigPin = 12; const int buttonPin = 2; // the number of the pushbutton pin int sensorPin = 0; const int echoPin = 11; float time, distance; // Variables will change: int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button void setup() { lcd.begin(16, 2); // Specify the LCD's number of columns and rows. Change to (20, 4) for a 20x4 LCD pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(buttonPin, INPUT); Serial.begin(9600); } void loop() { buttonState = digitalRead(buttonPin); // compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == HIGH) { // if the current state is HIGH then the button went from off to on: buttonPushCounter++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter); } else { // if the current state is LOW then the button went from on to off: Serial.println("off"); } // Delay a little bit to avoid bouncing delay(50); } // save the current state as the last state, for next time through the loop lastButtonState = buttonState; if (buttonPushCounter == 1){ // turn LED on: digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); time = pulseIn(echoPin, HIGH); distance = (time*.0343)/2; // For Serial Monitor Serial.print("Distance:CM "); Serial.println(distance); // For LCD Display lcd.setCursor(0,0); lcd.print("Distance in CM"); lcd.setCursor(0,1); lcd.print(distance); }else if (buttonPushCounter == 2){ int reading = analogRead(sensorPin); // measure the 5v with a meter for an accurate value //In particular if your Arduino is USB powered float voltage = reading * 5.0; voltage /= 1024.0; // now print out the temperature float temperatureC = (voltage - 0.5) * 100 ; Serial.print(temperatureC); Serial.println(" degrees C"); lcd.setCursor(0,0); lcd.print("Temperature Value "); lcd.setCursor(0,1); lcd.print(" degrees C"); lcd.setCursor(11,1); lcd.print(temperatureC); }else{ buttonPushCounter = 0; } }