Project: Road barrier

I need help with a project I’m working on.
Have made a controller for a electrical roadbarrier.
It works like this: I have many remote controlls that gives the atmega a pulse (input 7) and then opens the barrier.
Also there is another button input that puts the barrier to “shutdown”. A sleep mode where it stays open until I tell it to go back to normal mode. Need to hold this button down for 5sec.
The final function is that I attached a DS1307 RTC. Every wednesday at 06.00 am it will go open, and close again 4.05 pm.

Here is the code:

#include <EEPROM.h>

#include <Wire.h>

int clockAddress = 0x68;  // This is the I2C address
int command = 0;  // This is the command char, in ascii form, sent from the serial port     
long previousMillis = 0;  // will store last time Temp was updated
unsigned long Tidteller = 0;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test; 
byte storedstate;
enum {CLOSED, OPENING, OPEN, CLOSING, SHUTDOWN, FORCEOPEN} state;
unsigned long OpenTimer = 0;
unsigned long ForceOpen = 0;
unsigned long startPress = 0;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, 
// Probably need to put in checks for valid numbers.
void setDateDs1307()                
{
  // Use of (byte) type casting and ascii math to achieve result.  
  second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); 
  minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  dayOfWeek = (byte) (Serial.read() - 48);
  dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.write(decToBcd(second));  // 0 to bit 7 starts the clock
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));    // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
  // Reset the register pointer
  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.endTransmission();

  Wire.requestFrom(clockAddress, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.read() & 0x7f);
  minute     = bcdToDec(Wire.read());
  
  // Need to change this if 12 hour am/pm
  hour       = bcdToDec(Wire.read() & 0x3f);  
  dayOfWeek  = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  month      = bcdToDec(Wire.read());
  year       = bcdToDec(Wire.read());

  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  //Skriver ukedag:
  ukedagprint(); 
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);

}



void ukedagprint()
{
  if(dayOfWeek == 1)
  {
    Serial.print("Mandag  ");
  }
  if(dayOfWeek == 2)
  {
    Serial.print("Tirsdag  ");
  }
  if(dayOfWeek == 3)
  {
    Serial.print("Onsdag   ");
  }
  if(dayOfWeek == 4)
  {
    Serial.print("Torsdag   ");
  }
  if(dayOfWeek == 5)
  {
    Serial.print("Fredag   ");
  }
  if(dayOfWeek == 6)
  {
    Serial.print("Lørdag   ");
  }
  if(dayOfWeek == 7)
  {
    Serial.print("Søndag   ");
  }
}


