#include <LiquidCrystal_I2C.h>
// Define the LCD paramters
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address, columns, rows
// Pin definitions
const int buttonPin1 = 2; // Button to momentarily turn on LED
const int buttonPin2 = 3; // Button to decrease delay
const int buttonPin3 = 4; // Button to increase delay
const int increaseButtonPin = 5; // Button to increase a value
const int decreaseButtonPin = 6; // Button to decrease a value
const int pauseButtonPin = 7; // Button to pause/resuem the program
const int ledPin = 13; // Output LED
// Variables
int valueToChange = 100; // Initial value
const int minValue = 60; // Minimum value allowed
const int maxValue = 192; // Maximum value allowed
const int stepSize = 1; // Amount to increase or decrease the value
int ledDelayTime = 1000; // Initial LED on-time in milliseconds
int DelayTimeChange = 10; // Delay time change amount
const int minDelayTime = 0; // Minimum on-time allowed
const int maxDelayTime = 5000; // Maximum on-time allowed
const int MinFlashRate = 1000; // Minimum strobe flash rate allowed
// Last button states
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
// Millis counter for delay adjustment
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 200;
unsigned long LastTrigger = 0;
// Button 1 press counter
int button1PressCount = 0;
// Program state
bool isProgramRunning = true; // Initially, the program is running
bool isProgramPaused = false; // Initially, the program is not paused
void setup() {
pinMode(increaseButtonPin, INPUT_PULLUP);
pinMode(decreaseButtonPin, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(pauseButtonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("CK Video Camera");
lcd.setCursor(0,1);
lcd.print("Inspection System");
Serial.begin(9600); // open the serial port at 9600 bps
}
void loop() {
// Read button states
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int increaseButtonState = digitalRead(increaseButtonPin);
int decreaseButtonState = digitalRead(decreaseButtonPin);
int pauseButtonState = digitalRead(pauseButtonPin);
// Check if the Pause/Resume button is pressed
if (pauseButtonState == LOW && isProgramRunning) {
delay(debounceDelay);
// Toggle the program pause state
isProgramPaused = !isProgramPaused;
Serial.print("Program is ");
Serial.println(isProgramPaused ? "paused" : "resumed");
lcd.clear();
lcd.println(isProgramPaused ? "Freeze" : "Resumed");
}
if (isProgramRunning && !isProgramPaused) {
// Check if Button 1 is pressed and the required number of times
if (button1PressCount < valueToChange) {
if (buttonState1 == LOW && lastButtonState1 == HIGH && millis() - LastTrigger > MinFlashRate) {
// Increment the Button 1 press count
delay(debounceDelay);
button1PressCount++;
Serial.print("Button 1 pressed ");
Serial.print(button1PressCount);
Serial.println(" times.");
lcd.setCursor(0,0);
lcd.print("Gear Counter ");
lcd.print(button1PressCount);
lcd.println(" ");
if (button1PressCount == valueToChange) {
Serial.println("Button 1 press count reached valueToChange.");
// Reset the Button 1 press count
button1PressCount = 0;
// Continue with the rest of your code (LED flashing logic)
Serial.print("Waiting for ");
Serial.print(ledDelayTime);
Serial.println(" seconds");
Serial.print("Last Trigger Time: ");
Serial.println(LastTrigger);
Serial.print("Current Time: ");
Serial.println(millis());
Serial.print("Time since last trigger: ");
Serial.println(millis() - LastTrigger);
delay(ledDelayTime); // Wait to turn on the LED
Serial.println("Triggering for 1/10 second");
LastTrigger = millis();
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(100); // Keep the LED on for 1/2 second
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println();
}
}
}
// Check if Button 2 is pressed to decrease on-time
if (buttonState2 == LOW && lastButtonState2 == HIGH){
delay(debounceDelay);
if(ledDelayTime > minDelayTime) {
ledDelayTime -= DelayTimeChange; // Decrease delay time
Serial.print("Decreased delay to: ");
Serial.println(ledDelayTime);
lcd.setCursor(0,1);
lcd.print("Image Moves Forward ");
}
else {
Serial.println("Minimum delay of 0 reached.");
Serial.println(millis());
lcd.setCursor(0,1);
lcd.print("Max Limit Reached");
}
}
// Check if Button 3 is pressed to increase on-time
if (buttonState3 == LOW && lastButtonState3 == HIGH){
delay(debounceDelay);
if(ledDelayTime < maxDelayTime) {
ledDelayTime += DelayTimeChange; // Increase delay time
Serial.print("Increased delay to: ");
Serial.println(ledDelayTime);
lcd.setCursor(0,1);
lcd.print("Image Move Backwards ");
}
else {
Serial.println("Maximum delay of 5 reached.");
lcd.setCursor(0,1);
lcd.print("Max Limit Reached");
}
}
// Check if Increase Button is pressed
if (increaseButtonState == LOW) {
delay(debounceDelay);
if (valueToChange < maxValue) {
valueToChange += stepSize; // Increase the value
Serial.print("Increased value to: ");
Serial.println(valueToChange);
lcd.setCursor(0,1);
lcd.print("Increase Gear to ");
lcd.println(valueToChange);
} else {
Serial.println("Maximum value reached.");
lcd.setCursor(0,1);
lcd.println("Max Gear Reached");
}
}
// Check if Decrease Button is pressed
if (decreaseButtonState == LOW) {
delay(debounceDelay);
if (valueToChange > minValue) {
valueToChange -= stepSize; // Decrease the value
Serial.print("Decreased value to: ");
Serial.println(valueToChange);
lcd.setCursor(0,1);
lcd.print("Decrease Gear to ");
lcd.println(valueToChange);
} else {
Serial.println("Minimum value reached.");
lcd.setCursor(0,1);
lcd.println("Min Gear Reached");
}
}
}
// Update last button states
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
lastButtonState3 = buttonState3;
}