help with coin op Project

hi i would like help with my coin op sketch. I have the basic sketch working but I don't know how to implement improvements I would like. right now the sketch activate a really for 10 minutes once the coin is inserted. then the sketch delays for 10 minutes before turning the relay off. I would like it so that every time a coin is entered it would add 10 minutes to the count down time. Just say there is five minutes left and you enter another coin this would add an additional 10 minutes so it will be 15 minutes before the relay shut off and so on . here is my basic sketch

#include "pitches.h"
// constants won't change. They're used here to
// set pin numbers:
const int coinMecPin = 2; // the number of the coin mec pin
const int relayPin = 7; // the number of the relay pin

// variables will change:
int buttonState = LOW; // variable for reading the pushbutton status

void setup() {

pinMode(relayPin, OUTPUT); // initialize the relay pin as an output:
pinMode(coinMecPin, INPUT); // initialize the pushbutton pin as an input:
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(coinMecPin);

// check if the coin mec has taken money.
// if it has, turn on the relay for 10 min with warning sound at 1 min left:
if (buttonState == LOW) {
digitalWrite(relayPin, HIGH);// turn realy on:
mario (); //play mario 1up tone
delay(540000); //keep realy active for 9 mins
warning (); //plays 1 min warning tone
delay (60000); //keeps realy active for 1 min
digitalWrite(relayPin, LOW); // turn realy off

}
}
void mario (){ //mario 1up tone notes
// Play 1-up sound
tone(5,NOTE_E6,125);
delay(130);
tone(5,NOTE_G6,125);
delay(130);
tone(5,NOTE_E7,125);
delay(130);
tone(5,NOTE_C7,125);
delay(130);
tone(5,NOTE_D7,125);
delay(130);
tone(5,NOTE_G7,125);
delay(125);
noTone(5);

}
void warning() { // Play warning sound
tone(5,NOTE_B5,100);
delay(100);
tone(5,NOTE_E6,550);
delay(500);
noTone(5);
}

Which is to say, you want the sketch to do something while the relay is activated - you would like it to listen for more coins.

Go look at the "blink without delay" example. Search for the "two things at once" post.

thanks for the replay yes I want to listen for more coins and if more coins are added I want a countdown timer to increase by 10 minutes per coin

Phoenix2017:
thanks for the replay yes I want to listen for more coins and if more coins are added I want a countdown timer to increase by 10 minutes per coin

That's great. There's a sticky post on this subforum Using millis() for timing. A beginners guide which you should read.

No progress? Did you try "blink without delay" as PaulMurrayCbr advice?
Do not use delays.

thanks for the replay yes I want to listen for more coins and if more coins are added I want a countdown timer to increase by 10 minutes per coin

So, what is the problem?

On any given pass through loop(), some time has elapsed since the last time you decremented the time remaining. If that is enough time, and the remaining time is not 0, decrement the time remaining, and update the variable where you store the time you did that. If the remaining time reached 0, turn the appropriate pin off.

On any given pass through loop(), a coin was, or was not, inserted. If one was, increment the remaining time. If the time was 0, turn the appropriate pin in.

With meaningful names for variables and functions, it shouldn't take more than 30 minutes to get working code.

thanks for that's but I'm new to this so I think that will be to complex for me at this stage any examples ?

so I think that will be to complex for me at this stage

Why?

What I do when I'm faced with a complex program that I don't know enough to write is to break it down into smaller pieces, by calling non-existent functions with non-existent variables.

void loop()
{
   // Get current "time"
   unsigned long now = millis();

   // Is it time to decrement the time remaining?
   if(now - lastDecrement > oneSecond)
   {
      lastDecrement = now;
      timeRemaining--;
   }

   // Did a coin get dumped in?
   if(gotCoin())
      timeRemaining += 1000UL * 10 * 60; // Add 10 minutes

   if(timeRemaining == 0 && deviceIsOn)
      turnDeviceOff();

   if(timeRemaining > 0 && !deviceIsOn)
      turnDeviceOn();
}

So, you can see that you need to declare some variables, with appropriate values - lastDecrement, oneSecond, deviceIsOn - and that you need to develop some functions - gotCoin(), turnDeviceOn(), and turnDeviceOff().

None of those functions seem particularly difficult to develop.

If you need help, take a stab at it, and post your attempt. We promise not to laugh. Too hard.

so i had a go at trying to work this out but still no joy am i anyway right

const int coinMecPin = 2; //the number of the coin mec pin
const int relayPin = 7; //the number of the relay pin

int buttonState = LOW; // variable for reading the buttonstate


void setup() {
  // put your setup code here, to run once:
pinMode(relayPin, OUTPUT);
pinMode (coinMecPin, INPUT);
}

void loop() {

 buttonState = digitalRead(coinMecPin); // read the state of the pushbutton value:
 
// Get current "time"
   unsigned long now = millis();

   // Is it time to decrement the time remaining?
   if(now - lastDecrement > oneSecond)
   {
      lastDecrement = now;
      timeRemaining--;
   }

   // Did a coin get dumped in?
   if(gotCoin());
      timeRemaining += 1000UL * 10 * 60; // Add 10 minutes

   if(timeRemaining == 0 && deviceIsOn)
      turnDeviceOff();

   if(timeRemaining > 0 && !deviceIsOn)
      turnDeviceOn();

  void gotCoin() {
    if (buttonState == LOW);
    digitalWrite (relayPin, HIGH);
    
        
  }
}

but still no joy am i anyway right

The code you posted does something. You expect it to do something. I'm guessing that "no joy" means that what the code does is not what you expect.

You need to tell us what the code actually does, and how that differs from what you want.

const int coinMecPin = 2; //the number of the coin mec pin
const int relayPin = 7; //the number of the relay pin

Do those comments really add any value?

 buttonState = digitalRead(coinMecPin); // read the state of the pushbutton value:

Why would you store the state of the coin mechanism pin in a variable called buttonState? You didn't sew a button onto the Arduino, did you?

   if(gotCoin());

Can; you; find; another; example; of; an; if; statement; that; ends; with; a; semi-colon;, where; the; semi-colon; actually; belongs; there;?

  void gotCoin() {
    if (buttonState == LOW);
    digitalWrite (relayPin, HIGH);
   
       
  }
}

Another useless semicolon in a useless (incorrectly placed) function.

Before wasting time posting code, you should at least make sure it compiles.