UVC cabinet circuitry/code help

hello! i am currently designing a system to be installed in a cabinet with a uv light that produces ozone in order to disinfect whatever is placed in the cabinet. the semantics are unimportant, but i am having certain trouble with interfacing the code with the current hardware. currently i have a stripped basic model of the circuit (see attached). the issue i am experiencing is with the door sensor. it is a simple reed switch, and the pin it is connected to (see code and schematic) is set as an INPUT_PULLUP. i have an ISR set up to detect when the state changes (door opens or closes), but for some reason the system tells me the door is open right away which is expected, but after i close the door for the first time causing the pin to go low (grounded) i cannot seem to pull the pin high again.

i have added delays (a no no, i understand) simply to test, but have definitely tried without the delays. i feel like there is a loss somewhere or an overflow.

NOT PICTURED:

i have attempted to pull the pin high with an external 1k pullup resistor from pin 0, where the reed switch is connected, to the 3v3 pin, and although the voltage on pin 0 will read 3v3, the digitalRead(0) still outputs 0!

(please forgive my ignorance on preference of how attachments are, well, attached.)\

updated cabinet code.txt (2.94 KB)

Why on Earth are you using interrupts?

Op’s code

//start with door open



int testPin = 0; //this will test if door is open/closed
int relayPin = 1; //this will control relay
int analogPin = 2;
int ledPin = 13; //debugging pin
int GrelayPin = 3; //pin for green light relay
int RrelayPin = 4; //pin for red light relay
unsigned long int Time0 = 0; //reference time for millis
unsigned long int Time1 = 0; //time for light to turn on
unsigned long int Time2 = 0; //time for red safety lights to turn on
unsigned long int Time3 = 0; //time for green safety lights to turn on
int safeInterval = 200;
int RlightInterval = 120000; //2 minutes, want 3 minutes 37 sec
int GlightInterval = 180000; //3 minutes, want 37 minutes 45 sec
int analogVal = 0;
int relayVal=2;
int testVal=2;
int count=0;

/**
 * changes:
 * make it so ozone sensor is read only to determine if green light should turn on
 * (after uv light, after 30 minute)
 * 
 */

volatile bool flag=1;
volatile bool rFlag=0;
volatile int switchState=0; //0 is unread, 1 is open, 2 is closed

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(testPin, INPUT_PULLUP); //sets to input pullup
  digitalWrite(testPin,HIGH);
  pinMode(relayPin, OUTPUT); //sets to output
  pinMode(ledPin, OUTPUT); //set to output
  pinMode(GrelayPin, OUTPUT);
  pinMode(RrelayPin, OUTPUT);
  digitalWrite(relayPin, LOW); //make sure initially light is off
  digitalWrite(RrelayPin, LOW);
  digitalWrite(GrelayPin, LOW);
  attachInterrupt(digitalPinToInterrupt(testPin), ISR, CHANGE); //door opened
  
  
  
  switchState=0;
}

void ISR()
{
  flag= !flag;
}

void Light_off()
{
    Serial.println("turning shit off");
    digitalWrite(relayPin, LOW); //turns light off
    digitalWrite(ledPin, LOW); //debugging
}

void loop() {
    Time0=millis();
    Serial.println(digitalRead(testPin));
    
    if(flag==0) //door is closed
    {
      analogVal=analogRead(analogPin);
      if(digitalRead(testPin)==HIGH)
      {
        flag=1;
      }
      //uv light conditions
      if(Time0-Time1>=safeInterval) //200ms after shutting door
      {
        Time1=Time0;
        //Serial.println("turning UV on");
        digitalWrite(relayPin,HIGH);
      }
      
      //red light conditions
      if((Time0-Time2>=RlightInterval && digitalRead(relayPin)==HIGH) || analogVal>30)
      {
        Time2=Time0;
        //Serial.println("turning red safety light on");
        digitalWrite(GrelayPin,LOW);
        digitalWrite(RrelayPin,HIGH);
      }

      //green light conditions
      if(analogVal<=30 && Time0-Time3>=GlightInterval)
      {
        Time3=Time0;
        //Serial.println("turning green all clear light on");
        digitalWrite(RrelayPin,LOW);
        digitalWrite(GrelayPin,HIGH);
      }
      delay(150);
    }
    if(flag==1)
    {
      Light_off();
      if(count==0);
      {
        digitalWrite(GrelayPin,HIGH);
      }
      delay(150);
    }
  }
Serial.begin(9600);

What is connected to pin zero?
Pin one?

if(count==0);

Oops

int GlightInterval = 180000;
...
...
attachInterrupt(digitalPinToInterrupt(testPin)

I hope this isn’t an AVR

the semantics are unimportant

I’m guessing software is not your forte

HAHAHA, we all start somewhere ;). Any help is of course appreciated! The pinout schematic was attached as a picture, sorry if it was done incorrectly! The reed switch is connected to pin zero which is then connected to ground. Any tips for a newbie?

