I am in the process of building an exposure unit. Its controlled by a timer the user can adjust for proper burn time. The code is working fine the way it is. The problem I'm running into now is that the vacuum top is slowly leaking, so i need to re-write the code using millis instead of the delay so i can turn the vacuum on/off during the duration of the exposure.
Currently, when the start button is pressed, the transistorPin goes HIGH but the time doesnt count down.
I haven't yet included the code, but the vacuum will turn on every 30 seconds for 10 seconds. I'm not exactly sure where the code is wrong, so any help would be much appreciated. ive been pulling hairs for days now.
code
int startPin = 9; //start button pin
int upPin = 6; //up button pin
int downPin = 8; //down button pin
int transistorPin = 7; //transistor base pin
int vacuumPin = 10; //vacuum pin
int buzzerPin = 13; //piezo buzzer pin
int upRead1 = 0; //variable to read up button
int upRead2 = 0; //debounce variable for up button
int downRead1 = 0; //variable to read down button
int downRead2 = 0; //debounce variable for down button
int startRead1 = 0; //variable to read start button
int startRead2 = 0; //debounce variable for start button
int timerCount = 0; //variable for timer setting
int minutesCount = 0; //minutes value
int secondsCount = 0; //seconds value
int timerCount2 = timerCount; //variable for timer setting
int minutesCount2 = timerCount2; //minutes value
int secondsCount2 = secondsCount; //seconds value
//millis delays
long currentTime = millis();
long previousTime1 = 0; //debounce
long previousTime2 = 0; // interval
long interval = 1000;
long vacuumONinterval = 10000;
long vacuumOFFinterval = 30000;
long debounceInterval = 10; //debounce
long slowdownInterval = 400; // rate at which buttons add time
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.print("UV LED Lightbox");
delay(1000);
lcd.clear();
lcd.print("Exposure Time?");
pinMode(transistorPin, OUTPUT);
pinMode(vacuumPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(startPin, INPUT);
pinMode(upPin, INPUT);
pinMode(downPin, INPUT);
Serial.begin(9600); //open serial port
}
void lcdWrite()
{
lcd.setCursor(0, 0); //set cursor to top left
lcd.print(minutesCount); //print minutes value
lcd.setCursor(3, 0); //set cursor on other side of minutes value
lcd.print("min"); //print min string
lcd.setCursor(7, 0); //set cursor further right
lcd.print(secondsCount); //print seconds value
lcd.setCursor(10,0); //set cursor further right
lcd.print("sec"); //print sec string
}
void loop() {
//read all three buttons
upRead1 = digitalRead(upPin);
downRead1 = digitalRead(downPin);
startRead1 = digitalRead(startPin);
//debounce read for all three buttons
if ((unsigned long)(millis()) - previousTime1 >= debounceInterval){
upRead2 = digitalRead(upPin);
downRead2 = digitalRead(downPin);
startRead2 = digitalRead(startPin);
previousTime1 = (unsigned long) (millis());
}
//slow down button value change speed
delay(200);
//if up button pressed increment timer count and print to lcd
if(upRead1==upRead2 && upRead1==HIGH)
{
lcd.clear(); //clear screen
secondsCount = secondsCount+5; //increment in sec intervals
if(secondsCount==60) //increment minute & rest sec for every 60 sec
{
minutesCount++;
secondsCount=0;
}
lcdWrite(); //print values
}
//if down button pressed decrement timer count and print to lcd
if(downRead1==downRead2 && downRead1==HIGH)
{
lcd.clear(); //clear lcd
secondsCount = secondsCount-5; //decrement minute & reset sec for every 60 sec
if(minutesCount>0) //if more than a minute left
{
if(secondsCount==-5) // reset sec value of -5 to 55 & decrement minute
{
secondsCount=55;
minutesCount--;
}
else
{
if(secondsCount<0) //if countdown finished, reset values to zero
{
secondsCount = 0;
minutesCount = 0;
}
}
}
lcdWrite();
if(secondsCount<0)
{
secondsCount=0;
lcd.clear();
lcd.print("Exposure Time?");
}
}
//if start button pressed activate transistor base and
//print timer count down to lcd
//activate buzzer to signal end of count
if(startRead1==startRead2 && startRead1==HIGH)
{
lcd.clear();
lcd.print("Please Wait...");
digitalWrite(vacuumPin, HIGH);
delay(5000);
digitalWrite(vacuumPin, LOW);
lcd.clear();
lcd.print("vacuum complete");
delay(1000);
lcd.clear();
timerCount = (minutesCount*60) + secondsCount;
int timerCount2 = timerCount;
Serial.println(timerCount);
for(int i=timerCount;i>0;i--)
{
lcd.clear();
if(timerCount2>60)
{
minutesCount = timerCount2/60;
secondsCount = timerCount2%60;
}
else if(secondsCount==60)
{
secondsCount=59;
}
else
{
minutesCount = 0;
secondsCount = timerCount2;
}
lcdWrite();
//send transistor base pin high
digitalWrite(transistorPin, HIGH);
currentTime = (unsigned long) (millis());
if(currentTime - previousTime2 >= interval)
{
previousTime2 = previousTime2 + 1000;
timerCount2 == timerCount2--;
digitalWrite(13, !digitalRead(13));
}
if( i = 0){
lcd.clear();
lcd.print("Complete");
//turn transistor off
digitalWrite(transistorPin, LOW);
//turn on piezo buzzer
analogWrite(buzzerPin, 255);
delay(2000);
//turn off piezo buzzer
analogWrite(buzzerPin, 0);
delay(2000);
lcd.clear();
lcd.print("Complete");
delay(3000);
lcd.clear();
//reset timer values
lcd.setCursor(0, 0); //set cursor to top left
lcd.print(minutesCount); //print minutes value
lcd.setCursor(3, 0); //set cursor on other side of minutes value
lcd.print("min"); //print min string
lcd.setCursor(7, 0); //set cursor further right
lcd.print(timerCount); //print seconds value
lcd.setCursor(10,0); //set cursor further right
lcd.print("sec"); //print sec string
}
}
}
}