Pin not dropping voltage for time clock code

Hi all,

Heads up, i have never programmed before still very early days of learning :slight_smile:

I am having some issues. I have a clock which is passing the hour as a variable.
Then i have digital pins connected to relays.

At its current form its a simple time clock .

My issue is the pins do not seem to lose voltage even though the code says they should of.
I can tell because the serial print stops writing with in the IF statement of any relay .

The pins get voltage when the time is right, but they do not turn off .
They remain on.

Any help will be greatly appreciated

#include <Wire.h>         
 
#define RTC_Address   0x32  //RTC_Address 
 
//Pin Connection 
 
unsigned char   date[7];
int hour;
int pin;
int pinout7 = 7;
int pinout8 = 8;
int pinout9 = 9;
int pinout10 = 10;
int pinout11 = 11;
int pinout12 = 12;
int pinout13 = 13;
String state = "OFF";


void setup()
{
  Wire.begin();
  Serial.begin(9600); 
  pinMode(7, OUTPUT);   
  pinMode(8, OUTPUT);   
  pinMode(9, OUTPUT);  
  pinMode(10, OUTPUT);  
  pinMode(11, OUTPUT);  
  pinMode(12, OUTPUT);  
  pinMode(13, OUTPUT);  
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
}
 
void loop()
{
  I2CWriteDate();//Write the Real-time Clock
  delay(100);
 
  while(1)
  {  
    I2CReadDate();  //Read the Real-time Clock     
    hour = Data_process();//Process the data;
    Serial.print(hour);
    Serial.println();


//          if (hour >=13 and hour <24 ){ // Turn on the lighting  1pm -12am
//                pin = 7;
//                pinaction(pin);
//                Serial.print("Lights 1");
//                Serial.println();
//            }
          
          if (hour >=12 and hour <21 ) // Turn on the lighting Flowering 9am-9-pm
            {
                pin = 8;state = "ON";
                pinaction(pin,state);
                Serial.print(" Lights 2 are on");
                Serial.println();
            }
          
          if (hour >=8 and hour <9 || hour >=11 and hour <13 || hour >=14 and hour <22 ) // Turn on the Fan 2
            {
                
                pin = 9;state = "ON";
                pinaction(pin,state);
                Serial.print("Fan2 is on ");
                Serial.println();
            }
       
          
          if (hour >=9 and hour <10 || hour >=13 and hour <14 ) // Turn on the co2 
            {
                pin = 10;state = "ON";
                pinaction(pin,state);
                Serial.print("Co2 is on morning dose");
                Serial.println();
            }
          

          if (hour >=9 and hour <22 ) // fan
            {
                pin = 11;state = "ON";
                pinaction(pin,state);
                Serial.print("fan");
                Serial.println();
            }
           if (hour >=6 and hour <9 ||hour >=15 and hour <17 ) // Turn on the Air Pump
            {
                pin = 12;state = "ON";
                pinaction(pin,state);
                Serial.print("Airation Active");
                Serial.println();
            }
           
            if (hour >=6 and hour <19 ||hour >=3 and hour <6 ) // Turn on light13 
            {
                pin = 13;state = "ON";
                pinaction(pin,state);
                Serial.print("Light 13");
                Serial.println();
            }

      } 
    
    delay(1000);//Delay 1 seccond 
   
   
}
   

  void pinaction(int a,String b){
    boolean c;
    if (b == "ON"){
      c = LOW;
    }
    if (b == "OFF"){
      c = HIGH;
    }
    // Serial.print("in the pin loop");
    // Serial.println();
    digitalWrite(a, c);
  }
//Read the Real-time data register of SD2403 
void I2CReadDate(void)
{
  unsigned char n=0;
 
  Wire.requestFrom(RTC_Address,7); 
  while(Wire.available())
  {  
    date[n++]=Wire.read();
  }
  delayMicroseconds(1);
  Wire.endTransmission();
}
 
