Cookies Vending Machine v1.0 diagram (At least what I think it is) and code

Well, SIGTERMer and many others asked me to share’em, so they’re here.

Truth to be said, I did almost everything without sketching or diagraming it; I did alot of trail and error kinda thing.

So, here’s the sketch:

And here’s the code (Commented):

#include <servo .h>
const int MeButton = 2;     // the number of the pushbutton pin
const int BossButton = 3;     // the number of the pushbutton pin
const int BusyLED =  4;      // the number of the LED pin
const int ReadyLED =  5;      // the number of the LED pin
long day;
Servo servo1;
int BossLimit;

void setup()
{
Serial.begin(9600);
day = 86400; // 60 seconds x 60 minutes x 24 hours
servo1.attach(9);
servo1.write(60); //angle (from 0 to 180). My setup is (60 to 180). Can change it according to your own setup
Serial.begin(9600);
pinMode(2,INPUT); //boss button
pinMode(3,INPUT); //my button
pinMode(4,OUTPUT); //busy LED
pinMode(5,OUTPUT); //ready LED
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
BossLimit = 3;
}

void loop()
{
int x;
x = digitalRead(2);
Serial.println(x,DEC);
x = digitalRead(3);
Serial.println(x,DEC);
Serial.println(BossLimit,DEC);
Serial.println(day);

int sensorValue = digitalRead(2); //if I wanted cookies
if ( sensorValue == 1)
{
servo1.write(180); //stretch
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
delay(2000); //wait for two seconds
servo1.write(60); //pull it back
delay(2000); //wait for another two seconds
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
day = day – 4; //eat 4 seconds of the day counter
}
else
{
sensorValue = digitalRead(3); //if boss wanted cookies
if ( sensorValue == 1 )
{
if ( BossLimit > 0) //if still within limit
{
servo1.write(180); //stretch
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
delay(2000); //wait for two seconds
servo1.write(60); //pull it back
delay(2000); //wait for another two seconds
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
BossLimit–; //drop boss limit by one
day = day – 4; //eat 4 seconds of the day counter
}
else //if reached limit
{
digitalWrite(4,HIGH); //beam up both lights
digitalWrite(4,HIGH); //beam up both lights
delay(1000); //wait for a second
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
day–; //eat a second of day counter
}
}
else //if no buttons pressed
{
if ( day == 0) day = 86400;

delay(1000); //wait for a second
day–; //eat a second of day counter
}
}
}

Anyway, I’ll take my shiny new Sony a55 for a spin today, I’ll let you know how I like it =D

5 thoughts on “Cookies Vending Machine v1.0 diagram (At least what I think it is) and code

  1. question: does the boss limit variable get reset after each day? or does your boss have to starve until you restart your audruino thingi?

    also, can I use your real name (or an arabic nick if you prefer)?

  2. Sorry, forgot to add that… Silly me…

    Replace:

    if ( day == 0) day = 86400;

    With:

    if ( day == 0)
    {
    day = 86400;
    BossLimit = 3
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.