void tidprint()
{
  Serial.println("");
  Serial.print(dayOfMonth);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print("  ");
  ukedagprint();
  Serial.print("   ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.print(second);
}



void setup()
{ 
  Wire.begin();
  Serial.begin(57600);
  pinMode(3, OUTPUT);      //Relay for opening barrier
  pinMode(4, OUTPUT);      //Relay for closing barrier
  pinMode(5, INPUT);       //Sensor, barrier closed
  pinMode(6, INPUT);       //Sensor, barrier open
  pinMode(7, INPUT);       //Open-button input 
  pinMode(8, INPUT);       //Input forr shutdown button
  pinMode(9, INPUT);       //Object moving sensor
  pinMode(13, OUTPUT);     //Shutdown led
  tidprint();
  Serial.println(" Oppstart ok");
  

  storedstate = EEPROM.read(0);
  if(storedstate == 0)
  {
    state = CLOSED;
  }
  else if(storedstate == 1)
  {
    state = SHUTDOWN;
  }
  else if(storedstate == 2)
  {
    state = FORCEOPEN;
  }
  else
  {
    state = SHUTDOWN;
  }
}

void loop()
{  
  
// Reset the register pointer
        Wire.beginTransmission(clockAddress);
        Wire.write(byte(0x00));
        Wire.endTransmission();

        Wire.requestFrom(clockAddress, 7);

        // A few of these need masks because certain bits are control bits
        second     = bcdToDec(Wire.read() & 0x7f);
        minute     = bcdToDec(Wire.read());
  
        // Need to change this if 12 hour am/pm
        hour       = bcdToDec(Wire.read() & 0x3f);  
        dayOfWeek  = bcdToDec(Wire.read());
        dayOfMonth = bcdToDec(Wire.read());
        month      = bcdToDec(Wire.read());
        year       = bcdToDec(Wire.read());
        


  if (Serial.available())
  {
    command = Serial.read();
    if (command == 84)       
    {
      setDateDs1307();
      getDateDs1307();
      Serial.println(" ");
    }
    else if (command == 71)   
    {
      Serial.print("Storedstate:  ");
      Serial.println(storedstate);
      getDateDs1307();
      Serial.println(" ");
    }
    
    Serial.print("Command: ");
    Serial.println(command);
  }

continuing with the code:

  switch (state) 
    {
      

    case CLOSED: 
      if ((dayOfWeek ==3) && (hour == 6) && (minute == 0)) 
        {
        state = FORCEOPEN;
        EEPROM.write(0, 2);      //Lagrer status i minnet
        tidprint();
        Serial.println(" State =  Closed -> Forceopen");
        digitalWrite(3, HIGH);
        }
      if (digitalRead(7))                    
        {
        state = OPENING;
        tidprint();
        Serial.println(" State = closed -> Opening");
        digitalWrite(3, HIGH);               
        }


      if (digitalRead(8)  == LOW)  
      {
        startPress = 0; 
      } 
      else              
      { 
        if (startPress == 0) 
        { 
          startPress = millis();  
        } 
        else if (millis() - startPress > 5000)   
        { 
          state = SHUTDOWN;
          EEPROM.write(0, 1);
          tidprint();
          Serial.println(" State = closed -> Shutdown");
          digitalWrite(3, HIGH);
          digitalWrite(13, HIGH);
        } 
      }
      
      
      
      break;
      
            
    case OPENING: 
      if (digitalRead(6))                    
        {
        state = OPEN;
        tidprint();
        Serial.println("State = Opening -> Open");
        digitalWrite(3, LOW);               
        OpenTimer = millis();                
        }
      break;
      

    case OPEN:
      digitalWrite(3, LOW);
      if (digitalRead(9))                   
        {
        OpenTimer = millis();                
        tidprint();
        Serial.println(" Sensor Fotocelle");
        }
      if (millis() - OpenTimer > 20000)     
        {
        state = CLOSING;                
        tidprint();
        Serial.println(" State = Open -> closing");
        digitalWrite(4, HIGH);              
        }
      break;
      

    case CLOSING: 
       if (digitalRead(9))
         {
         digitalWrite(4, LOW);             
         delay(300);
         digitalWrite(3, HIGH);             
         state = OPENING;
         tidprint();
         Serial.println(" State = Closing -> opening pga sensor");
         }
       if (digitalRead(5))                  
         {
         digitalWrite(4, LOW);             
         state = CLOSED;
         tidprint();
         Serial.println(" State = Closing -> closed");
         }
       break;
       

    case SHUTDOWN:
      if (digitalRead(6) == 0)                 
        {
          digitalWrite(3, HIGH);              
        }
      else
        {
          digitalWrite(3, LOW);          
        
          if (digitalRead(8))                 
          {
            digitalWrite(13, LOW);    
            OpenTimer = millis();          
            state = OPEN;                 
            EEPROM.write(0, 0);              
            tidprint();
            Serial.println(" State =  Shutdown -> Open");
          }
        }
      break;  
      
    case FORCEOPEN:
    
      if (digitalRead(6) == 0)    
      {
        digitalWrite(3, HIGH);   
      }
      else                        
      {
        digitalWrite(3, LOW);    
      }
      

      if ((dayOfWeek ==3) && (hour == 16) && (minute >= 5))  
        {
          
        state = OPEN;                        
        EEPROM.write(0, 0);                  
        tidprint();
        Serial.println("State = ForceOpen -> Open"); 
        OpenTimer = millis();                
        }

      if (digitalRead(8))               .
        {
        state = SHUTDOWN;              
        EEPROM.write(0, 1);            
        tidprint();
        Serial.println(" State = Forceopen -> shutdown");
        digitalWrite(13, HIGH);
        }
      
      break;  
  }
  command = 0;
  delay(200);
}

And here is my problem:

Sometimes it jumps from state 'closed' to state 'shutdown' without me puching any buttons. It happends randomly. Can there be an error with my code? I guess this could have been done much easier, but this is my first arduino project. I use a standalone atmega328 on a homemade pcb. Inputs are pulled down with 10kohm resistors and I use the 5v to every button / sensor

Do you have teh proper pull-up/pull-down resistors on your inputs? If there is any tendency for an input to float you WILL get false triggers. Also check to make sure grounds are all tied together. What kind(s) of power supply(s) are you using? Anything that could cause them to do funny things?

ah snap! ofcourse.. I forgot capasitors when I made the powersupply.. maybe thats why its wierd.

i use a LM7805 5v regulator. But totally forgot capasitors. Any idea what capasitors I should use?