Time limit on button press for led and memory alloy

Im quite new to coding in arduino.

I am working on a project using memory alloys (muscle wires) i have a pushbutton that when you press will send the current through the memory alloy and cause it contract.
This part i can do. but the alloy is susceptible to over heating so i would like to add a time limit of 10 seconds to the button so even if someone holds it down for 20 seconds the current will automatically cut off after 10.

I have written a code based on my experience in processing but it does not turn off the current after 10 seconds.

this code is just for an led so i can get this part working before i add the extra components associated with the memory alloy

Looking for some advice- if i need to be using the millis function. or a example of a code that would work in this manor


const int buttonPin = 2 ;     // the number of the pushbutton pin
const int ledPin = 13 ;    // connected to the base of the transistor

int counter = 0;
boolean counterStat = false;
int maxOn = 10000;

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 pinMode(ledPin, OUTPUT);   // transistor pin as an output:    
 pinMode(buttonPin, INPUT);     //  pushbutton pin as an input:

void loop(){
  buttonState = digitalRead(buttonPin);  // read the pushbutton value:
     if (counterStat){
   digitalWrite(ledPin, HIGH); //ON
   counter += 1000;
    if (buttonState == HIGH) {    
    counterStat = true;   
  else if(buttonState==LOW && counterStat==false) {
    digitalWrite(ledPin, LOW); 

   if(counter >= maxOn) {
    counterStat = false;
    counter = 0;
    digitalWrite(ledPin, LOW); //OFF

Perhaps something like this pseudo code

unsigned long startMillis;
unsigned long maxTimeMillis = 10000;

boolean buttonPress = digitalRead(myButtonPin);
if (buttonPress) {
   startMillis = millis();
while (buttonPress && (startMillis - millis() <= maxTimeMillis)) {
   // turn on memory metal
   buttonPress = digitalRead(myButton); // in case button is released
// turn off memory metal

I can think of lots of potential problems with this simple approach, but it should give you the idea.