Well here's Pete's code modified to increment count whenever pin 3 is grounded.
Bit more complicated that it should be because I use a different version of the LiquidCrystal library and only have a 20×4 display.
Testing by tapping a lead from pin 3 to ground. It works, but as noted in the code, it detects contact bounce galore! Debouncing is a separate issue and, as the best way of fixing it depends on Pete's input circuit, I won't cover it in this post.
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
// SoD : there are a few different LiquidCrystal_I2C libraries. Changed to use the one I have
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
LiquidCrystal_I2C lcd(0x3f,20,4); // SoD's display has LCD address to 0x3f for a 20 chars x 4 line display
//int ledPin = 13;
int ledPin = 2;
int switch1 = 3;
volatile int count; // SoD: Changed to volatile.
void switch1InterruptServiceRoutine()
{
// SoD: Executed when an interrupt is received on the attached pin
// Note: code is potentially too simple for direct use with a mechanical switch
// because the ISR will trigger repeatedly if the switch contact bounces. (As they do!)
// See 'debouncing' on web
count++;
}
void setup()
{
Serial.begin(9600); // Used to type in characters
//lcd.begin(16, 2);// set up the LCD's number of columns and rows:
lcd.init(); lcd.backlight(); // needed for SoD's version of the LiquidCrystal library
lcd.setCursor(2, 0); // Print a message to the LCD.
lcd.print("Rail Squirt"
delay (2000);
lcd.setCursor(0,1);
lcd.print(" by Peter Bell"
delay (2000);
lcd. clear();
lcd.print(" Version 2.7M" //Version
lcd.setCursor(2, 1);
lcd.print(" 10-10-18"
pinMode(ledPin, OUTPUT);
delay (4000);
lcd. clear();
delay(1000);
// SoD – register the interrupt service routine
// Note 'FALLING' means that switch1 is normally HIGH. The interrupt is triggered whenever switch1 goes low
pinMode(switch1, INPUT_PULLUP ); // SoD: make the interrupt pin normally HIGH to detect FALLING
attachInterrupt( digitalPinToInterrupt( switch1 ), switch1InterruptServiceRoutine, FALLING );
}
void loop()
{
// Serial.begin(9600); SoD: already declared in setup(), only needed once
Serial.println(count);
lcd.setCursor(2, 0);
lcd.print("Zone" // ZONE 1 OFF
lcd.setCursor(9, 0);
lcd.print("1 OFF"
if (count > 155 && count < 175) {
digitalWrite(ledPin, HIGH);// output on
lcd. clear();
lcd.setCursor(2, 0);
lcd.print("Zone" // ZONE 1 ON
lcd.setCursor(9, 0);
lcd.print("1 ON"
}
lcd.setCursor(9, 0);
lcd.print("8 ON"
if (count > 175 && count < 1500) {
digitalWrite(ledPin, LOW); //output off
lcd. clear();
lcd.setCursor(2, 0);
lcd.print("Zone" // ZONE 8 OFF
lcd.setCursor(9, 0);
lcd.print(" END"
}
// count ++ ; SoD: replaced by ISR
lcd.setCursor(6,1);
lcd.print(count);
delay(200); // SoD: may not be necessary, best to avoid delay() if possible.
// if (count == 1500) count = 0; //re-sets
if ( count >= 1500 ) count = 0; // SoD: safer reset! Interrupts mean count==1500 might be skipped.
lcd. clear();
}
Edited By SillyOldDuffer on 11/10/2018 11:51:36
Edited By SillyOldDuffer on 11/10/2018 11:52:51