I finished the sketch. Here it is:
------------------------------------------------------------
/*
Arduino vending machine program
*/
int mosfetPin = 8;
int coinPin = 7;
int val;
void setup() {
pinMode(mosfetPin, OUTPUT); // Set mode of pin to output
pinMode(coinPin, INPUT); // Set mode of pin to input
}
void loop() {
val = digitalRead(coinPin); // Read input value and store it in val
if (val == LOW) { // Check if the coin hit the microswitch
digitalWrite(mosfetPin, HIGH); // Activate the mosfet gate
delay(5000); // Allow the motor to run enough to complete task
digitalWrite(mosfetPin, LOW); // Stop motor
}
}
-------------------------------------------------------------
Please check out my sketch and try to point out any flaws.
I don't think that's going to work for you for 2 reasons.
1) Assuming your coin switch is open at rest and closed when a coin passes what you're saying there is "Read the coin pin. If it's LOW (Open, meaning no coin) then run the motor for 5 seconds" Basically you're motor will just run and run giving out free sodas.
2) You had said you're charging $0.75 per soda so you'll need to count the coins.
(Note: Just throwing this together. Not sure if this will work.)
/*
Arduino vending machine program
*/
int mosfetPin = 8;
int coinPin = 7;
int val;
int coinCount = 0;
void setup() {
pinMode(mosfetPin, OUTPUT); // Set mode of pin to output
pinMode(coinPin, INPUT); // Set mode of pin to input
}
void loop() {
val = digitalRead(coinPin); // Read input value and store it in val
if (val == HIGH) { // Check if the coin hit the microswitch
coinCount++; // add 1 to the coin count
if(coinCount == 3) { // If we've collected 3 coins
digitalWrite(mosfetPin, HIGH); // Activate the mosfet gate
delay(5000); // Allow the motor to run enough to complete task
digitalWrite(mosfetPin, LOW); // Stop motor
coinCount = 0; // reset the coinCount for the next customer
}
}
I also foresee some problems with the if(val == HIGH) being run multiple times and causing the coin count to go into the double or even triple digits. Imagine this program is looping a thousand times a second. As the coin is about to come through it's going:
val = LOW
val = LOW
val = LOW
Then all of a sudden a coin comes through and closes your switch for say 50 milliseconds. Now it's going
val= HIGH
coinCount++
val= HIGH
coinCount++
val= HIGH
coinCount++
val= HIGH
coinCount++
val= HIGH
coinCount++
... until the switch opens again. (Actually once it hit 3 it would dispense a soda really only charging $.25 or it.) You can probably get past that by putting a delay inside that if statement.
You can also look search 'debounce' or 'debouncing' on the main Arduino site.