une question de PWM par un débutant

Salut fdufnews,
J'ai un peu progressé depuis...
Ça marche comme je le souhaite désormais :slight_smile:
Merci quand même pour ta réponse

#define UP_TIME             6800  // Time after which speed should decreases
#define DELTA_SPEED       0.95   // The delta decreasing speed.
// Motor state:
#define STOP                  0      // 0: motor stopped
#define GO_DOWN                  1      // 1: motor goes down.
#define GO_UP                  2      // 2: motor goes up.

const int buttonPin = 2;
const int contactPin = 3;
const int buttonPin2 = 4;
const int motorPin1 =  13;   
const int motorPin2 =  12;  
const int motorPwm= 11;

// variables will change:
int buttonState = 0;
int contactState = 0;
int buttonState2 = 0;
long startTime;            // Start time for a movement.
float deltaSpeed;      // Decreasing float.
int inMove;                                                            
int motorPower;      

void setup() {

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPwm, OUTPUT);   
  pinMode(contactPin, INPUT);   
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  Serial.begin(9600);

  inMove=STOP;                 // Not in movement at init.
  deltaSpeed=DELTA_SPEED;  // Set the delta decreasing speed.

  digitalWrite(motorPin1, LOW);      // No movement.
  digitalWrite(motorPin2, LOW);      // No movement.
}

void loop() {
// read the state of  pushbuttons values:
    buttonState = digitalRead(buttonPin);
    contactState = digitalRead(contactPin);
    buttonState2 = digitalRead(buttonPin2);

  if (!inMove) {  // Check if arm is in movement.

    if (buttonState == HIGH) { // Is the main button pressed ?
       Serial.print(65, BYTE);      
       delay(300);
       startTime=millis();                                                         // Set motor Power.                                                                        // Wait for 300 ms...                                            // Go down.
       analogWrite(motorPwm, 255);
       digitalWrite(motorPin1, HIGH);// Set motor Power. 
       inMove=GO_DOWN;                                                                                                                    
    }
  }
  else {
    switch (inMove) {
      
      case GO_DOWN:

      if (buttonState == HIGH && buttonState2 == HIGH) {                                                // System stops itself.
        Serial.print(66, BYTE);
        delay(10);
        analogWrite(motorPwm, 0);                                          
        digitalWrite(motorPin1, LOW);                                                                              // Full speed for next time.
        motorPower=255;        // Set new power (full power to UP).
        delay(250);                                             // Set state to GO-UP.
        analogWrite(motorPwm, motorPower); 
        digitalWrite(motorPin2, HIGH);      // Go up.                               
        inMove=GO_UP; 
      }
      break;

        case GO_UP:
        if (millis()-startTime>=UP_TIME) {                    // If near contactor...
           motorPower=(long) ((float) motorPower*deltaSpeed);  // .. decrease motor speed.      
           analogWrite(motorPwm, motorPower+160);   // Set new power. 
           digitalWrite(motorPin2, HIGH);      
      }
        if (contactState == HIGH) {  // No movement.
          Serial.print(67,BYTE);            // System is returned to original state.
          analogWrite(motorPwm, 0);      // Set power to 0 (stop engine ;o) ).
          digitalWrite(motorPin2, LOW);
          inMove=STOP;
      }
      break;
    }
  }
}