Hi all- first time post, and I'm pretty new to Ardunio programming. I've got a few successful projects under my belt, but this one is getting the best of me!
My project is to build a dual-sided PCB exposure box with Arduino controlled timer. I can get that working no problem.
I wanted to incorporate a door safety switch that, when open, turns off the UV, pauses the timer, and turns on a red LED array controlled by PWM set by a pot read by the arduino.
I wrote separate sketches that independently work, but when I combine them:
- Door switch input appropriately controls the red LED signal, and when open, shuts off the time circuit and UV Transistor. YAY.
- When door switch is closed, time circuit activates, red LED circuit turns off, BUT UV LED transistor is on.
- When door swtich is closed, I can set and/or activate countdown timer, and the timer functions, but UV LED transistor is on the whole time, regardless of button state or timer state.
- When I open the door switch mid cycle, the timer circuit pauses and shuts off (desired behavior) as does the UV transistor. When I close the door switch again, no matter how long, the timer picks up where it left off, the red LED goes off, and the UV LED turns on, but it stays on past timer expiration.
Somehow, by incorporating the door switch/LED code, I broke something..but I can't find it!
Here is the first version of the code- v12, that operates the timer setting, UV circuit and countdown just fine, but does not include the door switch, red LED and timer pause functionality. Following this will be a new post with the version I have (v14) that merges the switch, red LED and timer pause, but is not working:
/* This code works for setting, running time, and activating the UV array
when timer starts, and turns it off when the timer is up. Still missing/not working:
Red light - PWM driven from POT connected to door switch
Door switch - turns off UV, turns on Red, and pauses timer
After cycle is finished, pushing the encoder resets timer, but turns UV on.
*/
#include <EEPROM.h>
int segmentPins[] = {12, 2, 13, 16, 18, 4, 5};
int displayPins[] = {6,15,7,14};
int times[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 300, 315, 330, 345, 400, 415, 430, 445, 500, 600, 700, 800, 900, 1000, 1500, 2000, 3000};
int numTimes = 52;
byte selectedTimeIndex;
int timerMinute;
int timerSecond;
int ledState = 10;
unsigned long previousMillis = 0;
const long interval = 500; //colonPin blink interval
int redPin = 3;
int transistorPin = 1;
int doorSwitch = A5;
int redIntensity = 0;
int colonPin = 11;
int aPin = 10;
int bPin = 8;
int buttonPin = 9;
boolean stopped = true;
byte digits[10][7] = {
// a b c d e f g
{ 1, 1, 1, 1, 1, 1, 0}, // 0
{ 0, 1, 1, 0, 0, 0, 0}, // 1
{ 1, 1, 0, 1, 1, 0, 1}, // 2
{ 1, 1, 1, 1, 0, 0, 1}, // 3
{ 0, 1, 1, 0, 0, 1, 1}, // 4
{ 1, 0, 1, 1, 0, 1, 1}, // 5
{ 1, 0, 1, 1, 1, 1, 1}, // 6
{ 1, 1, 1, 0, 0, 0, 0}, // 7
{ 1, 1, 1, 1, 1, 1, 1}, // 8
{ 1, 1, 1, 1, 0, 1, 1} // 9
};
void setup()
{
for (int i=0; i < 7; i++)
{
pinMode(segmentPins[i], OUTPUT);
}
for (int i=0; i < 4; i++)
{
pinMode(displayPins[i], OUTPUT);
}
pinMode(colonPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(transistorPin, OUTPUT);
pinMode(doorSwitch,INPUT);
digitalWrite(transistorPin, LOW);
selectedTimeIndex = EEPROM.read(0);
timerMinute = times[selectedTimeIndex] / 100;
timerSecond = times[selectedTimeIndex] % 100;
}
void loop()
{
{
if (digitalRead(buttonPin))
{
stopped = ! stopped;
analogWrite(colonPin, ledState);
while (digitalRead(buttonPin)) {};
EEPROM.write(0, selectedTimeIndex);
digitalWrite(transistorPin,HIGH);
}
updateDisplay();
}
}
void updateDisplay() // mmss
{
int minsecs = timerMinute * 100 + timerSecond;
int v = minsecs;
for (int i = 0; i < 4; i ++)
{
int digit = v % 10;
setDigit(i);
setSegments(digit);
v = v / 10;
process();
}
setDigit(5); // all digits off to prevent uneven illumination
}
void process()
{
for (int i = 0; i < 100; i++) // tweak this number between flicker and blur
{
int change = getEncoderTurn();
if (stopped)
{
changeSetTime(change);
}
else
{
updateCountingTime();
}
}
if (timerMinute == 0 && timerSecond == 0)
{
analogWrite(colonPin,10);
digitalWrite(transistorPin,LOW);
}
}
void changeSetTime(int change)
{
selectedTimeIndex += change;
if (selectedTimeIndex < 0)
{
selectedTimeIndex = numTimes;
}
else if (selectedTimeIndex > numTimes)
{
selectedTimeIndex = 0;
}
timerMinute = times[selectedTimeIndex] / 100;
timerSecond = times[selectedTimeIndex] % 100;
}
void updateCountingTime()
{
static unsigned long lastMillis;
unsigned long m = millis();
if (m > (lastMillis + 1000) && (timerSecond > 0 || timerMinute > 0))
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// if LED is off, turn it on, and vice-versa
if (ledState == 0){
ledState = 10;
}else {
ledState = 0;
}
analogWrite(colonPin, ledState);
if (timerSecond == 0)
{
timerSecond = 59;
timerMinute --;
}
else
{
timerSecond --;
}
lastMillis = m;
}
}
}
void setDigit(int digit)
{
for (int i = 0; i < 4; i++)
{
digitalWrite(displayPins[i], (digit == i));
}
}
void setSegments(int n)
{
for (int i = 0; i < 8; i++)
{
digitalWrite(segmentPins[i],! digits[n][i]);
}
}
int getEncoderTurn()
{
// return -1, 0, or +1
static int oldA = LOW;
static int oldB = LOW;
int result = 0;
int newA = digitalRead(aPin);
int newB = digitalRead(bPin);
if (newA != oldA || newB != oldB)
{
// something has changed
if (oldA == LOW && newA == HIGH)
{
result = -(oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}

