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
Thanks
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:
Serial.begin(9600);
}
void loop(){
buttonState = digitalRead(buttonPin); // read the pushbutton value:
Serial.println(buttonState);
if (counterStat){
digitalWrite(ledPin, HIGH); //ON
counter += 1000;
Serial.println(counter);
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
}
}