Pages: [1]   Go Down
Author Topic: Map millis to servo  (Read 248 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi try to do a little timer for my son that have some time depending of the switch he press.
Here the code
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A sixteen bit "int" cannot represent 60000.
Use unsigned long
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4080
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      int val;
      while(millis() < millisend){
        val = millis();
All variables used for timing using millis() need to be unsigned longs
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: