Hi try to do a little timer for my son that have some time depending of the switch he press.
Here the code
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int temps = 0;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
int buttonState_2 = 0;
int buttonState_3 = 0;
int buton1time2 = 30000;
int buton1time3 = 60000;
long int millisstart;
long int millisend;
int second = 1000;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
Serial.begin(9600);
}
void loop(){
buttonState_2 = digitalRead(buttonPin2);
buttonState_3 = digitalRead(buttonPin3);
myservo.write(0);
if (buttonState_2 == LOW) {
temps = buton1time2;
}
if (buttonState_3 == LOW) {
temps = buton1time3;
}
if (temps != 0){
{
millisstart = millis();
millisend = (millisstart + temps);
int val;
while(millis() < millisend){
val = millis();
val = map(val, millisstart, millisend, 0, 180);
myservo.write(val);
Serial.println(val); // tell servo to go to position in variable 'pos'
delay(250);
} // waits 15ms for the servo to reach the position
temps = 0;
}
}
}
Code work good with the 30 second but when i try 60 second it does not work, when i check the val on the serial, i receive some strnge value.
Thanks in advance