Hi all,
I'm working on a project where I need to switch between two modes (ICT
and FCT
) using button presses. A single button press triggers ICT
, and a long press triggers FCT
. Once in FCT
, a single press should keep me in FCT
, and a long press should switch to Lux
. Similarly, in Lux
, I want a single press to go back to FCT
.
However, I'm experiencing issues where my code keeps re-entering the void setup functions, starting from the first and the transitions aren't working as expected. Below are some key snippets of my code. I don't know where is the mistake why my code is not working? Can someone please help me.
#include <LiquidCrystal.h>
#include <Wire.h>
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h"
#define AL_ADDR 0x29
SparkFun_Ambient_Light light(AL_ADDR);
const int rs = 12, en = 11, d4 = 3, d5 = 4, d6 = 5, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
float gain = .125;
int time = 100;
long luxVal = 0;
int buttonstate;
int button = 2;
int vPIN = A1;
float voltage;
float R1 = 10000.0;
float R2 = 10000.0;
int value = 0;
int analogPin = A0;
int raw = 0;
float Vin = 5.0;
float Vout = 0;
float R3 = 0;
float buffer = 0;
int pin1=13;
int pin2=A3;
int pin3=9;
int pin4=10;
int pin5 = 7;
int pin6 =8;
int r=0;
int lvpin = A2;
int out=0;
float ic_volt;
int sum=0;
int avg=0;
int i=0;
int buf[10]={0};
int count =10;
int total=0;
volatile unsigned long pressStartTime = 0;
volatile unsigned long pressDuration = 0;
volatile bool isPressed = false;
const unsigned long longPressThreshold = 500; // Threshold for long press in ms
bool inFCT = false; // Variable to track if we're in the FCT mode
void buttonPinInterrupt() {
if (digitalRead(button) == LOW) { // Button pressed
if (!isPressed) { // Only trigger when button is initially pressed
pressStartTime = millis(); // Record the time when the button is pressed
isPressed = true;
}
} else { // Button released
if (isPressed) {
pressDuration = millis() - pressStartTime; // Calculate press duration
isPressed = false; // Reset press state
}
}
}
void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button), buttonPinInterrupt, CHANGE);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
lcd.begin(16, 2);
Wire.begin();
light.begin();
if(light.begin())
Serial.println("Ready to sense some light!");
else
Serial.println("Could not communicate with the sensor!");
light.setGain(gain);
light.setIntegTime(time);
Serial.println("Reading settings...");
Serial.print("Gain: ");
float gainVal = light.readGain();
Serial.print(gainVal, 3);
Serial.print(" Integration Time: ");
int timeVal = light.readIntegTime();
Serial.println(timeVal);
}
void loop() {
if (isPressed) {
return; // Wait until button is released
}
if (pressDuration > 0) {
if (pressDuration < longPressThreshold) {
// Handle single tap
pressDuration = 0; // Reset
if (!inFCT) {
Serial.println("Single tap detected: Executing ICT");
ICT();
} else {
Serial.println("Single tap detected in FCT: Staying in FCT");
}
} else {
// Handle long press
pressDuration = 0; // Reset
if (inFCT) {
Serial.println("Long press detected in FCT: Switching to Lux");
Lux();
} else {
Serial.println("Long press detected: Executing FCT");
FCT();
}
}
}
}
void ICT()
{
lcd.clear();
lcd.setCursor(3, 0); // Set cursor to the first row
lcd.print("RESISTANCE");
lcd.setCursor(3,1);
lcd.print("MEASUREMENT");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RESISTANCE MODE");
lcd.setCursor(0, 1); // Set cursor to the first row
lcd.print("PROBE-J2,C8,C3");
delay(500);
r=1;
while(r==1)
{
raw = analogRead(analogPin);
if(raw)
{
buffer = raw * Vin;
Vout = (buffer)/1024.0;
buffer = (Vin/Vout) - 1;
R3 = (R1 * buffer);
Serial.println(R3);
delay(100);
if(R3 > 500 && R3 < 500000)
{
digitalWrite(pin1,HIGH);
digitalWrite(pin2,LOW);
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("RESISTANCE TEST");
lcd.setCursor(5,5);
lcd.print("PASS");
}
if(R3 < 500)
{
digitalWrite(pin1,LOW);
digitalWrite(pin2,HIGH);
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("RESISTANCE TEST");
lcd.setCursor(3,1);
lcd.print("FAIL");
}
if(R3 > 500000)
{
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("RESISTANCE MODE");
lcd.setCursor(0, 1); // Set cursor to the first row
lcd.print("PROBE-J2,C8,C3");
delay(500);
}
}
buttonstate = digitalRead(button);
if(buttonstate==LOW)
{
Serial.println("ok");
r=0;
voltage_res();
}
}
}
void voltage_res() {
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("VOLTAGE");
lcd.setCursor(3,1);
lcd.print("MEASUREMENT");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VOLTAGE MODE");
lcd.setCursor(0, 1);
lcd.print("PROBE-J2,C8,C3");
delay(500);
r = 1;
while(r == 1) {
value = analogRead(vPIN);
voltage = value * (5.0/1024)*((R1 + R2)/R2);
Serial.print("Voltage =");
Serial.println(voltage);
delay(100);
if(voltage > 3.45 && voltage < 4) {
digitalWrite(pin3,HIGH);
digitalWrite(pin4,LOW);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("VOLTAGE TEST");
lcd.setCursor(7,1);
lcd.print("PASS");
}
if(voltage < 1) {
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("VOLTAGE MODE");
lcd.setCursor(0, 1);
lcd.print("PROBE-J2,C8,C3");
delay(500);
}
if(voltage > 4) {
digitalWrite(pin4,HIGH);
digitalWrite(pin3,LOW);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("VOLTAGE TEST");
lcd.setCursor(7,1);
lcd.print("FAIL");
}
buttonstate = digitalRead(button);
if(buttonstate==LOW)
{
r=0;
Serial.println(buttonstate);
driver_ic();
}
}
}
void driver_ic()
{
lcd.clear();
lcd.setCursor(1, 0); // Set cursor to the first row
lcd.print("DRIVER IC TEST");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DRIVER IC MODE");
lcd.setCursor(0, 1); // Set cursor to the first row
lcd.print("PROBE ACROSS C8");
delay(500);
r=1;
while(r==1)
{
out = analogRead(lvpin);
ic_volt = out * (5.0/1024)*((R1 + R2)/R2);
Serial.print("Voltage =");
Serial.println(ic_volt);
delay(100);
if(ic_volt > 1 && ic_volt < 7)
{
digitalWrite(pin5,LOW);
digitalWrite(pin6,HIGH);
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("DRIVER IC TEST");
lcd.setCursor(5,1);
lcd.print("FAIL");
}
if(ic_volt < 1)
{
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DRIVER IC MODE");
lcd.setCursor(0, 1); // Set cursor to the first row
lcd.print("PROBE ACROSS C8");
delay(500);
}
if(ic_volt > 7)
{
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to the first row
lcd.print("DRIVER IC TEST");
lcd.setCursor(5,1);
lcd.print("PASS");
}
buttonstate = digitalRead(button);
if(buttonstate==LOW)
{
r=0;
ICT();
}
}
}
void FCT() {
inFCT = true; // Set the FCT flag to true
Serial.println("Entered FCT mode");
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("LUX VALUE");
lcd.setCursor(3, 1);
lcd.print("MONITORING");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BEFORE POTTING");
lcd.setCursor(0, 1);
lcd.print("PRESS ENTER KEY");
r = 1;
while (r == 1)
{
buttonstate = digitalRead(button);
// When button is pressed, process the lux value
if (buttonstate == LOW)
{
lcd.clear();
lcd.print("Processing...");
int lux = light.readLight();
Serial.print("Ambient Light Reading: ");
Serial.print(lux);
Serial.println(" Lux");
delay(1000);
int value = lux - 70;
sum = 0;
for (i = 0; i < count; i++)
{
Serial.println("CAME");
buf[i] = value;
sum += buf[i];
}
avg = sum / count;
Serial.println(avg);
// Display PASS/FAIL result
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LUX VALUE TEST");
if (avg <= 115)
{
lcd.setCursor(5, 1);
lcd.print("PASS");
}
else
{
lcd.setCursor(5, 1);
lcd.print("FAIL");
}
delay(5000); // Short delay to allow user to read result
r=0;
}
}
}
void Lux()
{
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("LUX VALUE");
lcd.setCursor(3, 1);
lcd.print("MONITORING");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AFTER POTTING");
lcd.setCursor(0, 1);
lcd.print("PRESS ENTER KEY");
r = 1;
while (r == 1)
{
buttonstate = digitalRead(button);
// When button is pressed, process the lux value
if (buttonstate == LOW)
{
lcd.clear();
lcd.print("Processing...");
int lux = light.readLight();
Serial.print("Ambient Light Reading: ");
Serial.print(lux);
Serial.println(" Lux");
delay(1000);
int value = lux - 55;
sum = 0;
for (i = 0; i < count; i++)
{
Serial.println("CAME");
buf[i] = value;
sum += buf[i];
}
avg = sum / count;
Serial.println(avg);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LUX VALUE TEST");
if (avg <= 55)
{
lcd.setCursor(5, 1);
lcd.print("PASS");
}
else
{
lcd.setCursor(5, 1);
lcd.print("FAIL");
}
delay(5000); // Short delay to allow user to read result
r=0;
// Add logic for Lux sensing and exit from FCT
inFCT = false; // Reset the FCT flag after Lux test
Serial.println("Completed Lux sensing, exited FCT mode");
// loop();
}
}
}