//Write the Real-time data register of SD2403 
void I2CWriteDate(void)
{       
//  WriteTimeOn();
// 
//  Wire.beginTransmission(RTC_Address);        
//  Wire.write(byte(0));//Set the address for writing       
//  Wire.write(0x01);//second:59     
//  Wire.write(0x37);//minute:1      
//  Wire.write(0x95);//hour:15:00(24-hour format)         
//  Wire.write(0x01);//weekday:Monday      
//  Wire.write(0x19);//day:19th      
//  Wire.write(0x10);//month:October     
//  Wire.write(0x15);//year:2015      
//  Wire.endTransmission();
// 
//  Wire.beginTransmission(RTC_Address);      
//  Wire.write(0x12);   //Set the address for writing         
//  Wire.write(byte(0));            
//  Wire.endTransmission(); 
// 
//  WriteTimeOff();  
}
 
//The program for allowing to write to SD2400
void WriteTimeOn(void)
{       
  Wire.beginTransmission(RTC_Address);       
  Wire.write(0x10);//Set the address for writing as 10H         
  Wire.write(0x80);//Set WRTC1=1      
  Wire.endTransmission();
 
  Wire.beginTransmission(RTC_Address);    
  Wire.write(0x0F);//Set the address for writing as OFH         
  Wire.write(0x84);//Set WRTC2=1,WRTC3=1      
  Wire.endTransmission();   
}
 
//The program for forbidding writing to SD2400
void WriteTimeOff(void)
{       
  Wire.beginTransmission(RTC_Address);   
  Wire.write(0x0F);   //Set the address for writing as OFH          
  Wire.write(byte(0));//Set WRTC2=0,WRTC3=0      
  Wire.write(byte(0));//Set WRTC1=0  
  Wire.endTransmission(); 
}
 
//Process the time_data
int Data_process(void)
{
  unsigned int i;
 
  for(i=0;i<7;i++)
  {
    if(i!=2)
      date[i]=(((date[i]&0xf0)>>4)*10)+(date[i]&0x0f);
    else
    {
      date[2]=(date[2]&0x7f);
      date[2]=(((date[2]&0xf0)>>4)*10)+(date[2]&0x0f);
    }
  }
//Use the serial monitor to see information being transmitted   
Serial.print("Sec = ");//second
  Serial.print(date[0]);
  Serial.print("   Min = ");//minute
  Serial.print(date[1]);
  Serial.print("   H = ");//hour
  Serial.print(date[2]);
  Serial.print("   W = ");//week
  Serial.print(date[3]);
  Serial.print("   D = ");//day
  Serial.print(date[4]);
  Serial.print("   M = ");//month
  Serial.print(date[5]);
  Serial.print("   Y = ");//year
  Serial.print(date[6]);
 
 Serial.println(); 
  hour = int(date[2]);
  return hour;
  

}

I have a clock which is passing the hour as a variable.

Not even remotely possible. Hardware can not pass data.

My issue is the pins do not seem to lose voltage even though the code says they should of.

How do you determine that this is the case?

  pinMode(7, OUTPUT);   
  pinMode(8, OUTPUT);   
  pinMode(9, OUTPUT); 
  pinMode(10, OUTPUT); 
  pinMode(11, OUTPUT); 
  pinMode(12, OUTPUT); 
  pinMode(13, OUTPUT);

So much for the useless names assigned to the pin numbers...

  I2CWriteDate();//Write the Real-time Clock
  delay(100);
 
  while(1)
  {

I fail to understand why you are doing something in loop() that should be done in setup(), and then preventing loop() from actually looping.

Put the write in setup() and get rid of the stupid while loop.

                pin = 8;state = "ON";

ONE statement per line.

  void pinaction(int a,String b){

It's stupid to pass a boolean state as a String. The pin can be on or off. Stop pissing resources away using Strings.

Your real problem, though, is that once you turn a pin on, you never turn it off.

The pinaction() function contributes nothing. Lose it.

All of @PaulS' rather ascerbic comments are worth studying carefully - but I'm not sure if they bear on your problem.

I don't see anywhere in your code where you call the function pinAction() to turn the LED off.

This sort of complex IF statement

if (hour >=8 and hour <9 || hour >=11 and hour <13 || hour >=14 and hour <22 )

is notoriously easy to get wrong. And you have a lot of them.

If I was writing that I would put each IF clause on its own line.

Using meaningful names for everything makes a program self-documenting

...R

A wonderful class that would fit really well to your application, simplify your code and get you more granular control over your timers:

TimeAlarms

don't get distracted by the use of the word Alarms in the title... it is all about timers.