Go Down

Topic: Map millis to servo (Read 1 time) previous topic - next topic


Hi try to do a little timer for my son that have some time depending of the switch he press.
Here the code
Code: [Select]
#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);


void loop(){
buttonState_2 = digitalRead(buttonPin2);
buttonState_3 = digitalRead(buttonPin3);
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);
    Serial.println(val);    // tell servo to go to position in variable 'pos'
    }    // 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


A sixteen bit "int" cannot represent 60000.
Use unsigned long
"Pete, it's a fool (who) 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.
I speak for myself, not Arduino.


Code: [Select]
      int val;
      while(millis() < millisend){
        val = millis();

All variables used for timing using millis() need to be unsigned longs
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up