Need help with my candybox-script :D

Hi!

I need some help with my first Arduino project. I’m new to coding as well.
So here’s the idea:
I want to make a box (for candy) that is shut during weekdays, and opens itself on friday 12:00.
And on sunday around midnight it shuts again.

I don’t really have any of the electrical hardware yet, so I’m not able to test this home. But I’m thinking of using an Arduino Uno, 1 green LED, 1 red LED and a servo. The servo will be connected to a sliding barrel bolt that locks the box.

Right now I’m starting with a test of the lock when it starts. I should probably put the testfaze in the setup.
Also I’m using delay to control the timeing. Because I don’t know to code with time(days, hours etc.).
Please check out my script and if you have a better idea, please let me know.

// Candybox

// 1 sec = 1000, 1 min = 60 000, 1 hour = 3 600 000, 1 day = 86 400 000
// Script has to be started on monday at 12.00 

#include <Servo.h>

Servo myservo;

int pos = 0;
int RED = 13;
int GREEN = 14;

void setup() {
   pinMode(RED, OUTPUT);
   pinMode(GREEN, OUTPUT);
   myservo.attach(9);
}

void loop() 
{
  
  digitalWrite(RED, HIGH);  // Testfaze, locks and opens, then 5 sec until it closes 
  digitalWrite(GREEN, HIGH);
  delay(1000);
  digitalWrite(GREEN, LOW);
  digitalWrite(RED, LOW);
  
 for(pos = 0; pos < 140; pos += 1)  
  {                                  
    myservo.write(pos);               
    delay(5);                     
  } 

 
  delay(5000);
  digitalWrite(RED, HIGH);


  // Testfaze over

  delay(345600000);        // Opens friday at 12.00
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, HIGH);
  for(pos = 140; pos>=1; pos-=1)   
  {                                
    myservo.write(pos);               
    delay(5);                       
  } 

  
  delay(216000000);        // Lock sunday 00:00 / midnight
  digitalWrite(RED, HIGH);
  digitalWrite(GREEN, LOW);
  for(pos = 0; pos < 140; pos += 1)  
  {                                  
    myservo.write(pos);               
    delay(5);                     
  } 


  delay(43193000);         // until monday at 12:00
  

}

for extremely long time intervals like what u need.. u will have to use a real time clock..RTC http://www.ebay.com/itm/New-RTC-I2C-DS1307-AT24C32-Real-Time-Clock-Module-for-Arduino-AVR-PIC-51-ARM-US-/401021672535?hash=item5d5ec11c57:g:7KcAAOSwlaxWOCUD there are many.. find what u need/like.. all arduinos 'roll over' after x days.. not good for a project like this..

all arduinos 'roll over' after x days.. not good for a project like this..

Complete and utter nonsense! The rollover happens, but is easily and safely handled with very simple logic. There is an example sketch that ships with the IDE that demonstrates it, "Blink without Delay". Go have a look, and stop spreading misinformation.

http://forum.arduino.cc/index.php?topic=382368.0

doesnt look good. RTCs are there to do exactly what this project is needing to do.. exactly at X time on Y day..do something..

arduinos are not RTC compatible.. u will end up with time drift/problems/hangup...blah blah.. just buy a 3 dollar RTC and all is good..

backwoodsjack: http://forum.arduino.cc/index.php?topic=382368.0

doesnt look good. RTCs are there to do exactly what this project is needing to do.. exactly at X time on Y day..do something..

arduinos are not RTC compatible.. u will end up with time drift/problems/hangup...blah blah.. just buy a 3 dollar RTC and all is good..

While I agree with your general point, it seems that you are still clinging to your belief by referring to a "hangup". There isn't one. There also isn't really any "problem" other than the time drift, and lack of battery backup for the time.

The thread you linked to simple illustrates that like most programming tasks, it has to be done correctly. If a person can't do things right, they should give up programming and take up gardening or something less "left brain".

chriswer: And on sunday around midnight it shuts again.

Why?

The box will be empty. :)

OK, that's a joke, but how will you open it to replace the candy?

...R

Thanks for all the feedback.

So just to be clear, you all agree that using the delay-function would not work for this project?

@backwoodsjack: By roll-over you mean it will crash and restart? I just bought a clock module online, so thanks for the input! I will just have to learn how to code with time, but that's just a Google search away.

@Robin2: Haha, you're right! We only buy candy on fridays, so that's when we'll fill it. I could add another way to access it, but that would defeat the purpose.

chriswer: @Robin2: Haha, you're right! We only buy candy on fridays, so that's when we'll fill it.

That makes even less sense :)

By the way I am not trying to discourage you from an interesting Arduino project.

...R

You're right, that sounded weird. Well, as of now we always have some candy lying around. And if we buy new candy, we do it on fridays before the weekend. So we will be able to access the candy during the weekend, and any leftovers will be put tin the box on sundays.

If the idea will work in real life, I don't know. But I really don't care because this turned out to be a real fun project! :)