Link 1: Hit Counter Arduino Sketch
This sketch is for a counter that counts every time a button is pressed. It displays the number on an LCD screen.
Link 2: Piezo Vibration Sketch
This sketch is for the vibration sensor to light up an LED light.
Arduino Code:
Our code below displayed "Counts: 0" but the sensor did not cause it to count as we planned.
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//creates starting point for counter
int counts = 0;
//defines the pin connections
const int GroundPin= 1;
const int sensePin= 2;
//defines normal and threshold voltage levels
const int threshold= 250;
//variable to hold the value of sensePin
int senseState = 0;
//variable to hold previous value of the sensePin
int prevsenseState = 0;
void setup() {
Serial.begin(9600);
pinMode(GroundPin, OUTPUT);
digitalWrite(GroundPin, LOW);
// set up the LCD's number of columns and rows:
lcd.begin(8, 2);
// Print a message to the LCD.
lcd.print("Count: ");
}
void loop() {
// check the status of the sense
senseState = digitalRead(sensePin);
int reading= digitalRead(sensePin);
//compare the senseState to its previous state
if (senseState != prevsenseState)
Serial.println(reading);
if (reading > threshold) {
counts = counts + 1;
}
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of counts:
lcd.print(counts);
}
No comments:
Post a Comment