TheMemberFormerlyKnownAsAWOL:
Why on Earth are you using interrupts?

Because if the state of the reed switch changes (the cabinet door opens/closes) i need to be able to turn off the uv light without being caught in the middle of a block of code. If there is a better way to do it i am all ears!

TheMemberFormerlyKnownAsAWOL:

int GlightInterval = 180000;

...
...
attachInterrupt(digitalPinToInterrupt(testPin)



I hope this isn't an AVR

I'm guessing software is not your forte

Big oof, amazing what you notice after posting to forum!

TheMemberFormerlyKnownAsAWOL:

if(count==0);

Oops

Yes, oops indeed! Ha

morgansom:
Because if the state of the reed switch changes (the cabinet door opens/closes) i need to be able to turn off the uv light without being caught in the middle of a block of code. If there is a better way to do it i am all ears!

What is your code doing that it takes so long to notice a switch changing state?

TheMemberFormerlyKnownAsAWOL:
What is your code doing that it takes so long to notice a switch changing state?

Maybe i do not need it. I will try again this afternoon without it, but before there was a slight delay likely due to poor coding (go figure :wink: )

I have made the suggested changes, but i am still having a problem where pin 0, once the reed switch is closed and driven to ground, is unable to pull back to high. when connected with a 1k pullup resistor I am able to read 3v3 on the pin, but the micro still does not register that as a "high " value, and Serial.println(digitalRead(0)); still outputs 0

//start with door open



const int testPin = 0; //this will test if door is open/closed
const int relayPin = 1; //this will control relay
const int analogPin = 2;
const int ledPin = 13; //debugging pin
const int GrelayPin = 3; //pin for green light relay
const int RrelayPin = 4; //pin for red light relay
unsigned long int Time0 = 0; //reference time for millis
unsigned long int Time1 = 0; //time for light to turn on
unsigned long int Time2 = 0; //time for red safety lights to turn on
unsigned long int Time3 = 0; //time for green safety lights to turn on
unsigned long safeInterval = 200;
unsigned long RlightInterval = 120000; //2 minutes, want 3 minutes 37 sec
unsigned long GlightInterval = 180000; //3 minutes, want 37 minutes 45 sec
int analogVal = 0;
int relayVal=2;
int testVal=2;

/**
 * changes:
 * make it so ozone sensor is read only to determine if green light should turn on
 * (after uv light, after 30 minute)
 * 
 */

void setup() {
  // put your setup code here, to run once:
  Serial.begin(74880);
  pinMode(testPin, INPUT_PULLUP); //sets to input pullup
  //digitalWrite(testPin,HIGH);
  pinMode(relayPin, OUTPUT); //sets to output
  pinMode(ledPin, OUTPUT); //set to output
  pinMode(GrelayPin, OUTPUT);
  pinMode(RrelayPin, OUTPUT);
  pinMode(analogPin,INPUT);
  digitalWrite(relayPin, LOW); //make sure initially light is off
  digitalWrite(RrelayPin, LOW);
  digitalWrite(GrelayPin, HIGH);
}


void Light_off()
{
    Serial.println("turning shit off");
    digitalWrite(relayPin, LOW); //turns light off
    digitalWrite(ledPin, LOW); //debugging
}

void loop() {
    testVal = digitalRead(testPin);
    Serial.println(testVal);
    Time0=millis();
    delay(5);
    analogVal=analogRead(analogPin);
    delay(5);
    Serial.println(analogVal);
    if(testVal==0) //door is closed
    {
      //uv light conditions
      if(Time0-Time1>=safeInterval) //200ms after shutting door
      {
        Time1=Time0;
        //Serial.println("turning UV on");
        digitalWrite(relayPin,HIGH);
      }
      
      //red light conditions
      if((Time0-Time2>=RlightInterval && digitalRead(relayPin)==HIGH) || analogVal>30)
//      if(Time0-Time2>=RlightInterval && digitalRead(relayPin)==HIGH)
      {
        Time2=Time0;
        //Serial.println("turning red safety light on");
        digitalWrite(GrelayPin,LOW);
        digitalWrite(RrelayPin,HIGH);
      }
//
      //green light conditions
      if(analogVal<=30 && Time0-Time3>=GlightInterval)
//      if(Time0-Time3>=GlightInterval)
      {
        Time3=Time0;
        //Serial.println("turning green all clear light on");
        digitalWrite(RrelayPin,LOW);
        digitalWrite(GrelayPin,HIGH);
      }
    }
    
    else if(testVal==1)
    {
      Light_off();
    }
    delay(5);
  }

here is the new code

everything works fine EXCEPT the analogRead()…when left alone, this analogRead does not allow the pin 0 (the pin that the reed switch is connected to) to pull high again.

    delay(5);
    analogVal=analogRead(analogPin);
    delay(5);

that is the section.

i placed delays around the analogread thinking that maybe the micro was trying to analogread too fast and crashing, but this was not the case.

any help is appreciated!

Which pin is the reed switch attached to? A0 or D0?