So after a while of not getting my wiring/coding correct, I finally was able to put together a simple switch that increments a counter and flashes an LED everytime the switch is activated.
The counter value is displayed everytime the switch is activated. This simple switch could be replaced with any other sensor, such as an impact sensor that counts how many people walk on a rug.
int ledPin = 13; //designates LED pin
int switchPin = 4; //designates probe pin
int delayAmount = 700;
int switchStatus = 0; //starts the switch status at LOW
int counter = 0; //the counter is set at 0
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //LED is the output
pinMode(switchPin, INPUT); //the switch feeds in the information
}
void loop()
{
switchStatus = digitalRead(switchPin); //switch gets an initial value
if (switchStatus == HIGH) //this if statement checks to increment the counter
{
counter++;
Serial.print("The circuit has been completed ");
Serial.print(counter);
Serial.println(" times.");
}
while (switchStatus == HIGH) //this while loop keeps the LED blinking while switch is activated
{
digitalWrite(ledPin, HIGH);
delay(delayAmount);
digitalWrite(ledPin, LOW);
delay(delayAmount);
switchStatus = digitalRead(switchPin); //the switch status is checked before flashing the LED again
}
}